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


(Chahrazad Essalim) #1

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

is there any workaround to this ?


#2

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.


(Chahrazad Essalim) #3

this worked thank you.