PicklingError when using torch.save

when runing the code below
print(‘Saving best accuracy…’)
state = {
‘model’: model.modules,
‘best_acc’: test_best_acc,
‘epoch’: epoch
if not os.path.isdir(‘checkpoint’):
torch.save(state, ‘checkpoint/ckpt.t7’)

it happens

PicklingError Traceback (most recent call last)
in ()
64 for epoch in range(1, 300):
65 train_epoch(epoch, baseCNN, train_loader, rms_baseCNN, cross_entropy)
—> 66 vali_epoch(epoch, baseCNN, vali_loader, cross_entropy)

in vali_epoch(epoch, model, data_loader, criterion)
59 if not os.path.isdir(‘checkpoint’):
60 os.mkdir(‘checkpoint’)
—> 61 torch.save(state, ‘checkpoint/ckpt.t7’)

/usr/local/anaconda2/lib/python2.7/site-packages/torch/serialization.pyc in save(obj, f, pickle_module, pickle_protocol)
118 f = open(f, “wb”)
119 try:
–> 120 return _save(obj, f, pickle_module, pickle_protocol)
121 finally:
122 if new_fd:

/usr/local/anaconda2/lib/python2.7/site-packages/torch/serialization.pyc in _save(obj, f, pickle_module, pickle_protocol)
184 pickler = pickle_module.Pickler(f, protocol=pickle_protocol)
185 pickler.persistent_id = persistent_id
–> 186 pickler.dump(obj)
188 serialized_storage_keys = sorted(serialized_storages.keys())

PicklingError: Can’t pickle <type ‘instancemethod’>: attribute lookup builtin.instancemethod failed

So how can I solve this problem? Thanking very much


Please see the documentation for serialization: http://pytorch.org/docs/notes/serialization.html
In particular, I think you should be saving model.state_dict() and not model.modules.

1 Like

Thanking you, I solve the problem by following your advice

I have seen this recommendation many times, to save the state dict only, but what about the model instantiation then? How do you know which arguments you used to instantiate the model? Do you write custom code, just to capture those and store these separately?

In general I would say yes.
You can torch.save a tuple that contains both the arguments to re-create the model and the state dict.
This is a limitation of python not to be able to serialize complex objects in a convenient and reliable way :confused:

1 Like

Thanks for the quick answer.

Ok, I will try that. I suspect it also means that I need to call the constructor originally with a dict and keep the dict around, so that I can use it for save as well. Not a huge burden, but feels not all that elegant.

1 Like