Reputation: 65
I'm trying to build the convolutional layers in TensorFlow. Actually it is for answering a quiz below. They give us the formula to achieve new_height and new_width too. But I don't know what is the number to put for padding because I thought padding='SAME' or padding='VALID' and it didn't contain number.
new_height = (input_height - filter_height + 2 * P)/S + 1
new_width = (input_width - filter_width + 2 * P)/S + 1
# `tf.nn.conv2d` requires the input be 4D (batch_size, height, width, depth)
# (1, 4, 4, 1)
x = np.array([
[0, 1, 0.5, 10],
[2, 2.5, 1, -8],
[4, 0, 5, 6],
[15, 1, 2, 3]], dtype=np.float32).reshape((1, 4, 4, 1))
X = tf.constant(x)
def conv2d(input):
# Filter (weights and bias)
# The shape of the filter weight is (height, width, input_depth,
output_depth)
# The shape of the filter bias is (output_depth,)
# TODO: Define the filter weights `F_W` and filter bias `F_b`.
# NOTE: Remember to wrap them in `tf.Variable`, they are trainable
parameters after all.
F_W = ????
F_b = ????
# TODO: Set the stride for each dimension (batch_size, height, width, depth)
strides = ????
# TODO: set the padding, either 'VALID' or 'SAME'.
padding = ?
# `tf.nn.conv2d` does not include the bias computation so we have to add it
ourselves after.
return tf.nn.conv2d(input, F_W, strides, padding) + F_b
out = conv2d(X)
Upvotes: 2
Views: 112
Reputation: 86
SAME padding means the size of output feature-maps are the same as the input feature-maps (under the assumption of stride=1). For instance, if the input is Nin is of size 28×28, then in the output you expect to get Nout feature maps each of size 28×28 as well.
On the other hand, VALID padding means the size of the output feature-maps is reduced. For instance, if the input is Nin is of size 28x28, stride=1 and filter size is 3x3, then the output Nout feature-maps would be each of [{(28-3)/1}+1 = 26] 26x26.
To deduce the output the formula is: {(size of Input - size of Filter)/stride} + 1
hence from the above formula, you can see that the size of the output feature-map is inversely proportional to the stride (Higher the stride value, smaller will be the output feature map)
Upvotes: 2