Skip to content
GenAI Learn/Neural Networks From First Principles
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Gradient Descent, Intuitively, Applied to Networks

6 min read

You'll learn to

  • -Reconnect the landscape and gradient-descent intuition from the Math module to a real network's millions of parameters
  • -Understand why this landscape is far higher-dimensional than the earlier analogy suggested
  • -Implement gradient descent for a 2-parameter linear model and watch the loss decrease over iterations

The Math module introduced gradient descent as walking downhill on a loss landscape in the fog. This chapter reconnects that picture specifically to neural networks, where the landscape is far stranger than the simple 2D or 3D pictures anyone can actually visualize.

A Landscape With Millions of Dimensions

Every single weight and bias in the network is one dimension of the loss landscape. A modest neural network might have hundreds of thousands of these parameters. Large language models have billions. The landscape gradient descent walks across is not a hill you could picture in three dimensions. It is a shape with as many dimensions as the network has parameters, a genuinely different regime than the simple analogy suggests, even though the core idea (find the downhill direction, take a step, repeat) stays exactly the same.

One Step, Millions of Adjustments at Once

Each single step of gradient descent computes a downhill direction and adjusts every one of those millions of weights simultaneously by a small amount, based on the current training examples. Then it repeats, over and over, often millions of steps in total, gradually reshaping the entire network from random noise into something that produces useful predictions.

The smallest possible version of this, two parameters instead of millions, is already enough to see the whole loop in action. The code below fits a line, slope w and intercept b, exactly like linear regression's closed-form solution from the Classical ML Toolbox module, using gradient descent instead of the closed-form formula, and prints the loss shrinking step by step.

Gradient descent fitting a line, two parameters updated together, every step

You do not need to picture a million-dimensional landscape to have working intuition here. The core mental model from the Math module, find downhill, take a small step, repeat, is exactly correct, and the two-parameter version above is not a simplified toy. It is the literal same algorithm real networks use, just with two dimensions instead of millions.

Batches: Not Looking at All Data at Once

In practice, each step of gradient descent typically looks at only a small batch of training examples at a time rather than the entire dataset, computing the downhill direction from just that batch before moving to the next one. This makes each step far cheaper to compute and, somewhat counterintuitively, often trains better in practice than using the full dataset for every single step. This variant is called mini-batch gradient descent, or with a batch size of exactly one, stochastic gradient descent.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

ScaleDojo Logo
Initializing ScaleDojo