Search

Search papers and lessons

Deep Supervision

Level 3 · 12 min read

Deep supervision attaches extra loss terms to intermediate layers of a network, so early layers receive a direct training signal instead of waiting for gradients to travel back from the output.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

In a deep network, the training signal is computed at the output and passed backwards one layer at a time, so the layers near the input can receive weak or distorted updates. Deep supervision adds a small prediction head and a loss term at one or more intermediate layers, so those layers are trained directly against the target. The total training loss is the output loss plus these auxiliary losses.

Builds on: Vanishing Gradient Problem, Backpropagation, Loss Function · Leads to: Hierarchical Reasoning Model

Where it's used

Image classification networks from 2014. GoogLeNet, a deep convolutional network with many parallel blocks, added two auxiliary classifiers partway through the network. They were trained with their own loss during training, and they were removed at inference time, so the final model used only the main output. The authors used them to help gradients reach the earlier layers of a very deep model.

Deeply-supervised nets for classification. Lee and colleagues added a companion objective to each hidden layer of a Convolutional Neural Network, alongside the output loss. The paper reports that this makes the hidden layers more discriminative and helps training when gradients explode or vanish.

Recurrent and iterative reasoning models. When a Recurrent Neural Network is unrolled over many steps, the signal from the final answer has to travel through all of them. Some recent models for multi-step reasoning place a supervised loss at each step as well, and Hierarchical Reasoning Model builds on this idea. The reader will meet the same mechanism there: the intermediate outputs are trained, not only the last one.

How it works

Take a tiny Feedforward Neural Network with one neuron per layer and no non-linear activation, so every number can be checked by hand. The input is x=1x = 1 and the target is t=1t = 1. There are three weights, w1w_1, w2w_2 and w3w_3, all starting at 0.50.5. The first hidden value is h1h_1, the second is h2h_2, and the output is yy.

  1. Forward pass. h1=w1x=0.5h_1 = w_1 x = 0.5. Then h2=w2h1=0.25h_2 = w_2 h_1 = 0.25. Then y=w3h2=0.125y = w_3 h_2 = 0.125.
  2. Final loss. The output loss is Lout=(y−t)2=(0.125−1)2=0.765625L_{\text{out}} = (y - t)^2 = (0.125 - 1)^2 = 0.765625. Training will try to make this smaller.
  3. Gradient along the output path. Backpropagation sends the error back from yy to w1w_1. The gradient reaching h1h_1 through h2h_2 is −0.4375-0.4375, and since x=1x = 1, the gradient for w1w_1 is −0.4375-0.4375. Each layer multiplied the signal by a factor of 0.50.5 on the way back, so the first layer receives a small update.
  4. Add an auxiliary head at h1h_1. Put a small head on the first hidden layer, here one multiplication a=v h1a = v\,h_1 with v=1v = 1,, and give it its own loss Laux=(a−t)2L_{\text{aux}} = (a - t)^2. Here a=0.5a = 0.5, so Laux=0.25L_{\text{aux}} = 0.25.
  5. Gradient from the auxiliary loss. The auxiliary loss depends directly on h1h_1. Its gradient at h1h_1 is 2(a−t) v=−12(a - t)\,v = -1. This term does not have to pass through w2w_2 and w3w_3, so it is not shrunk by them.
  6. Total gradient for w1w_1. Add the two paths: −0.4375+(−1)=−1.4375-0.4375 + (-1) = -1.4375. The first layer now gets a gradient more than three times as large as in step 3.
  7. Update. With a learning rate of 0.10.1 and Gradient Descent, the new w1w_1 is 0.5−0.1×(−1.4375)=0.643750.5 - 0.1 \times (-1.4375) = 0.64375. Without the auxiliary head it would have been 0.543750.54375.

The output layer of the network is unchanged in the sense that it still receives its own loss. The only new thing is a second route for the gradient to reach the early layer, and the early layer can now learn from a target close to it.

The math

Step 1 in symbols: the forward pass

For a network with layers h1,…,hLh_1, \dots, h_L, each layer computes hℓ=ϕℓ(hℓ−1)h_\ell = \phi_\ell(h_{\ell-1}), where ϕℓ\phi_\ell is the layer's function and h0=xh_0 = x is the input. The output is y=hLy = h_L, and the output loss is Lout=ℓ(y,t)L_{\text{out}} = \ell(y, t), where tt is the target and ℓ\ell is any Loss Function.

