# What Do Residual Connections Do?

> What a residual connection is, why it helped very deep networks train, and where transformers use one, explained simply for your AI interview.

**In short:** A residual connection adds a layer's input back onto its output, so the layer only has to learn the change. It helped very deep networks train, and transformers put one around each of their sub-layers.

AI and LLM interview question Q053, Transformer architecture. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-do-residual-connections-do

## Say it in 30 seconds

A residual connection adds a layer's input back onto its output. So the layer only has to learn the change, and the rest passes straight through. ResNet used this in 2015 to train much deeper networks. Transformers use it around each attention and feed-forward block. It keeps the signal, and the learning signal, travelling through a deep stack.

## Say it in 2 minutes

Think of a deep network as a long stack of layers, each one changing the signal a little. A residual connection changes how one layer is wired. The layer's input is copied along a shortcut, the layer computes a change, and the two are added. The output is the input plus the change.

The reason this matters is that deeper plain networks did worse, even though they had more layers to work with. In the 2015 ResNet paper, on CIFAR-10, a set of images in 10 classes, a 56-layer plain network had higher training error than a 20-layer one. The authors said this was not overfitting. Overfitting means low training error but high test error, so the model has learnt its examples too closely. Here the deeper model was worse on its training data too. The shortcut gives a block an easy default: if it has nothing useful to add, its input passes straight through.

Gradients are the other half. During training the error travels backwards, and a clean shortcut gives it a direct route to the early layers. The 2016 follow-up paper found that, with identity shortcuts (ones that pass the input through unchanged), the gradient of a layer does not vanish even when the weights are arbitrarily small. In an interview I would say it helps the signal and the gradient travel, and I would not claim it solves every training problem.

Transformers use the same idea. The original transformer paper wraps each attention and feed-forward sub-layer in a residual connection, then normalises the result. The shortcut adds no weights, so it is cheap to add.

## What is a residual connection, in plain words?

A layer takes a signal in and hands a new signal out. A residual connection copies the layer's input along a shortcut and adds that copy to the layer's output. The layer then only has to learn what to change. Anything it does not need to change arrives intact.

Deep networks are long stacks of layers, and stacking them caused trouble at first. The 2015 ResNet abstract opens with the problem: "Deeper neural networks are more difficult to train." Their residual connections were the answer, and their networks went up to 152 layers.

Picture a hospital patient record updated shift by shift. The nurse adds what changed, and the older notes stay on the page. A residual block works the same way: the input stays, and the layers add a correction on top of it. In a legal search ranker with 40 blocks, the document's original signal survives the whole stack, and each block only adds a small correction.

- **Residual connection:** A shortcut that adds a layer's input back onto its output.
- **Weights:** The numbers a model learns. Training adjusts them.
- **Sub-layer:** One of the smaller parts inside a transformer layer: attention, which lets each word look at the others, or a feed-forward network, which works on each word on its own.
- **Gradient:** How much the error changes when a weight moves a little. Training uses it to decide which way each weight moves.
- **Overfitting:** A model that does well on its training examples but poorly on new ones.

## How does a residual block work?

A residual block has two paths side by side. One runs the layers, which compute a change. The other is the shortcut, which copies the input without touching it. The two are added, and the sum is the block's output.

*Diagram, How one residual block is wired: The input goes two ways: through the layers, which learn only a change, and along the shortcut, which copies it. The two are added, and the sum goes to the next block. The shortcut adds no weights.*
1. The input goes into the layers.
2. A copy of the input goes along the shortcut.
3. The layers send back only the change they learned.
4. The shortcut sends its copy back, unchanged.
5. The sum goes on to the next block.

1. **Copy the input:** Send x along the shortcut, untouched.
2. **Learn the change:** The layers compute what to adjust. This is the only part the block learns.
3. **Add and pass on:** Add the change to the copy. The sum goes to the next block.

Because the shortcut has no weights, it adds no parameters. Its technical name is an identity shortcut, because it passes the input through unchanged. ResNet puts it plainly: "Identity shortcut connections add neither extra parameter nor computational complexity."

