René's URL Explorer Experiment


Title: Saving and Loading — PyTorch main documentation

Description: Save and load functions in PyTorch C++ — torch::save and torch::load for tensors and modules.

Keywords:

direct link

Domain: docs.pytorch.org


Hey, it has json ld scripts:
    {
       "@context": "https://schema.org",
       "@type": "Article",
       "name": "Saving and Loading",
       "headline": "Saving and Loading",
       "description": "Save and load functions in PyTorch C++ \u2014 torch::save and torch::load for tensors and modules.",
       "url": "/api/serialize/save_load.html",
       "articleBody": "Saving and Loading# The primary interface for serialization uses the torch::save and torch::load functions, which can save and load tensors, modules, and optimizers. Save Functions# template\u003ctypename Value, typename ...SaveToArgs\u003evoid torch::save(const Value \u0026value, SaveToArgs\u0026\u0026... args)# Serializes the given value. There must be an overload of operator\u003c\u003c between serialize::OutputArchive and Value for this method to be well-formed. Currently, such an overload is provided for (subclasses of): torch::nn::Module, torch::optim::Optimizer torch::Tensor To perform the serialization, a serialize::OutputArchive is constructed, and all arguments after the value are forwarded to its save_to method. For example, you can pass a filename, or an ostream. .. code-block:: cpp torch::nn::Linear model(3, 4); torch::save(model, \u201cmodel.pt\u201d); torch::optim::SGD sgd(model-\u003eparameters(), 0.9); // 0.9 is learning rate std::ostringstream stream; // Note that the same stream cannot be used in multiple torch::save(\u2026) // invocations, otherwise the header will be corrupted. torch::save(sgd, stream); auto tensor = torch::ones({3, 4}); torch::save(tensor, \u201cmy_tensor.pt\u201d); template\u003ctypename ...SaveToArgs\u003evoid torch::save(const std::vector\u003ctorch::Tensor\u003e \u0026tensor_vec, SaveToArgs\u0026\u0026... args)# Serializes the given tensor_vec of type std::vector\u003ctorch::Tensor\u003e. To perform the serialization, a serialize::OutputArchive is constructed, and all arguments after the tensor_vec are forwarded to its save_to method. For example, you can pass a filename, or an ostream. .. code-block:: cpp std::vectortorch::Tensor tensor_vec = { torch::randn({1, 2}), torch::randn({3, 4}) }; torch::save(tensor_vec, \u201cmy_tensor_vec.pt\u201d); std::vectortorch::Tensor tensor_vec = { torch::randn({5, 6}), torch::randn({7, 8}) }; std::ostringstream stream; // Note that the same stream cannot be used in multiple torch::save(\u2026) // invocations, otherwise the header will be corrupted. torch::save(tensor_vec, stream); Load Functions# template\u003ctypename Value, typename ...LoadFromArgs\u003evoid torch::load(Value \u0026value, LoadFromArgs\u0026\u0026... args)# Deserializes the given value. There must be an overload of operator\u003e\u003e between serialize::InputArchive and Value for this method to be well-formed. Currently, such an overload is provided for (subclasses of): torch::nn::Module, torch::optim::Optimizer torch::Tensor To perform the serialization, a serialize::InputArchive is constructed, and all arguments after the value are forwarded to its load_from method. For example, you can pass a filename, or an istream. .. code-block:: cpp torch::nn::Linear model(3, 4); torch::load(model, \u201cmodel.pt\u201d); torch::optim::SGD sgd(model-\u003eparameters(), 0.9); // 0.9 is learning rate std::istringstream stream(\u201d\u2026\u201d); torch::load(sgd, stream); auto tensor = torch::ones({3, 4}); torch::load(tensor, \u201cmy_tensor.pt\u201d); template\u003ctypename ...LoadFromArgs\u003evoid torch::load(std::vector\u003ctorch::Tensor\u003e \u0026tensor_vec, LoadFromArgs\u0026\u0026... args)# Deserializes the given tensor_vec of type std::vector\u003ctorch::Tensor\u003e. To perform the serialization, a serialize::InputArchive is constructed, and all arguments after the value are forwarded to its load_from method. For example, you can pass a filename, or an istream. .. code-block:: cpp std::vectortorch::Tensor tensor_vec; torch::load(tensor_vec, \u201cmy_tensor_vec.pt\u201d); std::vectortorch::Tensor tensor_vec; std::istringstream stream(\u201d\u2026\u201d); torch::load(tensor_vec, stream); Saving and Loading Tensors# // Save a tensor torch::Tensor tensor = torch::randn({2, 3}); torch::save(tensor, \"tensor.pt\"); // Load a tensor torch::Tensor loaded; torch::load(loaded, \"tensor.pt\"); Saving and Loading Modules# // Define a model struct Net : torch::nn::Module { Net() { fc1 = register_module(\"fc1\", torch::nn::Linear(784, 64)); fc2 = register_module(\"fc2\", torch::nn::Linear(64, 10)); } torch::Tensor forward(torch::Tensor x) { x = torch::relu(fc1-\u003eforward(x)); return fc2-\u003eforward(x); } torch::nn::Linear fc1{nullptr}, fc2{nullptr}; }; // Save model auto model = std::make_shared\u003cNet\u003e(); torch::save(model, \"model.pt\"); // Load model auto loaded_model = std::make_shared\u003cNet\u003e(); torch::load(loaded_model, \"model.pt\"); Saving Optimizer State# auto model = std::make_shared\u003cNet\u003e(); auto optimizer = torch::optim::Adam(model-\u003eparameters(), 0.001); // Train... // Save both model and optimizer torch::save(model, \"model.pt\"); torch::save(optimizer, \"optimizer.pt\"); // Load both torch::load(model, \"model.pt\"); torch::load(optimizer, \"optimizer.pt\");",
       "author": {
         "@type": "Organization",
         "name": "PyTorch Contributors",
         "url": "https://pytorch.org"
       },
       "image": "https://pytorch.org/docs/stable/_static/img/pytorch_seo.png",
       "mainEntityOfPage": {
         "@type": "WebPage",
         "@id": "/api/serialize/save_load.html"
       },
       "datePublished": "2023-01-01T00:00:00Z",
       "dateModified": "2023-01-01T00:00:00Z"
     }
 

