【发布时间】:2020-07-19 21:19:59
【问题描述】:
我正在尝试使用标准 Keras 验证我的网络构建的第一层的输出。第一层的名字是conv2d。
我构建了一个新模型只是为了得到第一层的输出,使用以下代码:
inter_layer = None
weights = None
biases = None
for layer in qmodel.layers:
if layer.name == "conv2d":
print("Found layer: " + layer.name)
inter_layer = layer
weights = layer.get_weights()[0]
biases = layer.get_weights()[1]
inter_model = Model(qmodel.input,inter_layer.output)
inter_model.compile()
然后,我做了以下事情(img_test 是 cifar10 图像之一):
first_layer_output = inter_model.predict(img_test)
# Get the 3x3 pixel upper left patch of the 3 channels of the input image
img_test_slice = img_test[0,:3,:3,:]
# Get only the first filter of the layer
weigths_slice = weights[:,:,:,0]
# Get the bias of the first filter of the layer
bias_slice = biases[0]
# Get the 3x3 pixel upper left patch of the first channel of the output of the layer
output_slice = first_layer_output[0,:3,:3,0]
我打印了每个切片的形状,得到了正确的形状:
- img_test_slice: (3,3,3)
- weigths_slice: (3,3,3)
- 输出切片:(3,3)
据我所知,如果我这样做:
partial_sum = np.multiply(img_test_slice,weigths_slice)
output_pixel = partial_sum.sum() + bias_slice
output_pixel 应该是 output_slice 的值之一(实际上是索引 [1,1] 中的值,因为该层具有 padding = 'SAME')。
但是....不是。
也许我遗漏了一些关于卷积计算如何工作的非常简单的东西,但据我所知,进行元素乘法然后对所有值求和加上偏差应该是输出像素之一层。
也许层的输出数据的排列方式与层的输入不同?
【问题讨论】:
标签: python numpy tensorflow keras convolution