Shortcuts, f, pickle_module=pickle, pickle_protocol=DEFAULT_PROTOCOL, _use_new_zipfile_serialization=True)[source]

Saves an object to a disk file.

See also: Saving and loading tensors

  • obj (object) – saved object

  • f (Union[str, PathLike, BinaryIO, IO[bytes]]) – a file-like object (has to implement write and flush) or a string or os.PathLike object containing a file name

  • pickle_module (Any) – module used for pickling metadata and objects

  • pickle_protocol (int) – can be specified to override the default protocol


A common PyTorch convention is to save tensors using .pt file extension.


PyTorch preserves storage sharing across serialization. See Saving and loading tensors preserves views for more details.


The 1.6 release of PyTorch switched 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 want to use the old format, pass the kwarg _use_new_zipfile_serialization=False.


>>> # Save to file
>>> x = torch.tensor([0, 1, 2, 3, 4])
>>>, '')
>>> # Save to io.BytesIO buffer
>>> buffer = io.BytesIO()
>>>, buffer)


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources