我在Windows10、Python3.5、Keras2.0.6中训练并保存了一个Keras模型。在
在Windows中,我可以加载模型并重用它。但是,当我尝试在Linux(Ubuntu)Keras 2.0.5中加载模型时,我得到了以下错误:ValueError: Optimizer weight shape (90,) not compatible with provided weight shape (31, 90)
我尝试过卸载Keras并使用Pip重新安装,然后用Conda做同样的事情。这是与Windows和Linux的兼容性问题,还是其他问题?在
非常感谢
用于训练和保存模型的代码:from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K
def inRange(y_true, y_pred):
return K.sum(K.cast(K.less_equal(K.abs(y_true-y_pred), 8), "int32")) / K.shape(y_true)[0]
# create model
model = Sequential()
model.add(Dense(n1, input_dim=X_train.shape[1], activation='relu'))
model.add(Dense(n2, activation='relu'))
model.add(Dense(1, activation='linear'))
# Compile model
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy', inRange])
# Fit the model
history = model.fit(X_train, y_train, epochs=maxEpoch, batch_size=10)
# evaluate the model
scores = model.evaluate(X_train, y_train)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# save the model
model.save('length_predict.h5', overwrite=True, include_optimizer=True)
加载保存模型的代码:
^{pr2}$