Recommended resources
The freely available resources this lesson will direct you to.
In our previous lesson, we established our primary tool for optimization: gradient descent, an iterative algorithm that follows the slope of the loss surface to find a minimum. We used the analogy of descending a mountain in the fog, but we treated all mountains as simple, bowl-shaped valleys. Today, we confront a more complex reality. The shape of the optimization landscape—its very topography—is a critical property that determines the difficulty of our search and the trustworthiness of our results.
This lesson addresses a fundamental dichotomy in optimization theory. Our learning outcome is to distinguish between convex and non-convex optimization problems and their implications for finding global minima. We will move from the intuitive notion of "bowls" versus "egg crates" to the rigorous mathematical definitions that underpin this distinction. Understanding this concept is crucial, as it explains why some machine learning models come with strong theoretical guarantees while others, particularly deep neural networks, require a host of sophisticated heuristics to train effectively.
The Great Divide: Convex vs. Non-Convex
At its heart, the distinction is about predictability and guarantees. A convex optimization problem is like having a single, perfectly smooth valley. No matter where you start, gradient descent will inevitably guide you to the one and only lowest point. A non-convex problem, however, is a rugged, mountainous terrain with countless valleys, ridges, and plateaus. Starting your descent from different locations might lead you to entirely different endpoints, many of which are merely local depressions far from the true, deepest canyon.
This video from Visually Explained provides an excellent introduction to the formal definitions of convexity in three key contexts: sets, functions, and the optimization problems themselves.
Convexity and The Principle of Duality
This video lays the groundwork by defining convexity from a geometric and functional perspective, which is essential for understanding its role in optimization.
Please watch the first three minutes of the video, covering the definitions (definitions). Pay close attention to how convexity is defined for: Sets: A line segment between any two points is contained entirely within the set. Functions: The region above the graph of the function (its epigraph) forms a convex set. Optimization Problems: The objective function and inequality constraint functions are convex.
The resource below, "Convex vs Non-Convex Optimization" from TensorTonic, will serve as our primary guide for this lesson. It offers a clear, comprehensive narrative from intuition to practical application.
Convex vs Non-Convex Optimization | TensorTonic
This article introduces the core concepts and their profound implications for machine learning.
First, read the introductory sections to build your intuition. Start with the introduction, which introduces the "bowl" vs. "egg crate" analogy. Then, read the section Why You Should Care, which connects this theoretical distinction to concrete ML models like Linear Regression and Neural Networks. Finally, review the formal definitions in the sections on Convex Sets and Convex Functions. Notice how the algebraic definition of a convex function, f(\lambda x + (1-\lambda)y) \le \lambda f(x) + (1-\lambda)f(y), is a formal statement that a chord connecting two points on the graph always lies on or above the function itself.
The Power of Convexity: Local is Global
The single most important consequence of a function being convex is this: any local minimum is also a global minimum. This is the guarantee that makes convex optimization so powerful and reliable.
Why is this true? For a differentiable function, a minimum occurs at a point where the gradient is zero. In a convex function, if the gradient is zero, the tangent plane at that point is horizontal. Because the entire function must lie on or above this tangent plane (a property called the first-order condition for convexity), that point must be the lowest point everywhere—a global minimum.
The Visually Explained video you watched earlier makes this exact point with a clear animation.
Convexity and The Principle of Duality
This segment beautifully illustrates the main implication of convexity for optimization.
Re-watch the segment from this point. The key insight is how convexity allows us to extrapolate from a local property (gradient is zero) to a global property (the point is a global minimizer).
In a non-convex setting, this guarantee vanishes. A point where the gradient is zero could be a global minimum, a local minimum, or a saddle point. Gradient descent, only seeing the local slope, can easily get "trapped" in a local minimum, believing it has found the solution when a much better one exists elsewhere.

