docs/cpp/source/api/serialize/index.md
The torch::serialize namespace provides utilities for saving and loading
model weights, tensors, and optimizer state. This enables checkpointing during
training and deployment of trained models.
When to use torch::serialize:
Basic usage:
#include <torch/torch.h>
// Save a model
auto model = std::make_shared<Net>();
// ... train the model ...
torch::save(model, "model.pt");
// Load a model
auto loaded_model = std::make_shared<Net>();
torch::load(loaded_model, "model.pt");
// Save and load tensors
torch::Tensor tensor = torch::randn({2, 3});
torch::save(tensor, "tensor.pt");
torch::Tensor loaded_tensor;
torch::load(loaded_tensor, "tensor.pt");
torch/csrc/api/include/torch/serialize.h - Main serialization headertorch/csrc/api/include/torch/serialize/archive.h - Archive classestorch/csrc/api/include/torch/serialize/input-archive.h - Input archivetorch/csrc/api/include/torch/serialize/output-archive.h - Output archive:maxdepth: 1
save_load
archives
checkpoints