torch.save¶
-
torch.
save
(obj, f: Union[str, os.PathLike, BinaryIO], pickle_module=<module 'pickle' from '/home/glaringlee/miniconda3/envs/pytorch/lib/python3.8/pickle.py'>, pickle_protocol=2, _use_new_zipfile_serialization=True) → None[source]¶ Saves an object to a disk file.
See also: Recommended approach for saving a model
- Parameters
obj – saved object
f – a file-like object (has to implement write and flush) or a string or os.PathLike object containing a file name
pickle_module – module used for pickling metadata and objects
pickle_protocol – can be specified to override the default protocol
Note
A common PyTorch convention is to save tensors using .pt file extension.
Note
PyTorch preserves storage sharing across serialization. See preserve-storage-sharing for more details.
Note
The 1.6 release of PyTorch switched
torch.save
to use a new zipfile-based file format.torch.load
still retains the ability to load files in the old format. If for any reason you wanttorch.save
to use the old format, pass the kwarg_use_new_zipfile_serialization=False
.Example
>>> # Save to file >>> x = torch.tensor([0, 1, 2, 3, 4]) >>> torch.save(x, 'tensor.pt') >>> # Save to io.BytesIO buffer >>> buffer = io.BytesIO() >>> torch.save(x, buffer)