Testing for Convexity: The Hessian Matrix
Intuition is useful, but we need a rigorous method to determine if a function is convex. For twice-differentiable functions, this test is provided by the Hessian matrix, which is the matrix of second-order partial derivatives.
Recall from multivariate calculus that for a function , the Hessian at a point is a matrix where the entry is .
The rule is as follows:
A function is convex if and only if its Hessian matrix is positive semi-definite (PSD) for all points in its domain.
A matrix is positive semi-definite if all of its eigenvalues are non-negative (). Geometrically, this means the function is "curving upwards" or is flat in every direction at every point. If all eigenvalues are strictly positive (), the function is strictly convex, which implies it has a unique global minimum.
Let's apply this to a core ML problem: linear regression with Mean Squared Error (MSE) loss.
The loss function is . As shown in the TensorTonic article, the Hessian of this loss function with respect to the weights is .
Convex vs Non-Convex Optimization | TensorTonic
This section provides the formal test for convexity and applies it to a key example.
Read the section The Hessian Test. The example of MSE loss is a cornerstone result showing why Ordinary Least Squares has a closed-form, globally optimal solution.
The matrix is a Gram matrix, which is always positive semi-definite. Therefore, the MSE loss function for linear regression is convex. This is a profound result: it guarantees that when we use gradient descent to train a linear regression model, we are descending a simple bowl and will find the single best set of weights.
The same convexity property holds for the loss functions of logistic regression and Support Vector Machines, which was a major reason for their dominance in machine learning for many years.
[PDF] Optimization for Large Scale Machine Learning
These slides from a presentation by Francis Bach provide a more formal, academic perspective on the role of convexity.
On slide 8, review the section titled Relevance of convex optimization. This summarizes the key benefits: easier algorithm design and the crucial "Global minimum vs. local minimum" distinction. Then, on slide 13, look at the summary under Convex optimization problems. This lists the conditions (convex loss, linear predictions) and consequences, and refers to the period from 1995-2020 as the "Golden years of convexity in machine learning."
The Non-Convex Jungle of Deep Learning
If convexity is so wonderful, why isn't everything convex? The moment we introduce the hierarchical, non-linear structure of a deep neural network, we shatter the guarantees of convexity. The loss surface of a neural network is an incredibly complex, high-dimensional, non-convex landscape.

Given that non-convex optimization is NP-hard in the general case, how is it that we can train these massive models at all? For a long time, the common fear was getting stuck in "bad" local optima. However, the modern understanding has evolved. Andrew Ng explains this shift in perspective in the following video.
The Problem of Local Optima (C2W3L10)
This video explains the modern view of optimization challenges in deep learning, shifting the focus from local optima to saddle points and plateaus.
Please watch these key segments: Old vs. New View: Introduces the idea that low-dimensional intuitions about local optima don't apply in high dimensions. Saddle Points: Explains why, in high-dimensional space, points with zero gradient are far more likely to be saddle points (curving up in some directions, down in others) than true local minima. Plateaus as the Problem: Identifies plateaus—vast, nearly flat regions—as a primary cause of slow training, and notes that advanced optimizers like Adam are designed to help navigate them.
The key insights about why deep learning works despite its non-convexity are:
- High dimensionality is a blessing: In a 20,000-dimensional space, for a point to be a local minimum, the loss function must curve upwards in all 20,000 directions. It's statistically much more likely that it will curve up in some and down in others, creating a saddle point which gradient-based methods can escape.
- Most local minima are good: Empirical and theoretical work suggests that for many large neural networks, most local minima have a loss value very close to that of the global minimum. The "bad" local minima that would trap a model in a state of poor performance are thought to be rare or located in parts of the landscape that are hard to reach.
- Plateaus and saddles are the main challenge: The primary difficulty in training isn't getting stuck in a bad valley, but rather getting bogged down on a vast, flat plateau where gradients are tiny, or oscillating around a saddle point. This is why optimizers with momentum and adaptive learning rates are essential.
Convex vs Non-Convex Optimization | TensorTonic
This final section of the article summarizes the challenges of non-convexity and the modern understanding of why deep learning is still trainable.
Please read the sections The Non-Convex Reality and Why Deep Learning Works Anyway. This reinforces the concepts from the video and provides a concise summary.
Conclusion
In this lesson, we have explored the critical distinction between convex and non-convex optimization, a concept that divides the world of machine learning into two paradigms.
Here are the key takeaways:
- Convex Optimization Problems feature a "bowl-shaped" loss surface where any local minimum is the global minimum. This guarantees that an algorithm like gradient descent can find the single best solution. Models like linear regression, logistic regression, and SVMs fall into this category.
- Non-Convex Optimization Problems have complex, rugged loss surfaces with multiple local minima, saddle points, and plateaus. There are no guarantees of finding the global optimum, and the final solution depends heavily on initialization and the optimization algorithm. Deep neural networks are archetypal non-convex problems.
- The Hessian matrix provides a formal test for convexity: if the Hessian is positive semi-definite everywhere, the function is convex.
- In modern deep learning, the primary optimization hurdles are not getting trapped in bad local optima, but rather navigating saddle points and escaping vast, low-gradient plateaus.
We now have a much more nuanced understanding of the optimization landscapes we are traversing. We know when our journey to the minimum is guaranteed and when it is a perilous expedition requiring sophisticated tools.
In our next lesson, we will introduce L1 and L2 regularization. These techniques intentionally modify the loss function, adding a penalty term that reshapes the optimization landscape. We will see how this can be used to control model complexity and prevent overfitting, effectively guiding the optimizer towards solutions that are not only low in loss but also more likely to generalize to new, unseen data.