【问题标题】:Keras replacing input of networkKeras 替换网络的输入
【发布时间】:2018-09-18 19:23:38
【问题描述】:

我有与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].

有什么办法吗?

【问题讨论】:

    标签: keras


    【解决方案1】:

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

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

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

    【讨论】:

    • 我最终重建了整个网络,这不是很方便,但是很有效。
    猜你喜欢
    • 2018-09-07
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2021-08-13
    • 2016-12-04
    • 2021-09-08
    • 2019-09-09
    • 2021-04-19
    相关资源
    最近更新 更多