---
title: Regularization and convolutional networks
heading: Weight decay, early stopping, dropout and data augmentation as ways to control a network's effective complexity, then convolutional layers with shared weights, their output sizes, parameter counts, operation counts and receptive fields
eyebrow: S14 · Lesson 28 · Regularization and convolutional networks
xp: 50
---

::: hook
A network with millions of weights can memorize its training set, noise and all. Some cures are familiar from lesson 9, like penalizing large weights; others are specific to networks, like stopping early or randomly switching units off. The most effective cure, though, is building knowledge of the problem into the architecture. An image classifier shouldn't need to learn separately that an edge is an edge in every corner of the picture. A convolutional layer shares one small filter across all positions, which cuts the weights by orders of magnitude and makes the network's features shift along with the image. It's the FIR filter of DSP, learned from data.
:::

::: play
- A one-dimensional convolutional layer with a three-tap kernel (a, b, c) applied to a signal containing a rectangular pulse: $y[n] = a\,x[n - 1] + b\,x[n] + c\,x[n + 1]$.
- With (−1, 0, 1) it's an edge detector, firing at both edges with opposite signs, wherever the pulse sits. With (1, 1, 1)/3 it smooths.
- The same three weights serve every position: that's weight sharing.
:::

```sim plot
view: graph
x: n = 0..28
y: y = -1.5..2
slider: a = -1..1 step 0.1 = -1
slider: b = -1..1 step 0.1 = 0
slider: c = -1..1 step 0.1 = 1
plot: step(n - 8) - step(n - 20) | stems | color: ch3 | label: input x[n]
plot: a*(step(n - 9) - step(n - 21)) + b*(step(n - 8) - step(n - 20)) + c*(step(n - 7) - step(n - 19)) | stems | label: output y[n]
```

::: predict
## A 32 × 32 color image (3 channels) feeds a layer of 16 filters, each 5 × 5, with padding so the output is also 32 × 32. How many weights does the layer have?

- [ ] About 50 million
- [ ] 49,152
- [x] 1,216
- [ ] 400

::: reveal
Each filter has 5 × 5 × 3 = 75 weights plus a bias, and there are 16 filters: 1,216 parameters. A fully connected layer producing the same 32 × 32 × 16 output from the 3,072 inputs would need 3,072 × 16,384 = 50.3 million weights. The convolution still does 1.23 million multiply-accumulates, because each weight is reused at all 1,024 positions.
:::
:::

::: nameit
## Regularizing networks

- **Weight decay** adds $\frac{\lambda}{2}\|\mathbf{w}\|^2$ to the error, so each step also shrinks the weights by a factor $1 - \eta\lambda$: lesson 9's ridge penalty.
- **Early stopping** halts training when the error on a validation set starts to rise. Weights start small and grow during training, so stopping early acts like a penalty on their size.
- **Dropout** sets each hidden unit to zero with probability p during training, so units can't rely on one another. In the usual "inverted" form, surviving units are scaled by 1/(1 − p) during training so nothing changes at test time.
- **Data augmentation** adds shifted, flipped or noisy copies of training examples, teaching the invariances directly.

## Convolutional layers

A convolutional layer slides F filters of size K × K × C over an input with C channels. Each output is a weighted sum of a K × K patch plus a bias, and every position uses the same weights.

- **Parameters:** $K^2CF + F$, independent of the image size.
- **Output size** along each dimension, with padding P and stride S: $\lfloor(N - K + 2P)/S\rfloor + 1$.
- **Cost:** each output value takes $K^2C$ multiply-accumulates.
- **Receptive field:** stacking L layers of K × K with stride 1 lets each output see $L(K - 1) + 1$ inputs across; strides and pooling make it grow faster.

**Pooling** (max or average over a window) shrinks the maps and adds tolerance to small shifts.
:::

::: example
## Count the parameters and multiply-accumulates of the predict question's layer, and the output size of a 3 × 3, stride-2 layer with no padding on the 32 × 32 output.

::: solution
1. **Parameters.**
   $$5 \times 5 \times 3 \times 16 + 16 = 1{,}216$$
