docs/cpp/source/api/optim/index.md
The torch::optim namespace provides optimization algorithms for
training neural networks. These optimizers update model parameters based
on computed gradients to minimize the loss function.
When to use torch::optim:
Basic usage:
#include <torch/torch.h>
// Create model and optimizer
auto model = std::make_shared<Net>();
auto optimizer = torch::optim::Adam(
model->parameters(),
torch::optim::AdamOptions(1e-3));
// Training loop
for (auto& batch : *data_loader) {
optimizer.zero_grad(); // Clear gradients
auto loss = loss_fn(model->forward(batch.data), batch.target);
loss.backward(); // Compute gradients
optimizer.step(); // Update parameters
}
torch/csrc/api/include/torch/optim.h - Main optim headertorch/csrc/api/include/torch/optim/optimizer.h - Optimizer base classtorch/csrc/api/include/torch/optim/sgd.h - SGD optimizertorch/csrc/api/include/torch/optim/adam.h - Adam optimizerAll optimizers inherit from the Optimizer base class, which provides common
functionality for parameter updates, gradient zeroing, and state management.
:members:
:undoc-members:
:members:
:undoc-members:
:members:
:undoc-members:
:members:
:undoc-members:
Selecting the right optimizer depends on your model architecture, dataset, and training requirements:
:widths: 20 40 40
:header-rows: 1
* - Optimizer
- Best For
- Trade-offs
* - **SGD + Momentum**
- CNNs, well-understood problems, when you can tune hyperparameters
- Requires careful learning rate tuning; often achieves best final accuracy
* - **Adam/AdamW**
- General-purpose, transformers, quick prototyping
- Works well out-of-the-box; AdamW preferred with weight decay
* - **RMSprop**
- RNNs, non-stationary objectives
- Good for recurrent architectures; handles varying gradient scales
* - **Adagrad**
- Sparse data (NLP, embeddings)
- Learning rate decreases over time; good for infrequent features
* - **LBFGS**
- Small models, fine-tuning, convex problems
- Memory-intensive; requires closure function
:maxdepth: 1
gradient_descent
adaptive
second_order
schedulers