Step 3 in symbols: the chain rule along the output path

The gradient of the output loss with respect to the first layer's weights is found by the Chain Rule:

∂Lout∂w1=∂Lout∂y⋅∂y∂h2⋅∂h2∂h1⋅∂h1∂w1\frac{\partial L_{\text{out}}}{\partial w_1} = \frac{\partial L_{\text{out}}}{\partial y} \cdot \frac{\partial y}{\partial h_2} \cdot \frac{\partial h_2}{\partial h_1} \cdot \frac{\partial h_1}{\partial w_1}

In the example, ∂Lout∂y=2(y−t)=−1.75\frac{\partial L_{\text{out}}}{\partial y} = 2(y - t) = -1.75, ∂y∂h2=w3=0.5\frac{\partial y}{\partial h_2} = w_3 = 0.5, ∂h2∂h1=w2=0.5\frac{\partial h_2}{\partial h_1} = w_2 = 0.5, and ∂h1∂w1=x=1\frac{\partial h_1}{\partial w_1} = x = 1. The product is −1.75×0.5×0.5×1=−0.4375-1.75 \times 0.5 \times 0.5 \times 1 = -0.4375.

In a deep network with many factors, each one often below 1 in magnitude, the product shrinks quickly. This is the Vanishing Gradient Problem in the form the example shows.

Steps 4 and 5 in symbols: the auxiliary term

Let the network have an auxiliary head on layer ℓ\ell, with prediction aℓ=gℓ(hℓ)a_\ell = g_\ell(h_\ell) and auxiliary loss Laux,ℓ=ℓ(aℓ,t)L_{\text{aux},\ell} = \ell(a_\ell, t). The total training objective is

L=Lout+∑ℓ∈Aλℓ Laux,ℓL = L_{\text{out}} + \sum_{\ell \in \mathcal{A}} \lambda_\ell \, L_{\text{aux},\ell}

where A\mathcal{A} is the set of layers with heads and λℓ≥0\lambda_\ell \ge 0 is a weight for each auxiliary loss. In the example, A={1}\mathcal{A} = \{1\}, λ1=1\lambda_1 = 1, and g1(h1)=v h1g_1(h_1) = v\,h_1.

The gradient of the auxiliary term with respect to h1h_1 is λ1⋅2(a1−t)⋅v=1⋅2(0.5−1)⋅1=−1\lambda_1 \cdot 2(a_1 - t) \cdot v = 1 \cdot 2(0.5 - 1) \cdot 1 = -1, as in step 5.

Step 6 in symbols: the total gradient

Because the total loss is a sum, its gradient with respect to w1w_1 is the sum of the two gradients:

∂L∂w1=∂Lout∂w1+λ1∂Laux,1∂h1⋅∂h1∂w1=−0.4375+(−1)(1)=−1.4375\frac{\partial L}{\partial w_1} = \frac{\partial L_{\text{out}}}{\partial w_1} + \lambda_1 \frac{\partial L_{\text{aux},1}}{\partial h_1} \cdot \frac{\partial h_1}{\partial w_1} = -0.4375 + (-1)(1) = -1.4375

Step 7 in symbols: the update

Each weight moves against its gradient, w←w−η ∂L/∂ww \leftarrow w - \eta \, \partial L / \partial w, with learning rate η\eta. With η=0.1\eta = 0.1, the new value is 0.5−0.1×(−1.4375)=0.643750.5 - 0.1 \times (-1.4375) = 0.64375.

Worked examples

Example
The same tiny network with a different auxiliary weight (tiny)

Keep the network and the auxiliary head from the walkthrough, but set the auxiliary weight to λ1=0.5\lambda_1 = 0.5.

  1. The output-path gradient for w1w_1 is still −0.4375-0.4375, because the output loss is unchanged.
  2. The auxiliary gradient at h1h_1 is λ1⋅(−1)=0.5×(−1)=−0.5\lambda_1 \cdot (-1) = 0.5 \times (-1) = -0.5.
  3. The total gradient is −0.4375+(−0.5)=−0.9375-0.4375 + (-0.5) = -0.9375.
  4. With learning rate 0.10.1, the update is 0.5−0.1×(−0.9375)=0.593750.5 - 0.1 \times (-0.9375) = 0.59375.

