我想可视化胶囊网络中的图层。为此,我需要中间层的形状。代码如下:
from keras.models import Model
model = Sequential()
conv1 = model.add(Conv2D(256, (9, 9), padding='valid', strides = 1, input_shape = (28, 28, 1), activation = 'relu', name = 'conv1'))
model.add(Conv2D(256, (9,9), padding='valid', strides = 2, name = 'primarycaps_conv2d'))
model.add(keras.layers.core.Reshape([-1,8]))
model.add(keras.layers.core.Lambda(squash, name = 'hello'))
layer_name = 'conv1'
intermediate_layer_model = Model(inputs=model.input,
outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)
引发以下错误:
NameError: name 'data' is not defined
如何克服这个问题?
答案 0 :(得分:0)
引用this,我得到了如下解决方案:
model = Sequential()
conv1 = model.add(Conv2D(256, (9, 9), padding='valid', strides = 1, input_shape = (28, 28, 1), activation = 'relu', name = 'conv1'))
model.add(Conv2D(256, (9,9), padding='valid', strides = 2, name = 'primarycaps_conv2d'))
model.add(keras.layers.core.Reshape([-1,8]))
model.add(keras.layers.core.Lambda(squash, name = 'hello'))
idx = 3 # index of desired layer
input_shape = model.layers[idx].get_output_shape_at(0)
print(input_shape)
获得如下的输出形状:
(None, 1152, 8).
解决了获取特定图层输出的问题。