docsearch:languageen
llm:site-typedocumentation
llm:frameworkPyTorch
llm:descriptionSave and load functions in PyTorch C++ — torch::save and torch::load for tensors and modules.
llm:navigation-filehttps://pytorch.org/docs/stable/llms.txt
llm:sitemaphttps://pytorch.org/docs/stable/sitemap.xml
llm:versionmain
llm:projectPyTorch
llm:page-typedocumentation
og:imagehttps://docs.pytorch.org/docs/stable/_static/img/pytorch_seo.png
None3

Links:

Skip to main contenthttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#main-content
Home https://docs.pytorch.org/cppdocs/index.html
mainhttps://docs.pytorch.org/cppdocs/index.html
Installing C++ Distributions of PyTorch https://docs.pytorch.org/cppdocs/installing.html
The C++ Frontend https://docs.pytorch.org/cppdocs/frontend.html
C++ API Reference https://docs.pytorch.org/cppdocs/api/index.html
ATen: Tensor Library https://docs.pytorch.org/cppdocs/api/aten/index.html
C10: Core Utilities https://docs.pytorch.org/cppdocs/api/c10/index.html
Autograd: Automatic Differentiation https://docs.pytorch.org/cppdocs/api/autograd/index.html
CUDA Support https://docs.pytorch.org/cppdocs/api/cuda/index.html
XPU Support https://docs.pytorch.org/cppdocs/api/xpu/index.html
Neural Network Modules (torch::nn) https://docs.pytorch.org/cppdocs/api/nn/index.html
Optimizers (torch::optim) https://docs.pytorch.org/cppdocs/api/optim/index.html
Data Loading (torch::data) https://docs.pytorch.org/cppdocs/api/data/index.html
Serialization (torch::serialize) https://docs.pytorch.org/cppdocs/api/serialize/index.html
Torch Library API https://docs.pytorch.org/cppdocs/api/library/index.html
Torch Stable API https://docs.pytorch.org/cppdocs/api/stable/index.html
FAQ https://docs.pytorch.org/cppdocs/faq.html
Go to pytorch.org https://pytorch.org
Xhttps://x.com/PyTorch
GitHubhttps://github.com/pytorch/pytorch
PyTorch Forumhttps://discuss.pytorch.org/
PyPihttps://pypi.org/project/torch/
mainhttps://docs.pytorch.org/cppdocs/index.html
Installing C++ Distributions of PyTorch https://docs.pytorch.org/cppdocs/installing.html
The C++ Frontend https://docs.pytorch.org/cppdocs/frontend.html
C++ API Reference https://docs.pytorch.org/cppdocs/api/index.html
ATen: Tensor Library https://docs.pytorch.org/cppdocs/api/aten/index.html
C10: Core Utilities https://docs.pytorch.org/cppdocs/api/c10/index.html
Autograd: Automatic Differentiation https://docs.pytorch.org/cppdocs/api/autograd/index.html
CUDA Support https://docs.pytorch.org/cppdocs/api/cuda/index.html
XPU Support https://docs.pytorch.org/cppdocs/api/xpu/index.html
Neural Network Modules (torch::nn) https://docs.pytorch.org/cppdocs/api/nn/index.html
Optimizers (torch::optim) https://docs.pytorch.org/cppdocs/api/optim/index.html
Data Loading (torch::data) https://docs.pytorch.org/cppdocs/api/data/index.html
Serialization (torch::serialize) https://docs.pytorch.org/cppdocs/api/serialize/index.html
Torch Library API https://docs.pytorch.org/cppdocs/api/library/index.html
Torch Stable API https://docs.pytorch.org/cppdocs/api/stable/index.html
FAQ https://docs.pytorch.org/cppdocs/faq.html
Go to pytorch.org https://pytorch.org
Xhttps://x.com/PyTorch
GitHubhttps://github.com/pytorch/pytorch
PyTorch Forumhttps://discuss.pytorch.org/
PyPihttps://pypi.org/project/torch/
ATen: Tensor Libraryhttps://docs.pytorch.org/cppdocs/api/aten/index.html
Tensor Classhttps://docs.pytorch.org/cppdocs/api/aten/tensor.html
Tensor Creationhttps://docs.pytorch.org/cppdocs/api/aten/creation.html
Tensor Indexinghttps://docs.pytorch.org/cppdocs/api/aten/indexing.html
Tensor Accessorshttps://docs.pytorch.org/cppdocs/api/aten/accessors.html
C10: Core Utilitieshttps://docs.pytorch.org/cppdocs/api/c10/index.html
Device and DeviceTypehttps://docs.pytorch.org/cppdocs/api/c10/device.html
Device Guardshttps://docs.pytorch.org/cppdocs/api/c10/guards.html
Streamshttps://docs.pytorch.org/cppdocs/api/c10/streams.html
Core Typeshttps://docs.pytorch.org/cppdocs/api/c10/types.html
Utilitieshttps://docs.pytorch.org/cppdocs/api/c10/utilities.html
Autograd: Automatic Differentiationhttps://docs.pytorch.org/cppdocs/api/autograd/index.html
Gradient Computationhttps://docs.pytorch.org/cppdocs/api/autograd/gradient.html
Custom Autograd Functionshttps://docs.pytorch.org/cppdocs/api/autograd/custom_functions.html
Gradient Modeshttps://docs.pytorch.org/cppdocs/api/autograd/modes.html
CUDA Supporthttps://docs.pytorch.org/cppdocs/api/cuda/index.html
CUDA Streamshttps://docs.pytorch.org/cppdocs/api/cuda/streams.html
CUDA Guardshttps://docs.pytorch.org/cppdocs/api/cuda/guards.html
CUDA Utility Functionshttps://docs.pytorch.org/cppdocs/api/cuda/utilities.html
XPU Supporthttps://docs.pytorch.org/cppdocs/api/xpu/index.html
XPU Streamshttps://docs.pytorch.org/cppdocs/api/xpu/streams.html
XPU Utility Functionshttps://docs.pytorch.org/cppdocs/api/xpu/utilities.html
Neural Network Modules (torch::nn)https://docs.pytorch.org/cppdocs/api/nn/index.html
Containershttps://docs.pytorch.org/cppdocs/api/nn/containers.html
Convolution Layershttps://docs.pytorch.org/cppdocs/api/nn/convolution.html
Pooling Layershttps://docs.pytorch.org/cppdocs/api/nn/pooling.html
Linear Layershttps://docs.pytorch.org/cppdocs/api/nn/linear.html
Activation Functionshttps://docs.pytorch.org/cppdocs/api/nn/activation.html
Normalization Layershttps://docs.pytorch.org/cppdocs/api/nn/normalization.html
Dropout Layershttps://docs.pytorch.org/cppdocs/api/nn/dropout.html
Embedding Layershttps://docs.pytorch.org/cppdocs/api/nn/embedding.html
Recurrent Layershttps://docs.pytorch.org/cppdocs/api/nn/recurrent.html
Transformer Layershttps://docs.pytorch.org/cppdocs/api/nn/transformer.html
Loss Functionshttps://docs.pytorch.org/cppdocs/api/nn/loss.html
Functional APIhttps://docs.pytorch.org/cppdocs/api/nn/functional.html
Utilitieshttps://docs.pytorch.org/cppdocs/api/nn/utilities.html
Optimizers (torch::optim)https://docs.pytorch.org/cppdocs/api/optim/index.html
Gradient Descent Optimizershttps://docs.pytorch.org/cppdocs/api/optim/gradient_descent.html
Adaptive Learning Rate Optimizershttps://docs.pytorch.org/cppdocs/api/optim/adaptive.html
Second-Order Optimizershttps://docs.pytorch.org/cppdocs/api/optim/second_order.html
Learning Rate Schedulershttps://docs.pytorch.org/cppdocs/api/optim/schedulers.html
Data Loading (torch::data)https://docs.pytorch.org/cppdocs/api/data/index.html
Datasetshttps://docs.pytorch.org/cppdocs/api/data/datasets.html
DataLoaderhttps://docs.pytorch.org/cppdocs/api/data/dataloader.html
Samplershttps://docs.pytorch.org/cppdocs/api/data/samplers.html
Transformshttps://docs.pytorch.org/cppdocs/api/data/transforms.html
Serialization (torch::serialize)https://docs.pytorch.org/cppdocs/api/serialize/index.html
Saving and Loadinghttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html
Archiveshttps://docs.pytorch.org/cppdocs/api/serialize/archives.html
Checkpointshttps://docs.pytorch.org/cppdocs/api/serialize/checkpoints.html
Torch Library APIhttps://docs.pytorch.org/cppdocs/api/library/index.html
Operator Registrationhttps://docs.pytorch.org/cppdocs/api/library/registration.html
Custom Classeshttps://docs.pytorch.org/cppdocs/api/library/custom_classes.html
Library Versioninghttps://docs.pytorch.org/cppdocs/api/library/versioning.html
Torch Stable APIhttps://docs.pytorch.org/cppdocs/api/stable/index.html
Library Registration Macroshttps://docs.pytorch.org/cppdocs/api/stable/registration.html
Stable Operatorshttps://docs.pytorch.org/cppdocs/api/stable/operators.html
Utilitieshttps://docs.pytorch.org/cppdocs/api/stable/utilities.html
https://docs.pytorch.org/cppdocs/index.html
C++ API Referencehttps://docs.pytorch.org/cppdocs/api/index.html
Serialization (torch::serialize)https://docs.pytorch.org/cppdocs/api/serialize/index.html
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#saving-and-loading
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#save-functions
Valuehttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4I0DpEN5torch4saveEvRK5ValueDpRR10SaveToArgs
SaveToArgshttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4I0DpEN5torch4saveEvRK5ValueDpRR10SaveToArgs
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4I0DpEN5torch4saveEvRK5ValueDpRR10SaveToArgs
serialize::OutputArchivehttps://docs.pytorch.org/cppdocs/api/serialize/archives.html#PyTorchclasstorch_1_1serialize_1_1_output_archive
torch::nn::Modulehttps://docs.pytorch.org/cppdocs/api/nn/index.html#PyTorchclasstorch_1_1nn_1_1_module
torch::optim::Optimizerhttps://docs.pytorch.org/cppdocs/api/optim/index.html#PyTorchclasstorch_1_1optim_1_1_optimizer
serialize::OutputArchivehttps://docs.pytorch.org/cppdocs/api/serialize/archives.html#PyTorchclasstorch_1_1serialize_1_1_output_archive
SaveToArgshttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4IDpEN5torch4saveEvRKNSt6vectorIN5torch6TensorEEEDpRR10SaveToArgs
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4IDpEN5torch4saveEvRKNSt6vectorIN5torch6TensorEEEDpRR10SaveToArgs
serialize::OutputArchivehttps://docs.pytorch.org/cppdocs/api/serialize/archives.html#PyTorchclasstorch_1_1serialize_1_1_output_archive
torch::Tensortorch::Tensor
torch::Tensortorch::Tensor
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#load-functions
Valuehttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4I0DpEN5torch4loadEvR5ValueDpRR12LoadFromArgs
LoadFromArgshttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4I0DpEN5torch4loadEvR5ValueDpRR12LoadFromArgs
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4I0DpEN5torch4loadEvR5ValueDpRR12LoadFromArgs
serialize::InputArchivehttps://docs.pytorch.org/cppdocs/api/serialize/archives.html#PyTorchclasstorch_1_1serialize_1_1_input_archive
torch::nn::Modulehttps://docs.pytorch.org/cppdocs/api/nn/index.html#PyTorchclasstorch_1_1nn_1_1_module
torch::optim::Optimizerhttps://docs.pytorch.org/cppdocs/api/optim/index.html#PyTorchclasstorch_1_1optim_1_1_optimizer
serialize::InputArchivehttps://docs.pytorch.org/cppdocs/api/serialize/archives.html#PyTorchclasstorch_1_1serialize_1_1_input_archive
LoadFromArgshttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4IDpEN5torch4loadEvRNSt6vectorIN5torch6TensorEEEDpRR12LoadFromArgs
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4IDpEN5torch4loadEvRNSt6vectorIN5torch6TensorEEEDpRR12LoadFromArgs
serialize::InputArchivehttps://docs.pytorch.org/cppdocs/api/serialize/archives.html#PyTorchclasstorch_1_1serialize_1_1_input_archive
torch::Tensortorch::Tensor
torch::Tensortorch::Tensor
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#saving-and-loading-tensors
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#saving-and-loading-modules
#https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#saving-optimizer-state
previous Serialization (torch::serialize) https://docs.pytorch.org/cppdocs/api/serialize/index.html
next Archives https://docs.pytorch.org/cppdocs/api/serialize/archives.html
PyData Sphinx Themehttps://pydata-sphinx-theme.readthedocs.io/en/stable/index.html
previous Serialization (torch::serialize) https://docs.pytorch.org/cppdocs/api/serialize/index.html
next Archives https://docs.pytorch.org/cppdocs/api/serialize/archives.html
Save Functionshttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#save-functions
save()https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4I0DpEN5torch4saveEvRK5ValueDpRR10SaveToArgs
save()https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4IDpEN5torch4saveEvRKNSt6vectorIN5torch6TensorEEEDpRR10SaveToArgs
Load Functionshttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#load-functions
load()https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4I0DpEN5torch4loadEvR5ValueDpRR12LoadFromArgs
load()https://docs.pytorch.org/cppdocs/api/serialize/save_load.html#_CPPv4IDpEN5torch4loadEvRNSt6vectorIN5torch6TensorEEEDpRR12LoadFromArgs
Saving and Loading Tensorshttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#saving-and-loading-tensors
Saving and Loading Moduleshttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#saving-and-loading-modules
Saving Optimizer Statehttps://docs.pytorch.org/cppdocs/api/serialize/save_load.html#saving-optimizer-state
Show Source https://docs.pytorch.org/cppdocs/_sources/api/serialize/save_load.md.txt
ExecuTorchhttps://docs.pytorch.org/executorch
Helionhttps://docs.pytorch.org/helion
torchaohttps://docs.pytorch.org/ao
kinetohttps://github.com/pytorch/kineto
torchtitanhttps://github.com/pytorch/torchtitan
TorchRLhttps://docs.pytorch.org/rl
torchvisionhttps://docs.pytorch.org/vision
torchaudiohttps://docs.pytorch.org/audio
tensordicthttps://docs.pytorch.org/tensordict
PyTorch on XLA Deviceshttps://docs.pytorch.org/xla
View Docshttps://docs.pytorch.org/docs/stable/index.html
View Tutorialshttps://docs.pytorch.org/tutorials
View Resourceshttps://pytorch.org/resources
Privacy Policyhttps://www.linuxfoundation.org/privacy/
https://www.facebook.com/pytorch
https://twitter.com/pytorch
https://www.youtube.com/pytorch
https://www.linkedin.com/company/pytorch
https://pytorch.slack.com
https://pytorch.org/wechat
Policieshttps://www.linuxfoundation.org/legal/policies
Trademark Usagehttps://www.linuxfoundation.org/trademark-usage
Privacy Policyhttp://www.linuxfoundation.org/privacy
Cookies Policyhttps://opensource.fb.com/legal/cookie-policy
Sphinxhttps://www.sphinx-doc.org/
PyData Sphinx Themehttps://pydata-sphinx-theme.readthedocs.io/en/stable/index.html

Viewport: width=device-width, initial-scale=1


URLs of crawlers that visited me.