Keras更改保存模型的图层名称并断开ValueError Graph

g52tjvyc  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(160)

我想更改已训练和保存的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]

模型摘要看起来很好,但仍然得到错误。我错过什么了吗?

sbdsn5lh

sbdsn5lh1#

请在更改模型层的现有名称期间将.name修改为._name,然后重试。像这样:

if layer.name == 'user_embedding_123':
    layer._name = 'user_embedding'

请检查以下代码作为示例:

model = load_model('my_model.hdf5')
model.summary()

输出:

Model: "sequential_24"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 Dense1_Layer (Dense)        (None, 512)               401920    
                                                                 
 Dense2_Layer (Dense)        (None, 256)               131328    
                                                                 
 Dense3_Layer (Dense)        (None, 128)               32896     
                                                                 
 Dropout_Layer (Dropout)     (None, 128)               0         
                                                                 
 Dense4_Layer (Dense)        (None, 10)                1290      
                                                                 
=================================================================
Total params: 567,434
Trainable params: 567,434
Non-trainable params: 0
_________________________________________________________________

要更改特定图层的名称,请执行以下操作:

for layer in model.layers:
    if layer.name == 'Dense1_Layer':
        layer._name = 'newDense11'
    if layer.name == 'Dense2_Layer':
        layer._name = 'newDense22'

model.summary()

输出:

Model: "sequential_24"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 newDense11 (Dense)          (None, 512)               401920    
                                                                 
 newDense22 (Dense)          (None, 256)               131328    
                                                                 
 Dense3_Layer (Dense)        (None, 128)               32896     
                                                                 
 Dropout_Layer (Dropout)     (None, 128)               0         
                                                                 
 Dense4_Layer (Dense)        (None, 10)                1290      
                                                                 
=================================================================
Total params: 567,434
Trainable params: 567,434
Non-trainable params: 0
_________________________________________________________________

保存并重新加载模型以检查更改

save_model(model, "final_model_renamed.hdf5")
model_renamed = load_model('final_model_renamed.hdf5')

model_renamed.summary()

输出:

Model: "sequential_24"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 newDense11 (Dense)          (None, 512)               401920    
                                                                 
 newDense22 (Dense)          (None, 256)               131328    
                                                                 
 Dense3_Layer (Dense)        (None, 128)               32896     
                                                                 
 Dropout_Layer (Dropout)     (None, 128)               0         
                                                                 
 Dense4_Layer (Dense)        (None, 10)                1290      
                                                                 
=================================================================
Total params: 567,434
Trainable params: 567,434
Non-trainable params: 0
_________________________________________________________________

相关问题