2. **Multiply-accumulates.** 32 × 32 × 16 outputs, 75 each:
   $$\begin{aligned} &32 \times 32 \times 16 \times 75 \\ &= 1{,}228{,}800 \end{aligned}$$
3. **Stride-2 layer.** Along each side:
   $$\lfloor (32 - 3)/2 \rfloor + 1 = 15$$
4. **A first layer.** 7 × 7 filters, stride 2 and padding 3 on a 224-pixel image give
   $$\lfloor (224 - 7 + 6)/2 \rfloor + 1 = 112$$

**Answer:** 1,216 parameters and 1,228,800 multiply-accumulates; the stride-2 layer's output is 15 × 15.
:::
:::

::: example
## How far does weight decay alone shrink a weight over 1000 steps with η = 0.01 and λ = 0.1? And what receptive field do five stacked 3 × 3 layers have?

::: solution
1. **Weight decay.**
   $$\begin{aligned} (1 - 0.001)^{1000} &= 0.368 \\ &\approx e^{-1} \end{aligned}$$
   A weight that the data don't support fades over about $1/(\eta\lambda)$ steps.
2. **Receptive field.**
   $$5 \times 2 + 1 = 11\ \text{pixels}$$
   across, the same as one 11 × 11 filter.
3. **Weights.** The stack uses
   $$5 \times 9 = 45$$
   weights per channel pair instead of 121.

**Answer:** weight decay shrinks the weight to 0.368 of its value, and five 3 × 3 layers see 11 pixels across.
:::
:::

::: example
## A 3 × 3 convolution maps 256 channels to 256. How many weights does it have, and how many does a depthwise separable version (a 3 × 3 filter per channel, then a 1 × 1 convolution) need?

::: solution
1. **Standard layer.**
   $$\begin{aligned} &3 \times 3 \times 256 \times 256 \\ &= 589{,}824 \end{aligned}$$
2. **Depthwise separable.**
   $$\begin{aligned} \text{depthwise} &= 3 \times 3 \times 256 \\ &= 2{,}304 \\ \text{pointwise} &= 256 \times 256 \\ &= 65{,}536 \end{aligned}$$
   a total of 67,840.
3. **Ratio.**
   $$589{,}824/67{,}840 = 8.7$$
   and the multiply-accumulates per output pixel fall by the same factor.
4. **Where it's used.** Mobile networks are built from such layers, trading a little accuracy for nearly an order of magnitude less computation.

**Answer:** the standard layer has 589,824 weights and the separable version 67,840, 8.7 times fewer.
:::
:::

::: check
given: N = 28 | 32 | 224; K = 3 | 5 | 7; P = 1 | 2; S = 1 | 2
answer: floor((N - K + 2*P)/S) + 1
tolerance: 0.5%

## A {K} × {K} convolution with padding {P} and stride {S} is applied to a {N} × {N} input. How wide is the output?

::: right
Right. $\lfloor(N - K + 2P)/S\rfloor + 1$ = {answer}.
:::

::: misses
- [floor((N - K)/S) + 1] Include the padding on both sides.
- [any] Not yet. Count the positions the filter can take.
:::
:::

::: check
given: K = 1 | 3 | 5; C = 3 | 64 | 256; F = 16 | 128 | 512
answer: K^2*C*F + F
tolerance: 0.5%

## A layer has {F} filters of size {K} × {K} over {C} input channels. How many parameters does it have, with biases?

::: right
Right. $K^2CF + F$ = {answer}.
:::

::: misses
- [K^2*F + F] Each filter spans all C input channels.
- [any] Not yet. Weights per filter, times filters, plus biases.
:::
:::

::: check
given: H = 14 | 56 | 112; K = 3 | 5; C = 32 | 128; F = 32 | 256
answer: H^2*F*K^2*C
tolerance: 0.5%

## A convolutional layer produces an {H} × {H} × {F} output with {K} × {K} filters over {C} input channels. How many multiply-accumulates does it take?

::: right
Right. $H^2F \times K^2C$ = {answer}.
:::

::: misses
- [K^2*C*F] That's the weight count; each weight is used at every output position.
- [any] Not yet. Outputs times work per output.
:::
:::

