Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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. How can I load my model on Python 3.6?

like image 478
Rahul Anand Avatar asked Oct 19 '25 13:10

Rahul Anand


2 Answers

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. Also always ensure that the actual libraries (e.g. keras...) have the same version.

like image 80
Thomas Avatar answered Oct 21 '25 01:10

Thomas


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.

What I did is to reinstantiate the exact model from code, and then call 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:

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.

like image 39
kawingkelvin Avatar answered Oct 21 '25 02:10

kawingkelvin