我试图实现conv2d(供我学习)。好吧,我写道:
def conv(ix, w):
# filter shape: [filter_height, filter_width, in_channels, out_channels]
# flatten filters
filter_height = int(w.shape[0])
filter_width = int(w.shape[1])
in_channels = int(w.shape[2])
out_channels = int(w.shape[3])
ix_height = int(ix.shape[1])
ix_width = int(ix.shape[2])
ix_channels = int(ix.shape[3])
filter_shape = [filter_height, filter_width, in_channels, out_channels]
flat_w = tf.reshape(w, [filter_height * filter_width * in_channels, out_channels])
patches = tf.extract_image_patches(
ix,
ksizes=[1, filter_height, filter_width, 1],
strides=[1, 1, 1, 1],
rates=[1, 1, 1, 1],
padding='SAME'
)
patches_reshaped = tf.reshape(patches, [-1, ix_height, ix_width, filter_height * filter_width * ix_channels])
feature_maps = []
for i in range(out_channels):
feature_map = tf.reduce_sum(tf.multiply(flat_w[:, i], patches_reshaped), axis=3, keep_dims=True)
feature_maps.append(feature_map)
features = tf.concat(feature_maps, axis=3)
return features
希望我做得正确。经MNIST检查,结果非常接近(但此实现速度较慢)。我希望这可以帮助你。
0
我在这里查看有关
tf.nn.conv2d
文档。但是我不明白它的作用或试图达到的目的。它在文档上说,现在那是做什么的?是逐元素乘法还是仅矩阵乘法?我也无法理解文档中提到的其他两点。我在下面写了它们:
如果有人可以举一个例子,也许有一段代码(极其有用)并解释那里发生了什么以及为什么这样的操作,那将真的很有帮助。
我尝试编码一小部分并打印出操作的形状。不过,我还是不明白。
我尝试过这样的事情:
我了解卷积神经网络的点点滴滴。我在这里学习过 。但是在tensorflow上的实现不是我期望的。因此它提出了一个问题。
编辑 :因此,我实现了一个简单得多的代码。但是我不知道发生了什么。我的意思是结果是这样的。如果有人能告诉我是什么过程产生此输出的,那将非常有帮助。
输出