Interpretation: halving λ1\lambda_1 halves the extra push on the early layer. The auxiliary weight is a knob that trades the early layer's training against the output loss, and it is set by hand or tuned.

Example
Output path and auxiliary head in a three-layer network (standard)

Use a network with inputs x=2x = 2, weights w1=w2=w3=1w_1 = w_2 = w_3 = 1, target t=3t = 3, and an auxiliary head a1=v h1a_1 = v\,h_1 with v=1v = 1 and λ1=1\lambda_1 = 1.

  1. Forward pass: h1=2h_1 = 2, h2=2h_2 = 2, y=2y = 2.
  2. Output loss: Lout=(2−3)2=1L_{\text{out}} = (2 - 3)^2 = 1.
  3. Output gradient: ∂Lout/∂y=2(2−3)=−2\partial L_{\text{out}} / \partial y = 2(2 - 3) = -2.
  4. Output path to h1h_1: −2×w3×w2=−2×1×1=−2-2 \times w_3 \times w_2 = -2 \times 1 \times 1 = -2.
  5. Output path to w1w_1: −2×x=−2×2=−4-2 \times x = -2 \times 2 = -4.
  6. Auxiliary loss: a1=1×2=2a_1 = 1 \times 2 = 2, so Laux=(2−3)2=1L_{\text{aux}} = (2 - 3)^2 = 1.
  7. Auxiliary gradient at h1h_1: 2(a1−t)×v=2(−1)×1=−22(a_1 - t) \times v = 2(-1) \times 1 = -2.
  8. Auxiliary gradient to w1w_1: −2×x=−4-2 \times x = -4.
  9. Total gradient for w1w_1: −4+(−4)=−8-4 + (-4) = -8.

Interpretation: the auxiliary head doubled the gradient on the first layer in this case. The total objective is L=1+1=2L = 1 + 1 = 2, and both terms point the first layer in the same direction. When the two terms disagree, they partly cancel, and that is one reason the auxiliary weight matters.

Example
Deep supervision in a recurrent model unrolled for T steps (realistic)

Suppose a model refines an answer over T=4T = 4 steps, and the answer at step kk is yky_k. Let the target be t=3t = 3. The model is trained with the loss L=∑k=14(yk−t)2L = \sum_{k=1}^{4} (y_k - t)^2, so every step is supervised, not just the last.

  1. Suppose the four answers are y1=0y_1 = 0, y2=1y_2 = 1, y3=2y_3 = 2 and y4=2.5y_4 = 2.5.
  2. Step losses: (0−3)2=9(0 - 3)^2 = 9, (1−3)2=4(1 - 3)^2 = 4, (2−3)2=1(2 - 3)^2 = 1, and (2.5−3)2=0.25(2.5 - 3)^2 = 0.25.
  3. Total loss: 9+4+1+0.25=14.259 + 4 + 1 + 0.25 = 14.25.
  4. Without deep supervision, only L=(y4−t)2=0.25L = (y_4 - t)^2 = 0.25 would be used, and the gradient would have to reach step 1 by passing through three more steps.
  5. With deep supervision, the step-1 answer receives a gradient from its own loss of 2(y1−t)=−62(y_1 - t) = -6, which is a direct signal.
  6. The early steps also learn to move toward 33 on their own, and the final step learns to refine rather than to do all the work.

Try it: in step 5, suppose the target were t=3t = 3 and step 1 gave y1=2y_1 = 2 instead of 00. What would the direct gradient at step 1 be, and would it push the answer up or down? Answer: 2(2−3)=−22(2 - 3) = -2. The gradient is negative, so gradient descent would increase y1y_1, toward the target.

Common mistakes and tradeoffs

It is tempting to think that auxiliary heads are free. They add parameters and compute during training. The head's weights also change what the early layer learns, so an auxiliary head placed in the wrong place can make the intermediate features less useful for the final output. In fact, the cost is small for a linear head, but the choice of location and weight λℓ\lambda_\ell can matter a lot, because the early layer now serves two objectives.

It is tempting to think that deep supervision replaces residual connections. It does not. A Residual Connection gives the gradient a direct path through a block, which helps every layer. Deep supervision gives an intermediate layer its own target. The two address related problems in different ways, and they are often used together.

