Save a tensor to file

Hello! I want to save a tensor to a file but when I do it using file.write(str(tensor)), what it writes is " tensor(-0.0947, device=‘cuda:0’, grad_fn=MeanBackward0". How can I save just the numerical value (-0.0947). thank you!

3 Likes

Use torch.save(tensor, 'file.pt') and torch.load('file.pt')

26 Likes

If you have a single number in the tensor, you can get it by using tensor.item()
If you have a multidimensional tensor you can get just the data by doing tensor.data

1 Like

what is the way to save it such that we get something like this when we load:

db = load(file_name)
tensor1 = db["tensor1"]
tensor2 = db["tensor2"]
... etc

I noticed that pickling might not be supported in the future (so dill might not work later).

I am not saving model parameters. Just tensors.

You can save a python map:

m = {'a': tensor_a, 'b': tensor_b}
torch.save(m, file_name)
loaded = torch.load(file_name)
loaded['a'] == tensor_a
loaded['b'] == tensor_b

This is actually the same thing (with an OrderedDict) that happens when you store a model’s parameters using torch.save(model.state_dict(), file).

4 Likes

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

Is there an approach to load the tensor file into a Dataloader? Because my tensor file is large and I need to iterate over each row.

1 Like