|
| 1 | +# Implementing Momentum Optimizer |
| 2 | + |
| 3 | +## Introduction |
| 4 | +Momentum is a popular optimization technique that helps accelerate gradient descent in the relevant direction and dampens oscillations. It works by adding a fraction of the previous update vector to the current gradient. |
| 5 | + |
| 6 | +## Learning Objectives |
| 7 | +- Understand how momentum optimization works |
| 8 | +- Learn to implement momentum-based gradient updates |
| 9 | +- Understand the effect of momentum on optimization |
| 10 | + |
| 11 | +## Theory |
| 12 | +Momentum optimization uses a moving average of gradients to determine the direction of the update. The key equations are: |
| 13 | + |
| 14 | +$v_t = \gamma v_{t-1} + \eta \nabla_\theta J(\theta)$ (Velocity update) |
| 15 | + |
| 16 | +$\theta_t = \theta_{t-1} - v_t$ (Parameter update) |
| 17 | + |
| 18 | +Where: |
| 19 | +- $v_t$ is the velocity at time t |
| 20 | +- $\gamma$ is the momentum coefficient (typically 0.9) |
| 21 | +- $\eta$ is the learning rate |
| 22 | +- $\nabla_\theta J(\theta)$ is the gradient of the loss function |
| 23 | + |
| 24 | +Read more at: |
| 25 | + |
| 26 | +1. Ruder, S. (2017). An overview of gradient descent optimization algorithms. [arXiv:1609.04747](https://arxiv.org/pdf/1609.04747) |
| 27 | + |
| 28 | + |
| 29 | +## Problem Statement |
| 30 | +Implement the momentum optimizer update step function. Your function should take the current parameter value, gradient, and velocity as inputs, and return the updated parameter value and new velocity. |
| 31 | + |
| 32 | +### Input Format |
| 33 | +The function should accept: |
| 34 | +- parameter: Current parameter value |
| 35 | +- grad: Current gradient |
| 36 | +- velocity: Current velocity |
| 37 | +- learning_rate: Learning rate (default=0.01) |
| 38 | +- momentum: Momentum coefficient (default=0.9) |
| 39 | + |
| 40 | +### Output Format |
| 41 | +Return tuple: (updated_parameter, updated_velocity) |
| 42 | + |
| 43 | +## Example |
| 44 | +```python |
| 45 | +# Example usage: |
| 46 | +parameter = 1.0 |
| 47 | +grad = 0.1 |
| 48 | +velocity = 0.1 |
| 49 | + |
| 50 | +new_param, new_velocity = momentum_optimizer(parameter, grad, velocity) |
| 51 | +``` |
| 52 | + |
| 53 | +## Tips |
| 54 | +- Initialize velocity as zero |
| 55 | +- Use numpy for numerical operations |
| 56 | +- Test with both scalar and array inputs |
| 57 | + |
| 58 | +--- |
0 commit comments