我正在尝试在TensorFlow中构建卷积层。实际上,这是为了回答以下测验。他们给了我们实现new_height和new_width的公式。但是我不知道要填充的数字是多少,因为我认为padding ='SAME'或padding ='VALID'并且其中不包含数字。

new_height =(input_height-filter_height + 2 * P)/ S +1

new_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)

最佳答案

相同的填充意味着输出特征图的大小与输入特征图的大小相同(在stride = 1的假设下)。例如,如果输入为Nin,则尺寸为28×28,那么在输出中,您还希望获得Nout要素贴图,每个尺寸为28×28。

另一方面,有效填充意味着减小了输出特征图的大小。例如,如果输入Nin的大小为28x28,步幅= 1,过滤器大小为3x3,则输出Nout特征图将分别为[{(28-3)/ 1} +1 = 26] 26x26。

要推导输出,公式为:{(size of Input - size of Filter)/stride} + 1

因此,从上面的公式中,您可以看到输出要素图的大小与步幅成反比(步幅值越高,输出要素图越小)

关于python - 填充中是否包含用于new_height和new_width的计算公式的数字?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48192342/

10-12 22:50