python - 如何修复保存的模型以避免“ValueError:您正在尝试将包含 3 层的权重文件加载到具有 0 层的模型中。”
问题描述
我知道已经问过这个问题(例如这里),但没有提供答案。这是我的小模型:
import keras
from keras.models import Sequential
from keras.layers import Dense
IMGSIZE = 128
model = Sequential()
# Input/Encoder
model.add(Dense(IMGSIZE, activation='relu'))
# Code layer
model.add(Dense(50, activation='relu'))
#Decoder
model.add(Dense(IMGSIZE, activation='relu'))
model.compile(optimizer='adadelta', loss='binary_crossentropy')
history = model.fit(X, X, batch_size=50, epochs=1, verbose=1)
model.save("autoencodermodel.h5")
然后我用大量的 epoch 训练了几天的模型,我不想再这样做了。
所以今天,重新启动后(模型不再在内存中),我尝试了这个:
from keras.models import load_model
model = load_model("autoencodermodel.h5")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-26-1393db34e61d> in <module>()
1 from keras.models import load_model
----> 2 model = load_model("autoencodermodel.h5")
/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile)
262
263 # set weights
--> 264 load_weights_from_hdf5_group(f['model_weights'], model.layers)
265
266 if compile:
/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py in load_weights_from_hdf5_group(f, layers, reshape)
899 'containing ' + str(len(layer_names)) +
900 ' layers into a model with ' +
--> 901 str(len(filtered_layers)) + ' layers.')
902
903 # We batch weight value assignments in a single backend call
ValueError: You are trying to load a weight file containing 3 layers into a model with 0 layers.
所以现在,我可以再做一次,但要指定输入 shape。但我不想,也没有时间再训练它。我使用 tensorflow 后端使用相同版本的 Keras 在同一台计算机上保存并(尝试)加载模型。
有没有办法破解 .h5 文件以使 load_model 函数工作?
下一次,我将使用一个Input
层来避免这个问题(错误?)。
解决方案
推荐阅读
- ubuntu - init.d 因 tomcat 而失败
- javascript - 是否有通过 landsat 开发 NDVI 30 年数据的代码?
- wordpress - Wordpress 功能仅与用户操作员一起运行
- javascript - Heroku 有 js 和 css 的问题
- c++ - 可以在不构建代码的情况下进行定义吗?
- ruby-on-rails - Rails 中的 API,信息不显示,不确定如何获取数据
- f# - 在 F# 中根据条件从字典中删除元素
- python - 循环获取行编辑的值;代码错误
- android - Android REST API 调用,后端没有响应新记录
- eclipse - eclipse svn插件更改