Keras取代网络输入

时间:2018-04-09 11:47:14

标签: keras

我遇到与Keras replacing input layer类似的问题,但我需要删除下一层,这需要不同的输入形状。

以下是我尝试做的简化:

a = Input(shape=(64,))
b = Dense(32)(a)
c = Dense(16)(b)
d = Dense(8)(c)
model = Model(inputs=a, outputs=d)
print(model.summary())
print('input shape = ' + str(model.input_shape))

model.layers.pop(0)
model.layers.pop(0)
print(model.summary())
print('input shape = ' + str(model.input_shape))

new_input = Input(shape=(32,))
new_output = model(new_input)
new_model = Model(new_input, new_output)
print(new_model.summary())

但模型的输入形状保持不变:

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                2080      
_________________________________________________________________
dense_2 (Dense)              (None, 16)                528       
_________________________________________________________________
dense_3 (Dense)              (None, 8)                 136       
=================================================================
Total params: 2,744
Trainable params: 2,744
Non-trainable params: 0
_________________________________________________________________
None
input shape = (None, 64)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense)              (None, 16)                528       
_________________________________________________________________
dense_3 (Dense)              (None, 8)                 136       
=================================================================
Total params: 664
Trainable params: 664
Non-trainable params: 0
_________________________________________________________________
None
input shape = (None, 64)

这阻止我创建新模型,因此上面的代码失败了:

ValueError: Dimensions must be equal, but are 32 and 64 for 'model_1/dense_1/MatMul' (op: 'MatMul') with input shapes: [?,32], [64,32].

任何想法如何做到这一点?

1 个答案:

答案 0 :(得分:1)

可能无法按照您描述的方式进行操作。这篇文章中接受的答案解释了一点。

how-to-change-input-shape-in-sequential-model-in-keras?

他们的解决方案是使用正确的输入形状重建图层,然后为该特定图层加载预先训练的权重。