New attributes in torch.Parameter are thrown away when dumping with pickle

sorry, can’t find the correct topic for issue described.

please see attached image

what would be the proper way to store new attributes with torch.parameter? Let me be specific, i need to create new attribute to pytorch parameters, and store into pickle.


import torch 
import pickle 
test_param = torch.nn.Parameter(torch.rand(2, 2))
test_param._new_param = 10 
print(f'{test_param._new_param = }') # this prints 'test_param.new_param = 10'
pickle.dump(test_param, open('test.pickle', 'wb')) 
new_param = pickle.load(open('test.pickle', 'rb'))
print(f'{hasattr(new_param, "_new_param") = }') # this prints 'hasattr(new_param, "new_param") = False'
print(f'{new_param._new_param = }') # this bugs with attribute error
# what would be the correct way to store new attributes into torch.Parameter 
# and be able to access it when loading from pickle file?

You should paste a fully working code example.

please elaborate, the code above is able to reproduce the issue. what would qualify as ‘working code’? this bug would break the code, therefore there is no ‘working code’.

Hi Michael!

At the risk of putting words into @iordanis’s mouth, perhaps he means
that you shouldn’t post images of textual information, as doing so breaks
copy-paste (as well as accessibility and searchability).

If I were to try running your code (working or not), I would soon discover
that pasting an image into a python interpreter does me no good.


K. Frank

Thank you, I have put the code required to reproduce the bug in text.