It is tempting to think that the auxiliary heads should be used at test time. In most image classifiers they are removed after training, because the goal is a single final prediction. In some dense-prediction models, the side outputs are combined into the final prediction on purpose. The design must decide which one applies.

When to use it: deep networks that train slowly or fail to train, models with a natural set of intermediate predictions, and unrolled or iterative models where the intermediate answers have a meaning. When not to use it: shallow networks where the vanishing gradient is not a problem, and tasks where an intermediate output has no defined target, since the auxiliary loss then has nothing sensible to match.

Alternatives: residual connections, normalisation layers, and careful initialisation and choice of Activation Function address the same problem without new loss terms. A common table for comparison:

ApproachWhat it changesCostMain limit
Deep supervisionAdds losses at intermediate layersExtra heads, one weight per headNeeds a meaningful intermediate target
Residual connectionsAdds a shortcut path inside blocksAlmost noneDoes not give intermediate targets
Better initialisation and normalisationKeeps signal sizes stableAlmost noneDoes not guarantee learning in very deep models

History

The vanishing gradient was understood as a practical problem in the 1990s, when networks deeper than a few layers were hard to train with gradient methods. The usual response then was to use fewer layers or better initialisation.

In 2014, Szegedy and colleagues described GoogLeNet, which used auxiliary classifiers at intermediate points. They reported that these helped the gradient reach the early layers during training, and they removed the classifiers when the network was used for prediction. The same year, Lee and colleagues proposed deeply-supervised nets, which add a companion objective to each hidden layer, and they reported better classification performance. The method needs no new layer types, only extra loss terms.

Later, residual connections became the more common fix for depth, and deep supervision became one tool among several. It still returns whenever a model has a meaningful intermediate output.

Recap

  • A deep network's output loss sends a training signal backward through every layer, and the signal can shrink on the way.
  • Deep supervision adds a head and a loss at an intermediate layer, so that layer receives a direct gradient.
  • The training objective becomes the output loss plus weighted auxiliary losses, and the gradient is the sum of the paths.
  • The auxiliary weight and the placement of the heads are design choices with real effects.
  • Residual connections and normalisation solve related problems and are often used alongside it.

Read next: Hierarchical Reasoning Model uses this idea in a recurrent setting, where the intermediate answers are trained as well as the last one.

Check yourself

QuestionIn one sentence, what does deep supervision add to the training objective?Show answer

It adds an auxiliary loss, computed from a prediction head on an intermediate layer, to the output loss. The total is a weighted sum, so the intermediate layer receives a direct training signal.

QuestionIn the tiny network with x=1x = 1, all weights 0.50.5 and t=1t = 1, the output-path gradient for w1w_1 is −0.4375-0.4375. If an auxiliary head on h1h_1 with v=1v = 1 and weight λ1=1\lambda_1 = 1 contributes −1-1 to the gradient at h1h_1, what is the total gradient for w1w_1?Show answer

The total is −0.4375+(−1)=−1.4375-0.4375 + (-1) = -1.4375, since x=1x = 1 means the gradient at h1h_1 carries straight through to w1w_1.

QuestionWhy does deep supervision not replace residual connections, even though both help deep networks train?Show answer

They address different problems. A residual connection gives the gradient a direct path through a block without adding any targets. Deep supervision gives an intermediate layer its own target and loss. The first changes how the signal flows, the second changes what the intermediate layer is asked to learn. They can be used together.

QuestionA model with four steps uses the loss L=∑k=14(yk−t)2L = \sum_{k=1}^{4} (y_k - t)^2 with target t=3t = 3. Step 2 outputs y2=1y_2 = 1. What is that step's loss, and what is the direct gradient of its loss with respect to y2y_2?Show answer

The loss is (1−3)2=4(1 - 3)^2 = 4. The gradient is 2(y2−t)=2(1−3)=−42(y_2 - t) = 2(1 - 3) = -4, which means increasing y2y_2 would reduce the loss.

QuestionYou are training a classifier whose intermediate layers have no natural target. Would you add deep supervision, and why?Show answer

Probably not. Deep supervision needs a target for each auxiliary head, and without a meaningful intermediate target the auxiliary loss would push the layer toward an arbitrary goal. Residual connections or normalisation are better first choices in that case.

Sources