Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 28

Visualizing Loss Surfaces

Recommended resources

Introduction

Welcome to the second major phase of our course! Having built a strong foundation in linear algebra—understanding how to represent data as vectors, manipulate it with matrices, and analyze its structure with tools like PCA and SVD—we are now shifting our focus from representation to optimization. This is the heart of how machine learning models actually learn from data.

The central idea behind training a model is to define a loss function (also called a cost or error function). This is a multivariable function that takes the model's parameters (its weights and biases) as input and outputs a single number representing how "wrong" the model's predictions are on the training data. The goal of training is to find the set of parameters that makes this loss as small as possible.

In this lesson, we will develop the fundamental mental model for all of optimization. We will learn to visualize the loss function as a high-dimensional "surface" or loss landscape. Your objective is to learn how to picture this landscape and identify its most important geographical features: its valleys (minima), peaks (maxima), and treacherous passes (saddle points). Understanding this terrain is the first step toward navigating it effectively.


From Functions to Landscapes

In machine learning, a model can have millions of parameters. The loss function, , therefore depends on all of these parameters. For a model with parameters , the loss is a function . Our goal is to find the combination of values that minimizes .

Directly picturing a function with millions of inputs is impossible. The key is to build our intuition from simpler cases. Let's start by watching a segment from 3Blue1Brown's excellent series on deep learning, which introduces the concept of a cost function and its landscape.

Gradient descent, how neural networks learn Chapter 2, Deep learning

This video brilliantly builds the intuition for a loss function, starting from the general idea and simplifying it to a visualizable 1D and 2D case.

Watch these three key segments: First, understand what a cost function is: a measure of how "lousy" a network is. Watch from this introduction. Next, see how minimizing a function works in a simple 1D case, analogous to a ball rolling down a hill. Watch this simplification. Finally, see this idea extended to two inputs, where the function becomes a surface. This is the core visualization for this lesson. Pay close attention to the analogy of a ball rolling down this 2D surface from this section.

As the video illustrates, for a model with two parameters, say and , we can visualize the loss function as a 3D surface. The and axes form a horizontal plane (the parameter space), and the value of the loss is the height of the surface at each point. The process of training the model is equivalent to finding the lowest point on this surface.


The Geography of a Loss Landscape

This "terrain" of the loss function has a specific geography that determines how easy or hard it is for an optimization algorithm to find the lowest point. Let's formally define the key features.

Analyzing Machine Learning Loss Surfaces

This article provides clear, concise definitions for the critical points on a loss surface.

First, read the introduction, Understanding Loss Surfaces. Then, skip to the section "Features of Loss Surfaces" and read the descriptions for all the bullet points, covering global and local minima, saddle points, plateaus, and the concept of sharp vs. flat minima.

Let's summarize and visualize these key landmarks.

  • Global Minimum: The single lowest point across the entire landscape. This represents the best possible set of parameters for the model.
  • Local Minima: "Valleys" in the landscape that are lower than their immediate surroundings, but not the lowest point overall. An optimizer can get "trapped" in a local minimum, thinking it has found the best solution when a better one exists elsewhere.
  • Maxima: The peaks of the landscape. Optimization algorithms are designed to move away from these.
  • Saddle Points: These are perhaps the most interesting and troublesome features in high-dimensional landscapes. A saddle point is a location where the landscape is flat, but it's a minimum along some directions and a maximum along others. Imagine the shape of a horse's saddle: it curves up front-to-back but down side-to-side. Simple optimizers can get "stuck" on these flat regions, slowing down or stopping training altogether.

Given your background in physics and chemistry, you can think of this landscape as a potential energy surface.

This image shows a potential energy surface. The 'Energy' axis corresponds to the loss, while 'Coordinate I' and 'Coordinate II' represent two model parameters. The local minima are stable or metastable states, while the saddle points represent transition states between them. This is a powerful analogy for how an optimizer explores the parameter space.

In the context of modern machine learning, especially deep learning, these landscapes are known to be highly complex and non-convex.

