Lesson Introduction

Welcome back! Today, we'll explore Gradient Descent with Momentum. You've already familiarized yourself with basic gradient descent. However, sometimes gradient descent is slow and gets stuck, especially on bumpy paths to the minimum.

So, how do we speed it up? We use momentum. Imagine pushing a heavy shopping cart. Instead of stopping and starting, you build momentum. This helps you move faster. By the end of this lesson, you'll understand how gradient descent with momentum works, implement it in Python, and see how it improves optimization.

Introducing Momentum
How Velocity Works in Gradient Descent with Momentum
Python Implementation: part 1

Now, let's implement Gradient Descent with Momentum in Python. Here's the code snippet:

# Gradient descent with momentum
def gradient_descent_with_momentum(f_grad, init_point, learning_rate=0.1, momentum=0.9, iterations=100):
    point = list(init_point)
    velocity = [0] * len(point)
    for _ in range(iterations):
        grad = f_grad(point)
        for i in range(len(point)):
            velocity[i] = momentum * velocity[i] - learning_rate * grad[i]
            point[i] += velocity[i]
    return point

Here’s a breakdown of the key lines in the code:

  • velocity = [0] * len(point): Initializes the velocity vector with zeros, having the same length as the starting point.
  • velocity[i] = momentum * velocity[i] - learning_rate * grad[i]: Updates the velocity by applying the momentum and subtracting the gradient scaled by the learning rate.
  • point[i] += velocity[i]: Updates the current point using the newly calculated velocity.
Python Implementation: part 2

Here's the continuation of our implementation with the example function and initial point:

# Example function: f(x, y) = x^2 + y^2
def quadratic_gradient(point):
    x, y = point
    return [2 * x, 2 * y]

# Initial point
init_point = [2, 2]

# Find the optimal point using gradient descent with momentum
optimal_point_momentum = gradient_descent_with_momentum(quadratic_gradient, init_point, learning_rate=0.1, iterations=100)
print("Optimal point after gradient descent with momentum:", optimal_point_momentum)  # Optimal point after gradient descent with momentum: [~0, ~0]
Benefits of Using Momentum

Using momentum in gradient descent offers several benefits:

  1. Faster Convergence: Reaches the minimum quicker.
  2. Reduced Oscillations: Smoothens the path, reducing back-and-forth movements.
  3. Better Navigation Through Local Minima: Avoids getting stuck in small bumps and oscillations.
Visualizing Momentum
Lesson Summary

Congratulations! You've learned about Gradient Descent with Momentum. We covered its importance, how it works, and implemented it in Python. You've seen how it speeds up optimization and reduces oscillations.

Now, let’s practice. In the practice session, you'll implement Gradient Descent with Momentum and observe its effects on different functions. Get ready to solidify your understanding and see momentum in action!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal