如何让 tensorflow 对具有 1 x 2 内核的 2 x 2 矩阵进行卷积?
How to get tensorflow to do a convolution on a 2 x 2 matrix with a 1 x 2 kernel?
我有以下矩阵:

和以下内核:

如果我做一个没有填充的卷积并滑动 1 行,我应该得到以下答案:

因为:
&space;+&space;(1%5Ctimes&space;2))
&space;+&space;(3&space;%5Ctimes&space;2))
根据 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)
.
我有以下矩阵:
和以下内核:
如果我做一个没有填充的卷积并滑动 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)
.