Save a tensor to file

Do you know if it’s better to save the tensors as numpy data or torch tensors data?

Anyone aware of the pros & cons of using numpy.save vs torch.save?

I wonder if that will cause bugs when using the ToTensor() transform if the data is already saved as torch tensors.


related: What is the recommended format to save data in pytorch?


fully functionable example:

#%%

import torch

from pathlib import Path

path = Path('~/data/tmp/').expanduser()

tensor_a = torch.rand(2,3)
tensor_b = torch.rand(1,3)

db = {'a': tensor_a, 'b': tensor_b}

torch.save(db, path/'torch_db')
loaded = torch.load(path/'torch_db')
print( loaded['a'] == tensor_a )
print( loaded['b'] == tensor_b )
3 Likes