重命名图层后,保存Keras模型失败

时间:2018-04-07 16:02:36

标签: keras

我在重命名图层时遇到问题。以下是说明问题的最简单示例:

from keras.layers import Dense, Conv2D, Flatten, Input
from keras.models import Model

inputs = Input(shape=(784,))

x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

print(model.summary())

for i, layer in enumerate(model.layers):
    layer.name = 'layer' + str(i)

print(model.summary())

model.save('temp')

它失败并显示以下消息:

     Traceback (most recent call last):
  File "scripts/save_load.py", line 24, in <module>
    model.save('temp')
  File "/lib/python3.6/site-packages/keras/engine/topology.py", line 2416, in save
    save_model(self, filepath, overwrite)
  File "/lib/python3.6/site-packages/keras/models.py", line 101, in save_model
    'config': model.get_config()
  File "/lib/python3.6/site-packages/keras/engine/topology.py", line 2281, in get_config
    new_node_index = node_conversion_map[node_key]
    KeyError: 'layer0_ib-0'

我做错了什么?

我知道我可以将名称传递给图层构造函数,在这种情况下似乎没有失败,但有没有机会改进我的解决方案?

0 个答案:

没有答案