This is a stylized depiction of a loss landscape in deep learning. It highlights the features we've discussed: a global minimum (the ultimate goal), several local minima (traps for the optimizer), and paths an optimizer might take. Concepts like "Momentum" are techniques designed to help the optimizer escape local minima and navigate this complex terrain, which we will explore later.

Concrete Examples of Loss Surfaces

To make these ideas less abstract, let's look at the plots of specific two-variable functions that create these different types of landscapes. We can visualize them as 3D surface plots and 2D contour plots. A contour plot is like a topographic map: it shows lines of equal "elevation" (equal loss).

The following article provides excellent visualizations for three canonical landscape types.

Convexity, Curvature, and Why Your Optimizer Gets Lost

This article uses code and plots to demonstrate what convex, non-convex, and saddle landscapes look like.

Please read through the first three main sections of the article, focusing on the visualizations. Convex landscape: Look at the 3D plot and contour map for f(x, y) = x^2 + y^2. Notice its perfect bowl shape. Saddle landscape: Examine the plots for f(x, y) = x^2 - y^2. Observe how it curves up along one axis and down along the other. Non-convex landscape: Study the plots for Himmelblau's function. This is a classic example of a complex surface with multiple local minima. Notice how different starting points on the gradient descent path end up in different valleys.

Let's recap what these examples show:

  1. The Convex Bowl: The function creates a perfect paraboloid. It has a single global minimum at and no other critical points. No matter where you start on this surface, rolling "downhill" will always lead you to the same bottom point. Models like linear regression with Mean Squared Error loss have convex loss landscapes, which guarantees that an optimizer can find the one true best solution.

  2. The Saddle: The function is the classic example of a saddle point at . If you are on the -axis, the point looks like a maximum. If you are on the -axis, it looks like a minimum. Research has shown that in the extremely high-dimensional spaces of neural networks, saddle points are far more common than local minima and are a primary obstacle for optimization.

  3. The Rugged Mountains: Himmelblau's function is an example of a non-convex function. It has multiple local minima. As the plots in the article show, where your optimizer ends up depends entirely on where it starts. This is characteristic of most deep learning problems. While finding the global minimum is often intractable, the goal is to find a "good enough" local minimum that corresponds to a well-performing model.


From 2D Intuition to High-Dimensional Reality

How does this 2D and 3D intuition help when we're dealing with millions of parameters? We can't draw a million-dimensional graph.

The crucial insight is that the mathematical definitions of minima, maxima, and saddle points do not depend on our ability to visualize them. The concepts remain the same. We use the low-dimensional landscapes as a mental scaffold. When we talk about an optimizer "getting stuck in a saddle point" in a neural network, we are using this 2D analogy to describe a mathematical situation happening in a million-dimensional space.

As mentioned in the "Analyzing Machine Learning Loss Surfaces" article, a common technique in research is to visualize 2D cross-sections of these high-dimensional landscapes. Researchers will pick two interesting directions in the parameter space and plot the loss surface along that 2D plane, creating a tangible visualization that helps diagnose the behavior of models and optimizers.


Conclusion

In this lesson, we have made the crucial conceptual leap from thinking about machine learning as just code and algorithms to visualizing the process of training as a journey across a vast, complex landscape. This geometric perspective is fundamental to understanding why optimization algorithms are designed the way they are.

Key Takeaways:

  • A model's loss function can be visualized as a loss landscape, a surface where the location represents the model's parameters and the height represents the model's error.
  • The goal of training is to navigate this landscape to find the global minimum, or at least a very deep local minimum.
  • The key features of this landscape are minima (valleys), maxima (peaks), and saddle points (flat regions that are minima in some directions and maxima in others).
  • The shape of the landscape determines the difficulty of optimization. Convex landscapes (like a simple bowl) are easy to optimize, while non-convex landscapes with many local minima and saddle points (typical of deep learning) are much harder.

We now have a map of the terrain. The next question is, how do we get our compass? In the next lesson, we will learn to compute partial derivatives and assemble them into the gradient vector. The gradient is the mathematical tool that tells us, at any point on the landscape, which direction is steepest uphill, and therefore which way is steepest downhill.