我想更改已训练和保存的Keras(2.3.1)模型的层名称,如下所示:user_embedding_123和item_embedding_284是模型仅有的两个输入层。
from keras.models import load_model, save_model
from keras.layers import Input, Dense
from keras.models import Model
model = load_model('final_model.hdf5')
for layer in model.layers:
if layer.name == 'user_embedding_123':
layer.name = 'user_embedding'
if layer.name == 'item_embedding_284':
layer.name = 'item_embedding'
save_model(model, "final_model_renamed.hdf5")
model_renamed = load_model('final_model_renamed.hdf5') # Get an error
但是我得到一个错误:
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("user_embedding:0", shape=(?, 768), dtype=float32) at layer "user_embedding". The following previous layers were accessed without issue: []
模型总结如下:
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
user_embedding_123 (Input (None, 768) 0
__________________________________________________________________________________________________
item_embedding_284 (Input (None, 768) 0
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 1536) 0 user_embedding_123[0][0]
item_embedding_284[0][0
__________________________________________________________________________________________________
hidden_0 (Dense) (None, 256) 393472 concatenate_1[0][0]
......
现在就像是:
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
user_embedding (InputLayer) (None, 768) 0
__________________________________________________________________________________________________
item_embedding (InputLayer (None, 768) 0
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 1536) 0 user_embedding[0][0]
item_embedding[0][0]
__________________________________________________________________________________________________
hidden_0 (Dense) (None, 256) 393472 concatenate_1[0][0]
模型摘要看起来很好,但仍然得到错误。我错过什么了吗?
1条答案
按热度按时间sbdsn5lh1#
请在更改模型层的现有名称期间将
.name
修改为._name
,然后重试。像这样:请检查以下代码作为示例:
输出:
要更改特定图层的名称,请执行以下操作:
输出:
保存并重新加载模型以检查更改
输出: