Python Keras-Windows和Linux之间的兼容性

Python Keras-Windows和Linux之间的兼容性,python,keras,Python,Keras,我在Windows10、Python3.5和Keras2.0.6中训练并保存了一个Keras模型 在Windows中,我可以加载模型并重用它。但是,当我尝试在Linux(Ubuntu)Keras 2.0.5中加载该模型时,出现以下错误: ValueError:优化器权重形状(90,)与提供的权重形状(31,90)不兼容 我尝试过卸载Keras并使用Pip重新安装,然后使用Conda执行同样的操作。这是Windows和Linux的兼容性问题还是其他问题 非常感谢 用于训练和保存模型的代码: fro

我在Windows10、Python3.5和Keras2.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页面),代码随后生效:)

请提供您的model@Paddy我添加了一些代码片段。感谢您再次检查Windows/Linux上的keras版本。必须完全相同。