::: check
given: L = 2 | 5 | 12; K = 3 | 5
answer: L*(K - 1) + 1
tolerance: 0.5%

## {L} convolutional layers of {K} × {K} filters with stride 1 are stacked. How wide is each final output's receptive field?

::: right
Right. $L(K - 1) + 1$ = {answer}.
:::

::: misses
- [L*K] Neighboring layers' windows overlap; each adds K − 1.
- [any] Not yet. Each layer widens the view by K − 1.
:::
:::

::: check
given: p = 0.1 | 0.2 | 0.3
answer: 1/(1 - p)
tolerance: 0.5%

## Inverted dropout drops units with probability {p} during training. By what factor must surviving units be scaled?

::: right
Right. $1/(1 - p)$ = {answer}, so each unit's expected output is unchanged.
:::

::: misses
- [1/p] Scale by the inverse of the keep probability.
- [any] Not yet. Keep the expected value the same.
:::
:::

::: check
given: e = 0.01 | 0.05 | 0.1; l = 0.001 | 0.01; s = 0.5 | 1 | 3; let n = round(s/(e*l))
answer: (1 - e*l)^n
tolerance: 0.5%

## With learning rate {e} and weight decay λ = {l}, what fraction of a weight remains after {n} steps if the data's gradient on it is zero?

::: right
Right. $(1 - \eta\lambda)^n$ = {answer}.
:::

::: misses
- [1 - e*l*n] The shrinkage compounds.
- [any] Not yet. Each step multiplies by $1 - \eta\lambda$.
:::
:::

::: check
kind: parts
given: K = 3 | 5 | 7; C = 32 | 128; F = 64 | 256

## A {K} × {K} convolution maps {C} channels to {F}.

::: part
answer: K^2*C + C*F
unit: weights
tolerance: 0.5%

### How many weights does a depthwise separable version need (no biases)?

::: misses
- [K^2*C*F] That's the standard convolution.
- [any] $K^2C + CF$.
:::
:::

::: part
answer: K^2*C*F/(K^2*C + C*F)
tolerance: 0.5%

### By what factor does it cut the weights?

::: misses
- [any] Standard over separable.
:::
:::
:::

::: check
kind: derivation
vars: w = -3..3, e = 0.0001..0.1, l = 0.0001..1, g = -3..3

## A weight w with loss gradient g is updated by gradient descent with rate η and an L2 penalty $\frac\lambda2w^2$.

::: step
answer: w - e*(g + l*w)

### What is the updated weight?
:::

::: step
answer: 1 - e*l

### By what factor does the penalty alone shrink the weight each step?
:::

::: right
Right. That's why an L2 penalty is called weight decay.
:::
:::

::: explain
## Why do convolutional layers work so well for images, compared with fully connected layers?

::: model
Images have two kinds of structure that a fully connected layer ignores. Useful features, such as edges and textures, are local, so each unit only needs to look at a small patch. And a feature means the same thing wherever it appears, so the same detector should be applied at every position. A convolutional layer builds both facts in: small filters capture locality, and sharing each filter across all positions makes the response shift with the image. That cuts the parameters by orders of magnitude, so far less data is needed to learn them, while stacked layers still combine local features into large-scale ones as the receptive field grows.
:::
:::

::: practice

::: check
given: N = 16 | 512; K = 1 | 3 | 11; P = 0 | 1 | 5; S = 1 | 2 | 4
answer: floor((N - K + 2*P)/S) + 1
tolerance: 0.5%

## Output width: N = {N}, K = {K}, P = {P}, S = {S}?

::: misses
- [any] $\lfloor(N - K + 2P)/S\rfloor + 1$.
:::
:::

::: check
given: K = 1 | 7; C = 1 | 1024; F = 8 | 2048
answer: K^2*C*F + F
tolerance: 0.5%

## Parameters: {F} filters, {K} × {K}, {C} channels?

::: misses
- [any] $K^2CF + F$.
:::
:::

::: check
given: L = 1 | 50; K = 3 | 7
answer: L*(K - 1) + 1
tolerance: 0.5%

## Receptive field: {L} layers of {K} × {K}?

::: misses
- [any] $L(K - 1) + 1$.
:::
:::
:::
