![](/img/trans.png)
[英]How to enforce Windows and Linux compatibility in my open source python project?
[英]Python Keras - compatibility between Windows and Linux
我已经在Windows 10,Python 3.5,Keras 2.0.6中训练并保存了Keras模型。
然后,在Windows中,我可以加载模型并重用它。 但是,当我尝试在Linux(Ubuntu)Keras 2.0.5中加载模型时,出现以下错误:
ValueError:优化程序权重形状(90,)与提供的权重形状(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)
加载保存的模型的代码:
import keras.backend as K
from keras.models import load_model
# Custom metric for use in the keras ANN models, needs to be loaded as a custom object
def inRange(y_true, y_pred):
'''
Function for determining the percentage of points that fall within the +-8% error
'''
return K.sum(K.cast(K.less_equal(K.abs(y_true-y_pred), 8), "int32")) / K.shape(y_true)[0]
# Load the ANN
model_length = load_model('length_predict.h5', custom_objects={'inRange':inRange})
谢谢,实际上这是版本不兼容的问题。
由于某种原因,Anaconda在Windows中安装版本2.0.6,但在Linux中仅安装2.0.5。 我在Linux机器上手动下载并安装了2.0.6(从Keras github页面),然后代码起作用了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.