2017-08-10 137 views
1

我已经在Windows 10,Python 3.5,Keras 2.0.6中培训并保存了Keras模型。Python Keras - Windows和Linux之间的兼容性

在Windows中,我可以加载模型并重用它。但是,当我尝试在Linux操作系统(Ubuntu的),Keras 2.0.5,加载模型我得到以下错误:

ValueError: Optimizer weight shape (90,) not compatible with provided weight shape (31, 90)

我试图卸载Keras和使用皮普重新安装,然后做同样的与康达。这是与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}) 
+0

请提供您的模型的一些代码 – Paddy

+1

@Paddy我已经添加了一些代码片段。谢谢 – jlt199

+0

建议仔细检查Windows/Linux上的keras版本。需要完全相同。 –

回答

0

谢谢你,这实际上是一个版本不兼容问题。

由于某些原因,Anaconda在Windows中安装了2.0.6版本,但在Linux中只安装了2.0.5版本。我在我的Linux机器上手动下载并安装了2.0.6(从Keras github页面),然后代码工作:)