Gradient Descent: How Direction, Step Size, and Noise Together Shape Learning
Starting from one-dimensional slopes and directional derivatives, work through one update by hand, then see why learning rate, curvature, mini-batch, momentum, and Adam change the training trajectory.
- Why is the negative gradient the locally steepest descent direction, not an "arrow pointing to the global minimum"?
- How can you derive the update rule from a first-order approximation and compute one parameter change by hand?
- What symptoms appear when the learning rate is too large, too small, or the curvature is uneven?
- Is the noise of mini-batch only a drawback? What do momentum and Adam change?
- How can you locate training faults using the loss, gradient norm, and update ratio?
ŷ=wx. Here w is the "multiplier" parameter the model needs to learn, the true pattern y=3x requires w=3; training starts from a deliberately wrong w=2. For the sample x=2,y=6, the model predicts 4, the half-squared loss L=½(ŷ−y)² is 2, and the gradient of the loss with respect to w is −4. This page answers: why subtract the gradient, and how far can one step actually go.1Why can you still descend while looking only at the slope under your feet?Intuition
If you cannot see the entire loss landscape and only know how much the current position tilts in each direction, how should you choose the next small step?
When there is only one parameter,derivativeis the slope at the current position: if the derivative is positive, a small step to the right will increase the loss, so you should try moving left; if the derivative is negative, the opposite is true.
Models usually have many parameters. The training program reads the parameters at the current position and the loss function, and when computing, first fixes the other parameters to find the slope in each dimension: we temporarily change only one of them and keep the others fixed, and the slope obtained is called this parameter'spartial derivative. Then arrange these slopes in order into a vector, and the output is thegradient. For example, the gradient of two parameters (3,−1) indicates: the loss rises quickly when the first parameter increases, and the loss decreases instead when the second parameter increases.
A gradient component with a larger absolute value indicates that the loss is more sensitive near that coordinate; but the gradient is not the entire map, so you cannot conclude from this that this parameter is more important, nor do you know where the distant minimum is.
Scroll horizontally to view the full diagram on small screens.
2Why the Negative Gradient Descends Fastest LocallyMath
Is “the gradient points in the direction of fastest ascent” a metaphor, or a conclusion that can be derived from formulas?
First understand in words: the gradient records, for each parameter, “how moving a little in the positive direction changes the loss.” Given the gradient at the current position and the allowed step length, we want to choose a unit descent direction; if we want each component's change to be as opposite as possible, we should move in the direction in which every component is negated, that is, the negative gradient.
The formula states this more precisely.θ represents all current parameters,Δθ represents the small step to be taken,J is the training objective. When looking only at a very small local region:
J(θ+Δθ) ≈ J(θ)+∇J(θ)·Δθ
Dot productcan be understood as “how much this step projects onto the gradient direction”: when in the same direction, it is positive and the loss tends to increase; when in the opposite direction, it is negative and the loss tends to decrease. If we fix the Euclidean length of one step to ε, first use the dot product to estimate the loss change caused by each candidate displacement; walking completely opposite to the gradient makes the dot product most negative, so within this first-order approximation it descends the most:
||∇J|| is the length of the gradient vector; dividing by it keeps only the direction, and multiplying by ε determines how long this step is. This conclusion only guarantees that “a step near the current position, with the same short length” is the steepest; it does not guarantee reaching the global minimum.
Optional proof: why does the opposite direction minimize the dot product?
The Cauchy–Schwarz inequality gives ∇J·Δθ ≥ −||∇J||·||Δθ||. When the two vectors point in completely opposite directions, equality holds; fix ||Δθ||=ε afterwards, the right-hand side is the smallest possible first-order change.
3How does the update rule turn a local approximation into actual parameter updates?Math
Now that we have a direction, how do we write it as a step actually executed in the training loop?
The update rule turns the abstract direction into an action the training program actually executes: it takes the current parameters, the current gradient or the gradient estimated from a mini-batch, and the learning rate, and outputs the next step's parameters. Let's translate this formula term by term:
tis the current update count;θtis all parameters before the update,θt+1is the parameters after the update;gtis the gradient at the current position, or the gradient estimated from a mini-batch of samples;ηtis read as eta, and islearning rate, controls how far to go along the gradient this time;- The preceding minus sign means moving in the direction opposite the gradient.
The program first computes the gradient, then scales it by the learning rate, and finally subtracts this change from the old parameters. Now let's go back to the example defined at the top of the page:w is the multiplier parameter, with ideal value 3 and current incorrect initial value 2. The current gradient is −4, and taking learning rate η=0.1:
| Quantity | Before update | After update |
|---|---|---|
| Parameter | w=2 | 2−0.1×(−4)=2.4 |
| Prediction | 2×2=4 | 2.4×2=4.8 |
| Loss | ½(4−6)²=2 | ½(4.8−6)²=0.72 |
The loss dropping from 2 to 0.72 shows that this local move was effective; but a single-step decrease does not prove that subsequent steps will be stable, nor that the model has learned the true pattern or can generalize to new samples. If we take η=1, we get w=6, prediction 12, and loss 18: the same correct gradient was ruined by an overly large step size.
4Why does the learning rate have a stable upper limit?curvature
What exactly controls “small enough,” and why does a steeper bowl wall make it easier to cross the valley bottom in one step?
curvatureDescribes how quickly the slope changes. On flat ground, moving a little farther does not change the slope much; on a steep, curved bowl wall, moving a little farther can cause the slope to reverse sign quickly, so one step crosses from the left side of the valley bottom to the right. The learning rate must be small enough that the local slope remains informative.
A running example can give a fully concrete answer. Here w is still the multiplicative parameter the model needs to learn,J(w) is the loss corresponding to the parameter value,g(w) is the gradient of the loss with respect to w,η is the learning rate. Substitute ŷ=2w and y=6 into the half-squared loss:
Gradient g(w)=4(w−3)
The optimal parameter is w*=3. Define the current parameter's distance from the optimum as et=wt−3, after one update step:
Here, taking the local curvature and learning rate as inputs, we first obtain the error multiplier 1−4η, then check whether its absolute value is less than 1: only when the absolute value is less than 1 will it shrink gradually; a negative multiplier means each step crosses the optimum and oscillates back and forth on both sides.
| Learning rate η | Error multiplier 1−4η | One update from w=2 | Meaning |
|---|---|---|---|
| 0.1 | 0.6 | w=2.4, loss 0.72 | Does not cross the valley bottom, approaches stably |
| 0.25 | 0 | w=3, loss 0 | In this ideal quadratic example, it reaches the optimum exactly in one step |
| 0.4 | −0.6 | w=3.6, loss 0.72 | Crosses the valley bottom, but the error still shrinks |
| 0.5 | −1 | w=4, loss is still 2 | Oscillates with equal amplitude on both sides, no longer approaches |
| 1 | −3 | w=6, loss 18 | Error magnified, training diverges |
A general quadratic function is written as J(w)=½a(w−w*)², where a controls the curvature, and the stability condition is 0<η<2/a. The larger a is, the lower the safe upper limit. Real neural networks are not a perfect quadratic bowl, and curvature also changes with position, so this interval explains a local stability boundary, not a fixed license valid for all training processes.
5Why narrow valleys make ordinary gradient descent move in a zigzagGeometry
A “narrow valley” is not a new algorithm, but a loss landscape where two parameter directions differ greatly in steepness.
Imagine the loss is determined by two parameters; connecting points with equal loss gives a set ofcontour lines. If the contour lines are close to circular, the steepness is similar in all directions; if they are elongated into a long ellipse, it is like a narrow valley:
- The direction crossing the valley is steep:A small change in the parameters causes a large change in the loss;
- The direction along the valley floor is flat:You have to go a long way for the loss to change noticeably;
- The lowest point is deep in the valley:Training really wants to move forward along the valley floor, but the gradient is often dominated by the steep slopes on both sides.
Starting from the current loss surface and starting point, ordinary gradient descent outputs a parameter trajectory; if the curvature differs greatly between directions, this trajectory can explain the phenomenon of “the gradient is not zero, the loss is decreasing, but training is still slow.”
Scroll horizontally to view the full diagram on small screens.
When computing updates, the gradient is first dominated by the steep direction. A single learning rate must accommodate the lateral steep slope and cannot be too large; but the same small learning rate is too small when used in the valley-floor direction, so the trajectory oscillates laterally back and forth while slowly moving toward the lowest point. The momentum in Section 7 will remember the long-term consistent valley-floor direction and partially cancel the left-right alternating swings.
The more pronounced the zigzag, the more severe the scale mismatch usually is, but the picture is affected by parameter coordinates and scaling, so a two-dimensional schematic cannot be used alone to diagnose a real high-dimensional network. High-dimensional here means that there are many parameter coordinates; mentioning it is to remind the reader that two-dimensional contour lines can only help understand the local mechanism.
6Why mini-batch deliberately uses imprecise gradientsRandomness
If full-batch data can provide more accurate gradients, why does deep learning usually estimate with a small batch of samples?
Full-batch gradientFirst compute the gradient of each sample in the training set, then average, and only then update the parameters once.mini-batch(small batch) takes as input a randomly drawn set of samples and the current parameters, computes the average gradient for only this set, then updates immediately; it repeats this process with a new batch. As long as the sampling method is reasonable, the mini-batch gradient will approximate the full-batch gradient only in the long-term average, but any single step will have sampling error, so the trajectory jitters.
For example, if the training set has 1 million samples and the batch size is 256: the full-batch method must process all 1 million samples before providing feedback once; the mini-batch method can provide feedback about 1,000,000/256≈3906 times. Both are similar in per-epoch sample computation, but the waiting time for a single update and the feedback frequency are completely different.
Here,hardware efficiencymainly refers to how many samples can be processed per unit time, whether the compute units are fully utilized, and how much GPU memory and inter-device communication are needed:
- If the batch is too small, the GPU only does a little work each time, the overhead of launching and moving data accounts for a high proportion, and many compute units sit idle;
- As the batch size increases, matrix operations are easier to parallelize, and per-unit-time throughput usually improves;
- If the batch is too large, it will occupy more GPU memory, and multiple devices will also need to synchronize many gradients; full-batch data usually cannot fit in GPU memory at all, and waiting for a single update is also very long.
| Batch size | Gradient noise | Hardware performance | Training implications |
|---|---|---|---|
| Very small | Large | Fast feedback, but the GPU may not be fully utilized | Frequent and jittery updates; learning rate is usually smaller |
| Medium | Controllable | Parallelism and GPU memory usage usually more balanced | The most common throughput/noise trade-off |
| Very large | Small | High sample throughput, but GPU memory and synchronization overhead increase | Fewer updates per epoch; learning rate needs retuning |
| Full batch | No sampling noise | A single update requires reading all data, possibly with cross-device synchronization | Each step is exact, but that does not mean the total time to reach the target is shorter |
Large jitter between batches indicates higher estimation variance, but does not necessarily mean training has failed; noise can sometimes help escape local flat regions, but it should not be mythologized as automatically finding better solutions. Batch size is still jointly constrained by GPU memory, throughput, device synchronization, and task generalization; after changing the batch size, the learning rate, number of updates, and validation performance should all be re-compared.
7From SGD to Momentum and AdamOptimizer
The optimizer is the rule for "how to update parameters after obtaining the gradient"; the difference among the three methods is whether they remember history.
These rules all take the current gradient and hyperparameters as input and output new parameters, to mitigate batch noise, back-and-forth oscillation, and differences in parameter scale.SGD is stochastic gradient descent, i.e.,stochastic gradient descent. Historically, "random" could refer to using only one random sample at a time; in modern training, updates that use a random mini-batch are also commonly called SGD. At each step it takes the current mini-batch gradient gt directly updates:
SGD is simple and uses little extra memory, but if the current batch is noisy, the direction will jitter along with it; in narrow valleys it also tends to oscillate left and right.
Momentum adds a "velocity" v, preserving a decaying average of past gradients:
θt+1=θt−ηvt
β controls how long the memory is. Components that point in the same direction over consecutive steps accumulate; components that alternate left and right cancel each other out. So it can accelerate along the valley floor and reduce lateral oscillation.
Adam also keeps two kinds of history:m is the moving average of the gradient, which can be understood as the recent direction;v is the moving average of the squared gradient, which can be understood as the recent scale. During the update, use m/v to adjust the effective step size of each parameter: coordinates with larger long-term gradients are appropriately scaled down, and those with smaller gradients are relatively scaled up. The formal implementation also includes ε to prevent division by zero and bias correction in the early stages of training.
| Method | What it remembers | Main effect | Cost or boundary |
|---|---|---|---|
| SGD | Does not save gradient history | Simple rule, low extra memory | Easily affected by batch noise and narrow valleys |
| Momentum | Recent gradient direction | Reinforce persistent direction, cancel back-and-forth oscillation | Still requires choosing learning rate and memory coefficient β |
| Adam | Recent direction and squared gradient scale | Adaptively scale step sizes for different parameters | Requires extra state for each parameter; default settings do not guarantee the best results for all tasks or generalization |
A smoother or faster training curve only shows that optimization is smoother under the current recipe; it does not mean Adam necessarily generalizes best.
8What warmup, decay, and gradient clipping each solveEngineering
They respectively control the early stage of training, the later stage of training, and occasional anomalous steps; they are not three interchangeable names.
In actual execution, warmup and decay produce the actual learning rate for the current step based on the current training step and the learning rate schedule; clipping reads the gradient norm and then decides whether to shrink it. The three do not modify the same quantity: the first two modify the learning rate, and clipping modifies the gradient for that step.
When observing curves, the early training no longer surges, late-stage oscillation weakens, and occasional spikes are clipped—these respectively indicate that the corresponding mechanisms are taking effect. Below, they are broken down in execution order.
| Method | How to execute | What to look for that indicates it is working | Failure boundary |
|---|---|---|---|
| Warmup | At the start of training, it first outputs a very small actual learning rate, then gradually increases it to the target value over the first several steps, allowing time for the parameters and Adam's historical statistics to stabilize. | Training no longer suddenly diverges in the first few steps, and the first batch of updates no longer makes large, abrupt moves. | It only protects the beginning of training; it cannot fix a learning rate that is too large over the long term. |
| Learning rate decay | In the early stage of training, a larger step size is used to find a direction; in the later stage, the step size is gradually reduced for fine adjustment. Cosine decay is only one kind of schedule that decreases along a cosine curve. | After approaching a low-loss region, the late-stage back-and-forth oscillation near the low point weakens. | It cannot fix incorrect gradients or an incorrect objective. |
| Gradient clipping | First compute the gradient vector's norm; if it exceeds the threshold C, scale it down proportionally only in that case, and then pass the constrained gradient to the optimizer. | Update spikes caused by an anomalous batch are clipped; you should also record how many steps trigger clipping and how much is clipped each time. | It cannot replace proper learning rate selection, nor can it eliminate the root cause of persistent exploding gradients. If it is triggered at nearly every step or the loss continues to diverge, continue to check anomalous batches, numerical overflow, and model structure. |
9How to Diagnose a Training Stall Layer by LayerWorked Example
When loss fails to decrease, first identify where in the chain the signal is broken; don't immediately switch to Adam and hope for luck.
This diagnostic process breaks “loss not decreasing” into verifiable failure hypotheses. It takes small-sample experiments, predictions and labels, gradient norms, update ratios, and validation curves, progressively narrowing the search across data, computation chain, step size, optimizer, or target misalignment:
- First, run the “small-sample memorization test”:Give the model only a few dozen samples and train repeatedly until it almost memorizes them. If it cannot even do that, the problem is usually not generalization but a disconnect in the data, loss, or update chain.
- Check predictions and loss inputs:Directly print the inputs, targets, predictions, and per-sample loss for a few samples to confirm that labels are not misaligned and that positions requiring loss computation have not been entirely masked out.
- Check the gradient norm:The gradient norm is the length of the vector composed of all parameter gradients. When it stays at 0, check whether parameters are 'frozen' (excluded from updates) or whether the computation chain has been accidentally disconnected; when a huge spike suddenly appears, check for abnormal batches and numerical overflow.
- Check how large the actual update is:Compute
||Δθ||/||θ||, that is, “length of the parameter change in this step ÷ length of the parameters themselves”. If the gradient is non-zero but this ratio is close to 0, it indicates the learning rate is too small, numerical precision has swallowed the change, or the optimizer is not updating the intended parameters. - Run a learning rate sprint:Train for only a few steps with multiple learning rates from small to large to find the range between 'starts to decrease clearly' and 'starts to oscillate or diverge', rather than running full experiments directly.
- Then compare optimizers and schedules:Fix the model, data order, batch size, and number of training steps, and change only one factor at a time among SGD, momentum, Adam, or the learning rate schedule.
- Finally, look at independent validation:A faster decrease in training loss only means the surrogate objective is being optimized faster; the stopping point should still be determined by data not involved in updates and by real task metrics.
This sequence first proves the model can memorize small samples, then verifies loss inputs and gradients, then checks actual updates, and only finally compares optimizers and independent validation. An anomaly in one item only narrows the search and does not automatically prove the sole root cause; when log metrics are defined incorrectly, random fluctuations, or multiple faults coexist, you still need controlled comparison experiments.
10Connecting the entire causal chainSynthesis
From local slope to reliable training, how does each link constrain the next?
- The loss turns current prediction quality into a scalar.
- The gradient gives the local sensitivity of that scalar to each parameter.
- The negative gradient makes the first-order approximation decrease fastest under the given geometry.
- The learning rate turns the direction into an actual distance and must respect local curvature.
- Mini-batch trades noisy estimates for more frequent, parallelizable updates.
- Momentum, preconditioning, and scheduling improve the trajectory and step sizes at different stages.
- Diagnostic metrics confirm that gradients, updates, and numerical states are genuinely valid.
- Independent validation determines whether this optimization trajectory also improves unseen data and real tasks.
11Common MisconceptionsDisambiguation
| Misconception | More accurate statement |
|---|---|
| The gradient points to the global optimum | It only describes the local first-order variation at the current position. |
| The negative gradient makes the true loss decrease at every step | When the step size is too large or the gradient is noisy, a single step can increase. |
| Backpropagation is gradient descent | The former computes the gradient; the latter uses the gradient to update the parameters. |
| A larger batch has more accurate gradients, so it is definitely better | You must also consider update frequency, compute, learning rate retuning, and generalization. |
| Adam is always better than SGD | Convergence speed, memory, stability, and final generalization depend on the task and recipe. |
12Check if you really understandSelf-test
From local slopes and concrete hand calculations to batch and optimizer choices; answers must explain the symbols or mechanisms.
- What are partial derivatives and gradients, respectively? Why is the gradient not a map to the global minimum?
- When looking only at the local first-order approximation, why does a fixed-length step along the negative gradient decrease the most?
- In the running example, when the learning rate is 0.25, after one step
w, what are the prediction and loss? - In
J(w)=2(w−3)²in, why does η=0.4 cross over the optimum yet still converge, while η=1 diverges? - What specific curvature relationship between the two directions does the “narrow valley” represent? Why does the trajectory proceed in a zigzag?
- What specifically does the “better hardware efficiency” of mini-batch refer to? Why is a larger batch not always better?
- What historical information do SGD, Momentum, and Adam remember, respectively? How do they change the trajectory in a narrow valley?
- Which phase of training or which type of anomaly do warm-up, learning rate decay, and gradient clipping address, respectively?
Reference answers
- A partial derivative is the loss slope corresponding to a change in one parameter while other parameters are held fixed; the gradient is a vector composed of all partial derivatives. It only describes first-order changes near the current position and does not know distant terrain, obstacles, or the global minimum.
- The local change in loss is approximated by the dot product of the gradient and the displacement. For a fixed Euclidean length, making the displacement exactly opposite the gradient minimizes that dot product, so the first-order prediction decreases the most.
- The current gradient is −4, so
w=2−0.25×(−4)=3; the prediction is 6, and the half-squared loss is 0. In this ideal one-dimensional quadratic example, it reaches the optimum in exactly one step. - The error multiplier is
1−4η. When η=0.4, the multiplier is −0.6; the negative sign means crossing to the other side, but the absolute value 0.6<1, so the error shrinks. When η=1, the multiplier is −3, and the error is magnified three times per step, so it diverges. - The direction across the valley has large curvature, and the direction along the valley floor has small curvature. A uniform learning rate is limited by the steep direction; the gradient is often dominated by the steep slope, so it swings left and right noticeably and advances slowly along the valley floor.
- It refers to per-unit-time sample throughput, compute-unit utilization, GPU memory, and communication overhead. Small batches may not fully utilize the GPU; overly large batches occupy GPU memory, make synchronization expensive, and result in fewer updates per round, so a trade-off is needed among throughput, noise, and feedback frequency.
- SGD uses only the current mini-batch gradient; Momentum keeps recent directions, accumulating persistent components and canceling alternating oscillations; Adam also keeps a squared gradient scale, adjusting effective step sizes for different parameters. They improve trajectories but cannot change or repair an incorrect objective.
- Warm-up gradually increases the learning rate in the early stage of training; decay reduces the step size in the later stage to reduce oscillation; clipping limits occasional huge gradients to prevent a single-step explosion. The three cannot replace one another.
13Concept Dependencies and Further LearningPath
| Direction | Next reading | Key question |
|---|---|---|
| Where the gradient comes from | Backpropagation | How can a single backward pass yield gradients for all parameters? |
| What defines the objective | Loss Function | Is the scalar optimized locally consistent with the true objective? |
| How step size changes over time | Optimizers and Learning Rate Schedules | What do warm-up, decay, and AdamW each address? |
| Why deep-network gradients behave abnormally | Vanishing Gradient Problem, Residual Connection | How does network architecture change gradient paths? |
| Trains well but validates poorly | Overfitting | Why doesn't optimization success guarantee generalization success? |
- Deep Learning — Numerical Computation: used to check derivatives, gradients, curvature, condition number, and local descent.
- Deep Learning — Optimization for Training Deep Models: used to check mini-batch stochastic optimization, momentum, and training diagnostics.
- Practical Recommendations for Gradient-Based Training of Deep Architectures: used to check SGD, batch size, learning rate, and practical recommendations.
- Adam: A Method for Stochastic Optimization: used to check first moment, squared gradient second moment, and adaptive updates.
Figures, numerical examples, and diagnostic chains are original organization of this project.