[英]How to load machine learning model in python 3.6 that is trained on python 3.5?
I have trained a machine learning model on Python 3.5
, and now I switched to Google Colab, which uses Python 3.6
, and when I try to load the model that I have trained on Python 3.5
, it gives this error:我已经在 Python
3.5
上训练了一个机器学习模型,现在我切换到使用 Python 3.6
Google Colab,当我尝试加载我在 Python 3.5
上训练过的模型时,它给出了这个错误:
SystemError: unknown opcode.
After googling, I found that this error occurs because of the environment change, then I cross-checked my python version, and both Python version were different. google了之后发现这个错误是因为环境变化导致的,于是我交叉检查了我的python版本,发现两个python版本都不一样。 How can I load my model on Python
3.6
?如何在 Python
3.6
上加载我的模型?
You shouldn't.你不应该。
Even if you get it to run without errors / warnings, there might be slight changes under the hood that change the behavior / performance of the model.即使您让它在没有错误/警告的情况下运行,引擎盖下也可能会有细微的变化,从而改变模型的行为/性能。
You should either retrain the model on Python 3.6 or create a virtual environment that runs Python 3.5 for your model to ensure it performs as expected.您应该在 Python 3.6 上重新训练模型,或者为您的模型创建一个运行 Python 3.5 的虚拟环境,以确保它按预期执行。 Also always ensure that the actual libraries (eg keras...) have the same version.
还要始终确保实际库(例如 keras...)具有相同的版本。
I ran into same problem as you did.我遇到了和你一样的问题。 I trained my model on a GCP on python 3.5 and move it to colab to continue with evaluation which is python 3.6.
我在 python 3.5 上的 GCP 上训练了我的模型,并将其移动到 colab 以继续进行 python 3.6 的评估。
What I did is to reinstantiate the exact model from code, and then call load_weights:我所做的是从代码中重新实例化确切的模型,然后调用 load_weights:
model = create_my_model()
model.load_weights('my_model_trained_with_py_35.h5')
model.save('my_model_py36.h5')
For my case, I don't have lot of custom code other than a Lambda with:就我而言,除了具有以下功能的 Lambda 之外,我没有很多自定义代码:
def abs_diff(x):
return tf.abs(x[0] - x[1])
Since your model could be arbitrarily more complex, this may or may not work, but it is worth a try esp.由于您的模型可能更加复杂,这可能有效也可能无效,但值得一试,尤其是。 if re-training is too expensive.
如果再培训太贵了。 As usual, evaluate the model with the same data and ensure nothing is strange.
像往常一样,使用相同的数据评估模型并确保没有任何异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.