Saving and loading tensors

In this lesson, we will look at dumping tensors to a local file and loading them again.

Saving a single tensor

Sometimes, we want to dump a tensor to the disk for future use immediately after an operation. It could save a lot of time in scenarios where the processing takes too long and we don’t want to go through the whole process again.

PyTorch provides torch.save to save objects to a file-like object. In this lesson, we only save the object to a file. The first parameter is the object we want to save, in this example, it’s a tensor. In this example, the second parameter is a file path

a = torch.tensor([1, 2, 3])
torch.save(a, "./output/tensor.pt")

Get hands-on with 1400+ tech skills courses.