使用 Keras (1.2.2),我正在加载一个顺序模型,其最后一层是:
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
然后,我想弹出最后一层,再添加一个全连接层,重新添加分类层。
model = load_model('model1.h5')
layer1 = model.layers.pop() # Copy activation_6 layer
layer2 = model.layers.pop() # Copy classification layer (dense_2)
model.add(Dense(512, name='dense_3'))
model.add(Activation('softmax', name='activation_7'))
model.add(layer2)
model.add(layer1)
print(model.summary())
如您所见,我的dense_3 和activation_7 没有连接到网络(summary() 中的空值带有“已连接到”)。我在文档中找不到任何解释如何解决此问题的内容。有任何想法吗?
dense_1 (Dense) (None, 512) 131584 flatten_1[0][0]
____________________________________________________________________________________________________
activation_5 (Activation) (None, 512) 0 dense_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 512) 5632
____________________________________________________________________________________________________
activation_7 (Activation) (None, 512) 0
____________________________________________________________________________________________________
dense_2 (Dense) (None, 10) 5130 activation_5[0][0]
____________________________________________________________________________________________________
activation_6 (Activation) (None, 10) 0 dense_2[0][0]
====================================================================================================
按照下面的答案,我在打印出来之前编译了模型model.summary()
,但由于某些原因,图层没有正确弹出,如摘要所示: 最后一层的连接错误:
dense_1 (Dense) (None, 512) 131584 flatten_1[0][0]
____________________________________________________________________________________________________
activation_5 (Activation) (None, 512) 0 dense_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 512) 5632 activation_6[0][0]
____________________________________________________________________________________________________
activation_7 (Activation) (None, 512) 0 dense_3[0][0]
____________________________________________________________________________________________________
dense_2 (Dense) (None, 10) 5130 activation_5[0][0]
activation_7[0][0]
____________________________________________________________________________________________________
activation_6 (Activation) (None, 10) 0 dense_2[0][0]
dense_2[1][0]
====================================================================================================
但应该是
dense_1 (Dense) (None, 512) 131584 flatten_1[0][0]
____________________________________________________________________________________________________
activation_5 (Activation) (None, 512) 0 dense_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 512) 5632 activation_5[0][0]
____________________________________________________________________________________________________
activation_7 (Activation) (None, 512) 0 dense_3[0][0]
____________________________________________________________________________________________________
dense_2 (Dense) (None, 10) 5130
activation_7[0][0]
____________________________________________________________________________________________________
activation_6 (Activation) (None, 10) 0 dense_2[0][0]
====================================================================================================