Template Function torch::save(const Value&, SaveToArgs&&…)

Function Documentation

template<typename Value, typename ...SaveToArgs>
void torch::save(const Value &value, SaveToArgs&&... args)

Serializes the given value.

There must be an overload of operator<< between serialize::OutputArchive and Value for this method to be well-formed. Currently, such an overload is provided for (subclasses of):

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.

torch::nn::Linear model(3, 4);
torch::save(model, "");

torch::optim::SGD sgd(/*lr=*/0.9);
std::ostringstream stream;
// Note that the same stream cannot be used in multiple torch::save(...)
// invocations, otherwise the header will be corrupted.
torch::save(sgd, stream);

auto tensor = torch::ones({3, 4});
torch::save(tensor, "");


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