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:
Domain: docs.pytorch.org
{
"@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:language | en |
| llm:site-type | documentation |
| llm:framework | PyTorch |
| llm:description | Save and load functions in PyTorch C++ — torch::save and torch::load for tensors and modules. |
| llm:navigation-file | https://pytorch.org/docs/stable/llms.txt |
| llm:sitemap | https://pytorch.org/docs/stable/sitemap.xml |
| llm:version | main |
| llm:project | PyTorch |
| llm:page-type | documentation |
| og:image | https://docs.pytorch.org/docs/stable/_static/img/pytorch_seo.png |
| None | 3 |
Links:
Viewport: width=device-width, initial-scale=1