The original transformer's encoder uses the same wiring. Each encoder layer has two sub-layers: an attention sub-layer and a feed-forward sub-layer. The decoder adds a third, which looks at the encoder's output. The paper puts a residual connection around each sub-layer, followed by layer normalisation, which rescales the numbers so they stay in a steady range.

> "We explicitly reformulate the layers as learning residual functions with reference to the layer inputs, instead of learning unreferenced functions." (ResNet paper, 2015)
>
> "Similar to the encoder, we employ residual connections around each of the sub-layers, followed by layer normalization." (Attention Is All You Need)

## Why do very deep networks need it?

In theory, a deeper network could copy a shallower one and do no worse. In practice, plain deep networks often did worse. The ResNet paper calls this a degradation problem: "with the network depth increasing, accuracy gets saturated (which might be unsurprising) and then degrades rapidly."

The 20-layer and 56-layer plain networks, tested on CIFAR-10, a dataset of images in 10 classes, show it: "The deeper network has higher training error, and thus test error." The authors say this is not caused by overfitting, which means low training error but high test error. Here the deeper model is worse even on its training data, which is not what overfitting looks like.

Their fix rests on one observation. If the best a block can do is to pass its input through unchanged (an identity mapping), it is easier to make its change zero than to learn that pass-through with a stack of nonlinear layers, which can bend the signal in complicated ways. A shortcut makes doing nothing the default, so the block only has to learn a correction.

> **Tip:** A careful point for the interview: the ResNet paper says vanishing gradients were already largely handled by better starting weights and normalisation layers. So residual connections were aimed at the degradation problem first.

## Why does the shortcut help the gradient?

Training sends the error backwards through every layer, and each layer scales the gradient on its way. In a plain stack the factors can multiply down to almost nothing by the time they reach the early layers, so those layers barely learn.

*Diagram, Where the learning signal goes: Both stacks get the same error from the output. In the plain stack, each layer can shrink it, so the early layers may barely learn. The shortcut carries the error back almost unchanged, so the early blocks get a usable learning signal.*
1. The error is sent back into the plain stack.
2. The same error is sent back into the residual stack.
3. Each layer can shrink it, so little reaches the start.
4. The shortcut carries it back almost unchanged.

The 2016 identity-mapping paper describes this direct route. With identity shortcuts, the gradient of a layer "does not vanish even when the weights are arbitrarily small", so the error keeps a clear path back.

> "the forward and backward signals can be directly propagated from one block to any other block" (Identity mappings paper, 2016)

Here is a small program with the same idea and made-up numbers. It is a 40-block ranker for a legal search tool, boiled down to one number per document. The plain block scales its signal by w. The residual block keeps the signal and adds w times it.

```python
# A simplified 40-block ranker for a legal search tool, boiled down to one number per document.
# w is the factor each layer applies to the signal (made up here, and small on purpose).
BLOCKS = 40
w = 0.01

def plain_block(x):
    return w * x          # no shortcut: the layer scales what it gets

def residual_block(x):
    return x + w * x      # the shortcut keeps x; the layer adds its change

def run(block, x):
    for _ in range(BLOCKS):
        x = block(x)
    return x

print(f"plain stack, factor left after 40 blocks:    {run(plain_block, 1.0):.2e}")
print(f"residual stack, factor left after 40 blocks: {run(residual_block, 1.0):.3f}")
```

Output:

```text
plain stack, factor left after 40 blocks:    1.00e-80
residual stack, factor left after 40 blocks: 1.489
```

In a straight chain like this, the factor each block applies is also the gradient factor, so the printed numbers show what reaches the first block after 40 steps. With w at 0.01, the plain stack's signal is about 1e-80, effectively zero. The residual stack's signal grows to about 1.49 instead, because each block adds 1% of its input.

> **Tip:** A clean way to put it: the shortcut gives the error a short, direct route back. Say that the 2016 result holds for its identity setup, because the claim depends on that setup.

