convolution
The convolution function is:
tf.nn.conv2d (input, filter, strides, padding, use_cudnn_on_gpu=none,
Data_format=none, Name=none)
Input for one-D inputs, fileter for filters (convolution core), d, usually [height, width, Input_dim, output_dim],height, width, respectively, the volume of the kernel of the high, wide. Input_dim, Output_dim the table input dimension and output dimension separately.
Import TensorFlow as tf
x1 = tf.constant (1.0, Shape=[1, 5, 5, 3])
x2 = tf.constant (1.0, Shape=[1, 6, 6, 3])
ke Rnel = Tf.constant (1.0, Shape=[3, 3, 3, 1])
y1 = tf.nn.conv2d (x1, kernel, strides=[1, 2, 2, 1], padding= "SAME")
y2 = tf.nn.conv2d (x2, Kernel, strides=[1, 2, 2, 1], padding= "SAME")
sess = tf. Session ()
Tf.global_variables_initializer (). Run (session=sess)
X1_cov, x2_cov = Sess.run ([y1, y2])
print (X1_cov.shape)
print (X2_cov.shape)
Reverse Convolution
The inverse convolution function is:
Tf.nn.conv2d_transpose (value,
filter,
output_shape,
strides,
padding= "SAME",
data_format= " NHWC ",
Name=none)
Output_shape is the output shape, because it is the reverse process of the convolution, so the input output of the filter here is the dimension position Exchange, [height, width, output_channels, in_channels].
Import TensorFlow as tf
x1 = tf.constant (1.0, Shape=[1, 5, 5, 3])
x2 = tf.constant (1.0, Shape=[1, 6, 6, 3])
ke Rnel = Tf.constant (1.0, Shape=[3, 3, 3, 1])
y1 = tf.nn.conv2d (x1, kernel, strides=[1, 2, 2, 1], padding= "SAME")
y2 = tf.nn.conv2d (x2, Kernel, strides=[1, 2, 2, 1], padding= "SAME")
y3 = Tf.nn.conv2d_transpose (y1,kernel,output_shape =[1,5,5,3],
strides=[1,2,2,1],padding= "SAME")
Y4 = Tf.nn.conv2d_transpose (y2,kernel,output_shape=[ 1,6,6,3],
strides=[1,2,2,1],padding= "SAME")
sess = tf. Session ()
Tf.global_variables_initializer (). Run (session=sess)
X1_cov, X2_cov,y1_decov,y2_decov = Sess.run ([Y1, Y2,y3,y4]) print (x1_cov.shape) print (x2_cov.shape) print (
y1_decov.shape
) Print (Y2_decov.shape)
Note that Output_shape needs to be specified according to input shape,filter shape and output_dim, but not arbitrarily, for example
Y4 = Tf.nn.conv2d_transpose (y2,kernel,output_shape=[1,5,5,3],
strides=[1,2,2,1],padding= "SAME")
Get Y4 shape (1, 5, 5, 3), but if set output_shape=[1,7,7,3],
Y4 = Tf.nn.conv2d_transpose (y2,kernel,output_shape=[1,7,7,3],
strides=[1,2,2,1],padding= "SAME")
There is an error:
Invalidargumenterror (above for traceback): Conv2dslowbackpropinput:size of Out_backprop doesn ' t match computed:actu Al = 3, computed = 4
[[Node:conv2d_transpose_1 = Conv2dbackpropinput[t=dt_float, data_format= "NHWC", padding= "SAME", Strides=[1, 2, 2, 1], Use_cudnn_on_gpu=true, _device= "/job:localhost/replica:0/task:0/gpu:0"] (Conv2d_transpose_1/output_shape, Const_2 , Conv2d_1)]]
[[node:conv2d_transpose/_5 = _recv[client_terminated=false, recv_device= "/job:localhost/replica : 0/task:0/cpu:0 ", send_device="/job:localhost/replica:0/task:0/gpu:0 ", Send_device_incarnation=1, Tensor_name=" Edge_23_conv2d_transpose ", Tensor_type=dt_float, _device="/job:localhost/replica:0/task:0/cpu:0 "] [)]]
void convolution (dilated convolution):
The void convolution function is:
tf.nn.atrous_conv2d (value, filters, rate, padding, name=none)
The Fileter is a filter (convolution kernel), the same format as the convolution, for [height, width, Input_dim, output_dim].rate for the input sampling step (sample stride).
X1 = Tf.constant (1.0, Shape=[1, 5, 5, 3]) kernel = tf.constant (
1.0, Shape=[3, 3, 3, 1])
y5=tf.nn.atrous_conv2d (x1, kernel,10, ' SAME ')
Y5.shape is (1, 5, 5, 1).
The full calling code is:
import tensorflow as TF x1 = tf.constant (1.0, Shape=[1, 5, 5, 3]) x2 = tf.constant (1.0, Shape=[1 , 6, 6, 3]) kernel = tf.constant (1.0, Shape=[3, 3, 3, 1)) Y1 = tf.nn.conv2d (x1, kernel, strides=[1, 2, 2, 1], padding= "SAM E ") y2 = tf.nn.conv2d (x2, Kernel, strides=[1, 2, 2, 1], padding=" SAME ") Y3 = Tf.nn.conv2d_transpose (y1,kernel,output_shape =[1,5,5,3], strides=[1,2,2,1],padding= "SAME") Y4 = Tf.nn.conv2d_transpose (y2,kernel,output_shape=[1,6,6,3), Stride s=[1,2,2,1],padding= "SAME") y5=tf.nn.atrous_conv2d (x1,kernel,10, ' SAME ') Sess = tf. Session () Tf.global_variables_initializer (). Run (session=sess) X1_cov, X2_cov,y1_decov,y2_decov,y5_dicov = Sess.run (
[Y1, Y2,y3,y4,y5]) Print (x1_cov.shape) print (x2_cov.shape) print (y1_decov.shape) print (y2_decov.shape) print (Y5_dicov.shape)