How do I load a model in Python 3 that was trained and saved using Python 2?

while loading a model trained using python 2 in python 3, i get the error about encoding :

is there any workaround to this ?

it looks like you are saving a string as part of your model. See https://stackoverflow.com/questions/28218466/unpickling-a-python-2-object-with-python-3 for example.

Either dont save strings on your model, or instead of saving and loading the whole model, you can:

torch.save(filename, model.state_dict())

##
model = Model() # construct a new model
model.load_state_dict(torch.load(filename))

See https://github.com/pytorch/examples/tree/master/imagenet for an example.

4 Likes

this worked thank you.

I made a very rough script to try and do the Python 2 -> Python 3 conversion here:

YMMV