### How would you debug a deeper model that trains worse?

A team adds 12 blocks to a document-ranking model, and the training error goes up. Here is how I would sort it out.

1. **Read the training error first:** Higher training error points to degradation or a training fault. Overfitting shows low training error with high test error.
2. **Check the wiring:** Confirm each block adds its input back, and that the two outputs are the same size at the add. A size mismatch is a bug to fix first, before any tuning.
3. **Look at gradients per block:** Measure gradient size in the first and last blocks. Tiny values near the input mean the learning signal is fading.
4. **Compare with the shallow model:** If the shallow model wins on training data too, the extra blocks are not earning their place.
5. **Change one thing at a time:** Try normalisation, careful starting weights and the residual add. Re-measure after each change.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: the layer adds its input back and learns only the change.
- [ ] Why it mattered: deeper plain networks had higher training error.
- [ ] The gradient point: the error gets a short, direct route back.
- [ ] The shortcut adds no weights, and transformers use it around each sub-layer.
- [ ] Honest limits: it is a strong help, not a complete guarantee on training.

**Common traps**

- Trap: "Residual connections are there to make the network deeper." Better: Residual connections make a deep stack trainable. They add no weights.
- Trap: "Residual connections fully solve vanishing gradients." Better: They help a lot, and the 2016 result holds for its identity setup. Starting weights and normalisation matter too.
- Trap: "The shortcut adds parameters, so it is expensive." Better: The shortcut is a plain copy with no weights, so it adds no parameters and little compute.

## Can you spot the residual connection?

**Spot the residual connection:** A code review on a contract-search ranker flags one line. Which line keeps a residual connection?

A. x = self.attn(x)
B. x = self.attn(x) * 0.5
C. x = x + self.attn(x)
D. x = self.attn(self.attn(x))

**Answer:** C. Only the third adds the input back onto the output, so the layer learns the change. The others replace the input, scale the layer's output, or run the same layer twice, and the shortcut is lost.

**Quick check:** A 40-block model trains worse than its 20-block version, and the training error is higher too. Which reading fits the ResNet paper best?

A. The model needs more blocks to learn more
B. Degradation, not overfitting: check the wiring
C. Overfitting, so add more dropout to the blocks
D. Remove every normalisation layer and retrain

**Answer:** B. Training error is higher as well, so this is degradation (a deeper model doing worse), not overfitting. More blocks, dropout or removing normalisation do not fix it.

## Can you answer these from memory?

**Flash cards**

- **What does a residual connection do?** Adds the layer's input back onto its output, so the layer learns only the change.
- **What problem did ResNet target?** Degradation: deeper plain networks had higher training error, not overfitting.
- **Does the shortcut add parameters?** No. Identity shortcuts add neither extra parameters nor computational complexity.
- **Why does it help gradients?** The error gets a short, direct route back through the blocks.
- **Do transformers use it?** Yes. The original transformer wraps each attention and feed-forward sub-layer in one.
- **Is it a complete fix?** No. It helps a lot, but other training choices still matter.

## Likely follow-up questions

- Q052 What is layer norm; pre-norm vs post-norm? https://designervenkat.online/interview/ai/what-is-layer-norm-pre-norm-vs-post-norm
- Q054 What does the feed-forward block do? https://designervenkat.online/interview/ai/what-does-the-feed-forward-block-do
- Q426 Vanishing and exploding gradients? (full answer coming)
- Q418 What is backpropagation? https://designervenkat.online/interview/ai/what-is-backpropagation
- Q413 Overfitting vs underfitting? https://designervenkat.online/interview/ai/overfitting-vs-underfitting
- Q064 Roughly how many parameters are in one transformer layer? (full answer coming)

## Learn it in depth

- [Transformers](https://designervenkat.online/learn/machine-learning/transformers)
- [Back propagation: who is to blame?](https://designervenkat.online/learn/machine-learning/backpropagation)
