An optimizer adjusts a model's parameter vector
where
This library provides a batch gradient descent optimizer with a fixed learning rate and maximum iteration count, suitable for small embedded models where training data fits in memory and simplicity is paramount.
The gradient
For a convex loss with Lipschitz-continuous gradient (
provided
For non-convex losses (typical in neural networks), gradient descent converges to a stationary point (
| Behavior | |
|---|---|
| Too small ( |
Slow convergence, many iterations wasted |
| Optimal ( |
Fastest reliable convergence |
| Too large ( |
Oscillation and divergence |
graph LR
subgraph "Learning Rate Effect"
A["η too small<br/>crawls to minimum"]
B["η just right<br/>smooth convergence"]
C["η too large<br/>oscillates / diverges"]
end
| Operation | Time | Space |
|---|---|---|
| One gradient evaluation |
|
|
| One parameter update | In-place | |
| Total training (T iterations) |
|
Memory overhead is minimal: only the current parameter vector, the gradient vector, and the optimizer result. No momentum buffers or second-moment estimates.
Setup: 2 parameters,
Iteration 1:
| Step | Computation | Result |
|---|---|---|
| Gradient | ||
| Update | ||
| Loss |
Iteration 2:
| Step | Computation | Result |
|---|---|---|
| Gradient | ||
| Update | ||
| Loss |
Pattern: Each iteration multiplies the loss by
Convergence trace:
graph LR
I0["θ₀ = (2, -1)<br/>ℒ = 5.0"] --> I1["θ₁ = (1.6, -0.8)<br/>ℒ = 3.2"]
I1 --> I2["θ₂ = (1.28, -0.64)<br/>ℒ = 2.05"]
I2 --> Idots["⋯"]
Idots --> I20["θ₂₀ ≈ (0.01, -0.005)<br/>ℒ ≈ 0.0003"]
-
Learning rate too high. The loss oscillates or increases. Start with
$\eta = 0.01$ and decrease if unstable. -
Flat regions / saddle points. Gradient descent stalls when
$|\nabla \mathcal{L}| \approx 0$ at non-optimal points. Momentum-based methods escape saddle points faster. - Ill-conditioned loss landscape. When eigenvalues of the Hessian span many orders of magnitude, gradient descent oscillates along steep directions while barely progressing along flat ones. Adaptive methods (Adam) handle this better.
-
Max iterations reached. The optimizer returns whatever parameters it has at
maxIterations— always checkresult.finalCostto assess convergence quality. - Fixed-point gradients. In Q15/Q31, multiply-accumulate in the gradient can overflow. Scale the learning rate to keep weight updates within representable range.
| Variant | Key Difference |
|---|---|
| SGD (Stochastic Gradient Descent) | Uses a random mini-batch per iteration; noise helps escape local minima |
| SGD with Momentum | Accumulates a velocity: |
| Nesterov Momentum | Evaluates gradient at the lookahead position |
| AdaGrad | Per-parameter adaptive learning rate based on accumulated squared gradients |
| RMSProp | Exponentially decaying average of squared gradients; handles non-stationary objectives |
| Adam | Combines momentum and RMSProp with bias correction; most popular adaptive method |
- On-device neural network training — Gradient descent trains small models directly on the microcontroller.
- Online adaptation — Updating model parameters in real-time as new sensor data arrives.
- Calibration — Minimizing calibration error for sensor linearization.
- System identification — Fitting model parameters to measured input-output data.
graph TD
Opt["Optimizer<br/>(Gradient Descent)"]
Loss["Loss Functions"]
Model["Model"]
Reg["Regularization"]
LR["Linear Regression"]
Loss -->|"∇ℒ"| Opt
Model -->|"θ, ∇θ"| Opt
Reg -->|"penalty gradient"| Loss
Opt -.->|"analytical solution at η→∞, 1 step"| LR
| Component | Relationship |
|---|---|
| Loss Functions | Provides the Cost() and Gradient() the optimizer calls each iteration |
| Model | Passes initial parameters to the optimizer and receives optimized parameters back |
| Regularization | Adds a penalty gradient to |
| Linear Regression | For MSE on a linear model, gradient descent converges to the same solution as the normal equation |
- Ruder, S., "An overview of gradient descent optimization algorithms", arXiv:1609.04747, 2016.
- Bottou, L., Curtis, F.E., and Nocedal, J., "Optimization methods for large-scale machine learning", SIAM Review, 60(2), 2018.
- Kingma, D.P. and Ba, J., "Adam: A method for stochastic optimization", ICLR, 2015.