我正在尝试在 TensorFlow 中构建卷积层。实际上它是为了回答下面的一个测验。他们也给了我们实现 new_height 和 new_width 的公式。但我不知道填充的数字是多少,因为我认为 padding='SAME' 或 padding='VALID' 并且它不包含数字。
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)
请您参考如下方法:
SAME 填充意味着输出特征图的大小与输入特征图的大小相同(在 stride=1 的假设下)。例如,如果输入的 Nin 大小为 28×28,那么在输出中您也希望得到 Nout 个大小为 28×28 的特征图.
另一方面,VALID 填充意味着输出特征图的大小减小了。例如,如果输入 Nin 的大小为 28x28,stride=1 且过滤器大小为 3x3,则输出 Nout 特征图将是 [ {(28-3)/1}+1 = 26] 26x26。
要推导出输出,公式为:{(Input 的大小 - Filter 的大小)/stride} + 1
因此从上面的公式可以看出,输出的feature-map的大小与stride成反比(stride值越大,输出的feature map越小)
