我知道已经提出了这个问题(例如here),但是没有提供答案。 这是我的小模型:
/var/lib/docker/containers/*/*.log
然后,我训练了模型了几天,经历了很多次,而且我不想再做一次。
因此,今天,重新启动后(内存中不再存在模型),我尝试了此操作:
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")
现在,我可以再次执行此操作,但只需指定输入形状即可。但是我不想,也没有时间再训练。 我使用tensorflow后端将模型保存并(尝试)在同一台计算机上使用相同版本的Keras加载。
下次,我将使用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.
层来避免此问题(错误?)。