Title: Checkpoints — PyTorch main documentation
Description: Checkpointing in PyTorch C++ — saving and resuming training state.
Keywords:
Domain: docs.pytorch.org
{
"@context": "https://schema.org",
"@type": "Article",
"name": "Checkpoints",
"headline": "Checkpoints",
"description": "Checkpointing in PyTorch C++ \u2014 saving and resuming training state.",
"url": "/api/serialize/checkpoints.html",
"articleBody": "Checkpoints# Checkpoints save the complete training state so you can resume training after interruption. A checkpoint typically includes: Model parameters Optimizer state (momentum buffers, learning rates) Current epoch number Best validation loss/accuracy Creating Checkpoints# void save_checkpoint( std::shared_ptr\u003cNet\u003e model, torch::optim::Adam\u0026 optimizer, int epoch, const std::string\u0026 path) { torch::serialize::OutputArchive archive; model-\u003esave(archive); archive.write(\"epoch\", torch::tensor(epoch)); optimizer.save(archive); archive.save_to(path); } Loading Checkpoints# int load_checkpoint( std::shared_ptr\u003cNet\u003e model, torch::optim::Adam\u0026 optimizer, const std::string\u0026 path) { torch::serialize::InputArchive archive; archive.load_from(path); model-\u003eload(archive); torch::Tensor epoch_tensor; archive.read(\"epoch\", epoch_tensor); optimizer.load(archive); return epoch_tensor.item\u003cint\u003e(); } Complete Checkpoint Example# #include \u003ctorch/torch.h\u003e #include \u003ciostream\u003e #include \u003cfilesystem\u003e struct Net : torch::nn::Module { Net() { fc1 = register_module(\"fc1\", torch::nn::Linear(784, 256)); fc2 = register_module(\"fc2\", torch::nn::Linear(256, 10)); } torch::Tensor forward(torch::Tensor x) { x = torch::relu(fc1-\u003eforward(x.view({-1, 784}))); return fc2-\u003eforward(x); } torch::nn::Linear fc1{nullptr}, fc2{nullptr}; }; int main() { auto model = std::make_shared\u003cNet\u003e(); auto optimizer = torch::optim::Adam(model-\u003eparameters(), 1e-3); int start_epoch = 0; const std::string checkpoint_path = \"checkpoint.pt\"; // Resume from checkpoint if it exists if (std::filesystem::exists(checkpoint_path)) { std::cout \u003c\u003c \"Loading checkpoint...\" \u003c\u003c std::endl; start_epoch = load_checkpoint(model, optimizer, checkpoint_path); std::cout \u003c\u003c \"Resuming from epoch \" \u003c\u003c start_epoch \u003c\u003c std::endl; } // Training loop for (int epoch = start_epoch; epoch \u003c 100; ++epoch) { // ... training code ... // Save checkpoint every 10 epochs if ((epoch + 1) % 10 == 0) { save_checkpoint(model, optimizer, epoch + 1, checkpoint_path); std::cout \u003c\u003c \"Saved checkpoint at epoch \" \u003c\u003c epoch + 1 \u003c\u003c std::endl; } } return 0; } Best Practices# Save periodically: Save checkpoints at regular intervals (e.g., every epoch or every N batches) to minimize lost work. Keep multiple checkpoints: Maintain the last few checkpoints in case the most recent one is corrupted or represents a poor model state. Include all state: Save everything needed to resume training, including learning rate scheduler state if using one. Verify checkpoints: Occasionally verify that checkpoints can be loaded correctly.",
"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/checkpoints.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 | Checkpointing in PyTorch C++ — saving and resuming training state. |
| 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