Concepts / How Neural Networks Learn: Loss Functions and Optimization

How Neural Networks Learn: Loss Functions and Optimization

A common intuition in machine learning is that a bigger network should be able to learn more.

  • Programming

A Bigger Network That Fails to Learn

A common intuition in machine learning is that a bigger network should be able to learn more. Adding layers gives a network more room to represent complicated relationships. In practice, however, stacking many layers together does not simply make learning easier. Training a deep network can become unreliable, and the network may fail to learn the patterns that would let it make accurate predictions.

The source material names two specific problems behind this difficulty: vanishing gradients and exploding gradients. This article builds up to those problems step by step and then shows why they create the need for layer normalization. The goal is to understand the problem first, so that layer normalization is understood as a response to something real rather than an arbitrary addition to a network.

What do you think happens?

Suppose a signal is multiplied by 0.5 at each layer, and this happens across 10 layers in a row. Roughly how large is the signal after the 10th layer compared with where it started?

  • About half of its original size

  • About one tenth of its original size

  • About one thousandth of its original size

  • About the same size as before

Reveal answer
Answer:

About one thousandth of its original size. Multiplying by 0.5 ten times gives 0.5 raised to the tenth power, which equals 1/1024, roughly 0.001.

Each layer applies the same kind of multiplication, so the effects compound. Ten halvings shrink the signal by a factor of $0.5^{10} = \frac{1}{1024}$. Small per-layer changes do not stay small when they are repeated many times.

Training as a Search for Low Loss

Training a neural network is a search. The network has a set of parameters, called weights, and the learning process looks for a particular set of weights that minimizes the loss function. The loss function scores how far the network's outputs are from what they should be. A lower loss means the network is doing a better job on the training data.

The adjustments to the weights during this search are guided by gradients. A gradient carries information about how the loss would change if a weight were nudged slightly. In other words, gradients are the signals that tell the learning process which way to move the weights. You do not need a deep mathematical understanding of gradients to follow this material; the working idea is enough.

Key point

Because gradients guide every weight adjustment, a problem with their size directly disrupts the search for good parameters. If the signals are too weak or too strong, the search itself goes wrong.

Tracing a Signal Through Stacked Layers

Picture a signal that is multiplied by a number, passed through a layer, and then multiplied again by the same kind of number at the next layer. The question is what happens to the size of that signal after many such steps. This compounding effect is the core reason deep stacks behave differently from shallow ones. Vanishing and exploding gradients are two versions of this same compounding problem, appearing in the training signals rather than in a single forward pass.

Output layergradient 1Layer 9gradient 0.5Layer 8gradient 0.25Layer 1much smallermultiply by 0.5multiply by 0.5repeated multiplication
Starting at the output and moving backward through the layers, how does the gradient change at each step when every layer multiplies it by 0.5?

Read the diagram from the output toward the input. The gradient starts at full strength at the output and is weakened by a factor at every layer it passes through. By the time it reaches the earliest layers, it has been multiplied many times, so it is far smaller than where it began. The same arithmetic can work in the other direction, which is the origin of the exploding case described below.

Vanishing Gradients and Early Weights

The source identifies vanishing or exploding gradients as the problem that can make training deep networks challenging. In the vanishing case, the gradient reaching earlier layers becomes so small that those weights are hardly adjusted, so the network struggles to learn. The weights near the input receive almost no useful correction, even though the search for lower loss depends on them.

Worked example

Compounding a shrinking factor

Each layer multiplies the gradient by 0.5. Starting from a gradient of 1, how large is the gradient after 10 layers of this repeated multiplication?

  1. 1
    Identify the per-layer factor

    The factor applied at each layer is 0.5, which is below 1, so each step makes the gradient smaller.

  2. 2
    Write the compounded product

    After 10 layers the gradient is multiplied by 0.5 a total of 10 times, giving $0.5^{10}$.

  3. 3
    Simplify the expression

    Since $0.5 = \frac{1}{2}$, we get $0.5^{10} = \frac{1}{2^{10}} = \frac{1}{1024}$.

  4. 4
    Interpret the result

    The gradient at the earliest layer is about 0.001 of its starting size, so those weights receive very little adjustment.

Result

The gradient shrinks to about one thousandth of its original size, so early weights are hardly adjusted. This is the vanishing pattern.

The example is deliberately simplified. Real networks do not multiply by one fixed number, but the compounding logic is the same. It explains why depth amplifies whatever tendency a gradient has to shrink.

Exploding Gradients and Erratic Updates

In the exploding case, the gradient grows so large that the weight adjustments become erratic and destabilize training. Instead of nudging weights toward lower loss in a controlled way, the updates swing wildly. The search does not settle down, and the network cannot reliably find a set of weights that minimizes the loss function.

