Shortcuts

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, "model.pt");

torch::optim::SGD sgd(model->parameters(), 0.9); // 0.9 is learning rate
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, "my_tensor.pt");

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources