我有以下矩阵:
和以下内核:
如果我在没有填充的情况下进行卷积并滑动 1 行,我应该得到以下答案:
因为:
基于 的文档tf.nn.conv2d
,我认为这段代码表达了我刚才描述的内容:
import tensorflow as tf
input_batch = tf.constant([
[
[[.0], [1.0]],
[[2.], [3.]]
]
])
kernel = tf.constant([
[
[[1.0, 2.0]]
]
])
conv2d = tf.nn.conv2d(input_batch, kernel, strides=[1, 1, 1, 1], padding='VALID')
sess = tf.Session()
print(sess.run(conv2d))
但它产生这样的输出:
[[[[ 0. 0.]
[ 1. 2.]]
[[ 2. 4.]
[ 3. 6.]]]]
我不知道它是如何计算的。我已经尝试为 strides padding 参数尝试不同的值,但仍然无法产生我预期的结果。
您没有正确阅读我在链接的教程中的解释。在对no-padding, strides=1
您进行直接修改后,您将获得以下代码。
import tensorflow as tf
k = tf.constant([
[1, 2],
], dtype=tf.float32, name='k')
i = tf.constant([
[0, 1],
[2, 3],
], dtype=tf.float32, name='i')
kernel = tf.reshape(k, [1, 2, 1, 1], name='kernel')
image = tf.reshape(i, [1, 2, 2, 1], name='image')
res = tf.squeeze(tf.nn.conv2d(image, kernel, [1, 1, 1, 1], "VALID"))
# VALID means no padding
with tf.Session() as sess:
print sess.run(res)
这为您提供了预期的结果:[2., 8.]
. 在这里,由于挤压运算符,我得到了一个向量而不是列。
我在您的代码中看到的一个问题(可能还有其他问题)是您的内核的形状为(1, 1, 1, 2)
,但它假定为(1, 2, 1, 1)
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句