Tensorflow 保存模型可以工作,但加载却不行

问题描述 投票:0回答:1

我在张量流中训练了一个 LSTM 模型,它工作得很好,但是当我保存模型然后尝试从磁盘加载它时,它在加载时抛出一个 ValueError 。仅供参考,保存和加载适用于其他模型,这里是创建模型的代码:

model1 = Sequential()
model1.add(int_vectorize_layer)
model1.add(Embedding(vocab_size, embedding_dim))
model1.add(SpatialDropout1D(drop_lstm))
model1.add(tf.keras.layers.Bidirectional(LSTM(units=32)))
model1.add(Dropout(drop_lstm))
model1.add(Dense(128, activation='relu'))
model1.add(Dense(64, activation='relu'))
model1.add(Dense(32, activation='relu'))
model1.add(Dense(1, activation='sigmoid'))
model1.compile(loss = 'binary_crossentropy',optimizer = 'adam',metrics = ['accuracy'])```

以下是模型的摘要: 型号总结

保存并加载模型:

#works fine
model1.save("model.keras")
#this line throws the error
loaded_model1 = tf.keras.models.load_model("model.keras")

错误:

ValueError: A total of 1 objects could not be loaded. Example error message for object <LSTMCell name=lstm_cell, built=True>:

Layer 'lstm_cell' expected 3 variables, but received 0 variables during loading. Expected: ['kernel', 'recurrent_kernel', 'bias']

List of objects that could not be loaded:
[<LSTMCell name=lstm_cell, built=True>]

堆栈跟踪完全错误:

ValueError                                Traceback (most recent call last)
Cell In[141], line 1
----> 1 loaded_model1 = tf.keras.models.load_model("model.keras")

File ~/Desktop/projects/SPARK-STREAMING-PFA/Model/my-env/lib/python3.10/site-packages/keras/src/saving/saving_api.py:176, in load_model(filepath, custom_objects, compile, safe_mode)
    173         is_keras_zip = True
    175 if is_keras_zip:
--> 176     return saving_lib.load_model(
    177         filepath,
    178         custom_objects=custom_objects,
    179         compile=compile,
    180         safe_mode=safe_mode,
    181     )
    182 if str(filepath).endswith((".h5", ".hdf5")):
    183     return legacy_h5_format.load_model_from_hdf5(filepath)

File ~/Desktop/projects/SPARK-STREAMING-PFA/Model/my-env/lib/python3.10/site-packages/keras/src/saving/saving_lib.py:152, in load_model(filepath, custom_objects, compile, safe_mode)
    147     raise ValueError(
    148         "Invalid filename: expected a `.keras` extension. "
    149         f"Received: filepath={filepath}"
    150     )
    151 with open(filepath, "rb") as f:
--> 152     return _load_model_from_fileobj(
    153         f, custom_objects, compile, safe_mode
    154     )

File ~/Desktop/projects/SPARK-STREAMING-PFA/Model/my-env/lib/python3.10/site-packages/keras/src/saving/saving_lib.py:207, in _load_model_from_fileobj(fileobj, custom_objects, compile, safe_mode)
    204         asset_store.close()
    206     if failed_trackables:
--> 207         _raise_loading_failure(error_msgs)
    208 return model

File ~/Desktop/projects/SPARK-STREAMING-PFA/Model/my-env/lib/python3.10/site-packages/keras/src/saving/saving_lib.py:295, in _raise_loading_failure(error_msgs, warn_only)
    293     warnings.warn(msg)
    294 else:
--> 295     raise ValueError(msg)

ValueError: A total of 1 objects could not be loaded. Example error message for object <LSTMCell name=lstm_cell, built=True>:

Layer 'lstm_cell' expected 3 variables, but received 0 variables during loading. Expected: ['kernel', 'recurrent_kernel', 'bias']

List of objects that could not be loaded:
[<LSTMCell name=lstm_cell, built=True>]

我在 python-3.10.12 上运行代码,这里是所有库版本:

absl-py==2.1.0
asttokens==2.4.1
astunparse==1.6.3
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
comm==0.2.2
contourpy==1.2.1
cycler==0.12.1
debugpy==1.8.1
decorator==5.1.1
exceptiongroup==1.2.1
executing==2.0.1
flatbuffers==24.3.25
fonttools==4.51.0
gast==0.5.4
gensim==4.3.2
google-pasta==0.2.0
grpcio==1.62.2
h5py==3.11.0
idna==3.7
ipykernel==6.29.4
ipython==8.23.0
jedi==0.19.1
joblib==1.4.0
jupyter_client==8.6.1
jupyter_core==5.7.2
keras==3.2.1
kiwisolver==1.4.5
libclang==18.1.1
Markdown==3.6
markdown-it-py==3.0.0
MarkupSafe==2.1.5
matplotlib==3.8.4
matplotlib-inline==0.1.7
mdurl==0.1.2
ml-dtypes==0.3.2
namex==0.0.8
nest-asyncio==1.6.0
nltk==3.8.1
numpy==1.26.4
opt-einsum==3.3.0
optree==0.11.0
packaging==24.0
pandas==2.2.2
parso==0.8.4
pexpect==4.9.0
pillow==10.3.0
platformdirs==4.2.0
prompt-toolkit==3.0.43
protobuf==4.25.3
psutil==5.9.8
ptyprocess==0.7.0
pure-eval==0.2.2
Pygments==2.17.2
pyparsing==3.1.2
python-dateutil==2.9.0.post0
pytz==2024.1
pyzmq==26.0.2
regex==2024.4.16
requests==2.31.0
rich==13.7.1
scikit-learn==1.4.2
scipy==1.13.0
seaborn==0.13.2
six==1.16.0
smart-open==7.0.4
stack-data==0.6.3
tensorboard==2.16.2
tensorboard-data-server==0.7.2
tensorflow==2.16.1
tensorflow-io-gcs-filesystem==0.36.0
termcolor==2.4.0
threadpoolctl==3.4.0
tornado==6.4
tqdm==4.66.2
traitlets==5.14.3
tweet-preprocessor==0.6.0
typing_extensions==4.11.0
tzdata==2024.1
urllib3==2.2.1
wcwidth==0.2.13
Werkzeug==3.0.2
wordcloud==1.9.3
wrapt==1.16.0

我尝试过以其他方式保存模型,我尝试过将其保存为 h5 格式,但它给了我一个已弃用的警告和错误,我尝试过保存权重并将它们重新加载到具有相同架构的模型中,但随后 textVectorization 层需要适应训练数据或从另一个文件加载,这不是理想的情况,我认为这是使该代码工作的最佳解决方案,以便我可以轻松地从单个文件加载模型。我认为双向层是问题的根源,因为这是神经网络中仅有的两个 LSTM 层存在的地方。如果您对如何解决问题有任何想法,我愿意尝试一下。非常感谢任何和所有的帮助!谢谢!

python-3.x tensorflow machine-learning keras recurrent-neural-network
1个回答
0
投票

我解决了问题,事实证明我是对的,问题出在双向层,对我来说解决问题的就是删除它,这是具有相同精度、结构的新模型:

input_layer = Input(shape=(1,),dtype=tf.string)
x = int_vectorize_layer(input_layer)
x = Embedding(vocab_size, embedding_dim)(x)
x = SpatialDropout1D(drop_lstm)(x)

#Changed Bidirectionnal to this
forward_lstm = LSTM(units=32, return_sequences=False, name='forward_lstm')(x)
backward_lstm = LSTM(units=32, return_sequences=False, go_backwards=True, name='backward_lstm')(x)
concat_lstm = Concatenate()([forward_lstm, backward_lstm])

x = Dropout(drop_lstm)(concat_lstm)
x = Dense(128, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(32, activation='relu')(x)
output_layer = Dense(1, activation='sigmoid')(x)
model1 = Model(inputs=input_layer, outputs=output_layer)
model1.compile(loss='binary_crossentropy',
               optimizer='adam',
               metrics=['accuracy'])

现在我可以毫无问题地保存和加载模型了。

© www.soinside.com 2019 - 2024. All rights reserved.