Reverse the factor in the previous example to 2 and the same arithmetic gives about 1024 at the first layer, which is the exploding pattern. The gradient is not small but enormous, and each weight update inherits that size. Both failure modes come from the same compounding arithmetic; only the direction differs.

Comparing the Two Gradient Failures

Factor below 1for example 0.5Factor above 1for example 2Signal shrinks towardzeroafter many layersSignal grows rapidlyabout 1024 in the exampleEarly weights barelychangelearning strugglesErratic weightupdatestraining destabilizesrepeated multiplicationweights hardly adjustedrepeated multiplicationupdates swing wildly
When each layer multiplies the gradient by a factor below 1 versus above 1, how do the two outcomes differ after many layers?
FeatureVanishing gradientsExploding gradients
Per-layer factor in the exampleBelow 1 (0.5)Above 1 (2)
Gradient size at early layersBecomes very smallBecomes very large
Effect on early weightsHardly adjustedAdjusted erratically
Effect on trainingLearning struggles to progressTraining becomes unstable
Shared causeCompounding across layersCompounding across layers

Both failures are problems with gradient size that flow through the network during training.

Symptoms of Unstable Training

The source describes the effects of these gradient problems in three connected ways. First, they lead to unstable training dynamics, meaning the training process does not progress in a steady, predictable manner. Second, they make it difficult for the network to effectively adjust its weights. Third, as a result, the learning process struggles to find a set of weights that minimizes the loss function.

The consequence the source emphasizes is a failure to learn the underlying patterns in the data. The network may then be unable to make accurate predictions or decisions. This is a failure of learning itself, not merely a slower version of learning. A network with unstable gradients can end up with weights that do not capture the structure the task requires.

  • Training does not progress in a steady, predictable manner
  • Weights are difficult to adjust effectively, especially in early layers
  • The loss does not reliably decrease toward a good set of weights
  • The trained network fails to capture patterns needed for accurate predictions

Why Layer Normalization Is Needed

Putting the pieces together gives the motivation for layer normalization. Deep networks can suffer from unstable training, and the gradients that guide weight updates can shrink or grow as they pass through layers. Any remedy has to address that instability so that the network can again adjust its weights and find parameters that minimize the loss.

The need, then, is for stability in the training signal. Layer normalization is introduced to answer that need. Understanding the vanishing and exploding problems first makes the technique a response to a concrete failure rather than an unexplained step in the network's design.

Mistakes Learners Make About Depth

  • Common mistake

    Assuming a deeper network always learns more

    What it looks like

    Adding many layers to a network that is already struggling and expecting accuracy to rise.

    Why it fails

    Depth adds representational room, but stacking layers can make training unreliable and prevent the network from learning the patterns in the data.

    Do this instead

    Treat depth as a capacity that only helps if gradients remain stable enough to adjust the weights.

  • Common mistake

    Thinking vanishing and exploding gradients are separate problems with separate causes

    What it looks like

    Trying to fix a network with tiny gradients using the same reasoning you would use for a network with huge gradients.

    Why it fails

    Both come from the same compounding of per-layer effects; the direction of the factor decides which failure appears.

    Do this instead

    Ask whether the per-layer factor sits below or above 1 to identify which failure is happening.

  • Common mistake

    Reading the gradient as a number that only matters for speed

    What it looks like

    Believing a vanishing gradient just makes training slower but still reaches the same result.

    Why it fails

    The source describes a failure of learning itself: the network may never capture the underlying patterns.

    Do this instead

    Recognize that gradient size problems can change the final outcome, not just the pace.

  • Common mistake

    Believing the loss function is the same thing as the gradient; the loss scores the outputs, while the gradient describes how the loss changes when a weight is nudged

Checking Your Reasoning on Gradient Size

MEDIUM

Suppose each layer in a deep network multiplies the gradient by 0.9, and there are 10 layers. Is the gradient at the earliest layer likely to vanish or explode, and roughly what fraction of its starting size does it keep? Give the approximate value of $0.9^{10}$.

Hints
  • Ask whether 0.9 is below or above 1 before doing any arithmetic.
  • Multiply 0.9 by itself 10 times, or estimate it as a fraction of the starting size.
  • A result near 0.35 means the signal keeps about a third of its size, which is a milder shrinkage than the 0.5 example but still a compounding effect.

Key Takeaways

  • Training deep networks is a search for weights that minimize the loss, guided by gradients that signal how to adjust each weight.
  • Repeated multiplication across many layers compounds small per-layer changes, which is the shared cause of vanishing and exploding gradients.
  • Vanishing gradients leave early weights barely adjusted, while exploding gradients produce erratic weight updates; both lead to unstable training.
  • Unstable training can stop a network from learning the underlying patterns needed for accurate predictions, which is the motivation for layer normalization.

Related concepts

More concepts starting with H