alex alex - 3 months ago 73
Python Question

Implementing CNN with tensorflow

I'm new in convolutional neural networks and in Tensorflow and I need to implement a conv layer with further parameters:

Conv. layer1: filter=11, channel=64, stride=4, Relu.

The API is following:

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)

I understand, what is stride and that it should be [1, 4, 4, 1] in my case. But I do not understand, how should I pass a filter parameter and padding.
Could someone help with it?


At first, you need to create a filter variable:

W = tf.truncated_normal(shape = [11, 11, 3, 64], stddev = 0.1)

First two fields of shape parameter stand for filter size, third for the number of input channels (I guess your images have 3 channels) and fourth for the number of output channels.

Now output of convolutional layer could be computed as follows:

conv1 = tf.nn.conv2d(input, W, strides = [1, 4, 4, 1], padding = 'SAME'), where padding = 'SAME' stands for zero padding and therefore size of the image remains the same, input should have size [batch, size1, size2, 3].

ReLU application is pretty straightforward:

conv1 = tf.nn.relu(conv1)