I used setattr
to save some information with a Pytorch Tensor, which can be retrieved as expected:
import torch
before_write = torch.Tensor()
setattr(before_write, "features", "my_features")
print(before_write.features)
> 'my_features'
Unexpectedly, when I write the tensor to disk, and then read it back in again, the features have disappeared:
torch.save(before_write, "~/my_tensor")
after_write = torch.load("~/my_tensor")
print(after_write.features)
> AttributeError: 'Tensor' object has no attribute 'features'
Why is the attribute lost when writing the tensor to disk? Is this expected behaviour? Would there be a workaround to save the features together with the tensor?
NB: using an empty Python class and writing it with pickle, does retain the attributes.