---
title: "Problem set 5: neural networks"
heading: "Problem set 5: network sizes and forward passes, softmax outputs, backpropagated gradients and gradient checks, learning rates and conditioning, momentum, Adam and minibatches, and convolutional layers"
eyebrow: S14 · Problem set 5
type: problems
---

::: hook
These problems cover lessons 24 to 28: counting parameters and operations, forward passes through tanh and ReLU units, softmax, gradients by backpropagation, vanishing gradients and finite-difference checks, stable and optimal learning rates, momentum, Adam's moments, minibatch noise, weight decay, dropout and convolutional layer arithmetic.
:::

::: pool
pick: 12

::: check
kind: parts
given: D = 16 | 100; H = 32 | 200; K = 2 | 10; B = 16 | 64

## A {D}–{H}–{K} network with biases, run on batches of {B}.

::: part
answer: (D + 1)*H + (H + 1)*K
tolerance: 0.5%

### Parameters?

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

::: part
answer: B*(D*H + H*K)
tolerance: 0.5%

### Multiply-accumulates per batch?

::: misses
- [any] $B(DH + HK)$.
:::
:::
:::

::: check
kind: parts
given: u = 0.4 | 1.5; v = -0.8 | 0.6; a = 1 | 2.5; b = -1.5 | 0.5; x = 0.5 | 2; p = 0.5 | 2; q = -1 | 1; r = 0 | 1.5

## A one-input network with tanh hidden weights {u} and {v} and output weights {a} and {b}; separately, softmax activations {p}, {q}, {r}.

::: part
answer: a*tanh(u*x) + b*tanh(v*x)
tolerance: 0.5%

### Network output at x = {x}?

::: misses
- [any] $a\tanh(ux) + b\tanh(vx)$.
:::
:::

::: part
answer: exp(p)/(exp(p) + exp(q) + exp(r))
tolerance: 0.5%

### Softmax probability of the first class?

::: misses
- [any] $e^{a_1}/\sum e^{a_j}$.
:::
:::
:::

::: check
kind: parts
given: u = 0.3 | 1.1; v = 1.5 | -2.5; t = 0 | 1; x = 1 | 2

## $y = w_2\tanh(w_1x)$ with $w_1$ = {u}, $w_2$ = {v}, x = {x}, target {t}, squared error.

::: part
answer: (v*tanh(u*x) - t)^2/2
tolerance: 0.5%

### Error?

::: misses
- [any] $(y - t)^2/2$.
:::
:::

::: part
answer: (v*tanh(u*x) - t)*tanh(u*x)
tolerance: 0.5%

### $\partial E/\partial w_2$?

::: misses
- [any] $(y - t)z$.
:::
:::

::: part
answer: (v*tanh(u*x) - t)*v*(1 - tanh(u*x)^2)*x
tolerance: 0.5%

### $\partial E/\partial w_1$?

::: misses
- [any] $(y - t)w_2(1 - z^2)x$.
:::
:::
:::

::: check
kind: parts
given: w = 0.5 | 1.5; b = 0.25 | 1; v = -2 | 3; t = 0 | 2; x = 1 | 3

## $y = v\,\text{ReLU}(wx + b)$ with w = {w}, b = {b}, v = {v}; input {x}, target {t}, squared error.

::: part
answer: (v*(w*x + b) - t)*v*x
tolerance: 0.5%

### $\partial E/\partial w$?

::: misses
- [any] $(y - t)\,v\,x$ while the unit is on.
:::
:::

::: part
answer: (v*(w*x + b) - t)*(w*x + b)
tolerance: 0.5%

### $\partial E/\partial v$?

::: misses
- [any] $(y - t)\,\text{ReLU}(wx + b)$.
:::
:::
:::

::: check
kind: parts
given: d = 0.25 | 0.5; w = 1 | 1.5; L = 8 | 20; e = 1e-5 | 3e-4; r = 2 | 5; W = 1e5 | 1e8

## Backward signals scaled by slope {d} and weight {w} per layer through {L} layers; a gradient check off by {e}; a {W}-weight network.

::: part
answer: (d*w)^L
tolerance: 0.5%

### Overall gradient scale?

::: misses
- [any] $(wh')^L$.
:::
:::

::: part
answer: e/r^2
tolerance: 0.5%

### Central-difference error with the step divided by {r}?

::: misses
- [any] Falls as ε².
:::
:::

::: part
answer: 2*W
tolerance: 0.5%

### Forward passes for a central-difference gradient?

::: misses
- [any] Two per weight.
:::
:::
:::

::: check
kind: parts
given: a = 0.5 | 4; b = 40 | 400

## A quadratic error with Hessian eigenvalues from {a} to {b}.

::: part
answer: 2/b
tolerance: 0.5%

### Stability limit for η?

::: misses
- [any] $2/\lambda_\max$.
:::
:::

::: part
answer: 2/(a + b)
tolerance: 0.5%

### Best fixed η?

::: misses
- [any] $2/(\lambda_\min + \lambda_\max)$.
:::
:::

::: part
answer: (b/a - 1)/(b/a + 1)
tolerance: 0.5%

### Factor per step at that η?

::: misses
- [any] $(\kappa - 1)/(\kappa + 1)$.
:::
:::

::: part
answer: (sqrt(b/a) - 1)/(sqrt(b/a) + 1)
tolerance: 0.5%

### Factor per step with tuned momentum?

::: misses
- [any] $(\sqrt{\kappa} - 1)/(\sqrt{\kappa} + 1)$.
:::
:::
:::

::: check
kind: parts
given: k = 20 | 500; e = 1e-4 | 1e-8

## Condition number {k}, target reduction {e}.

::: part
answer: ceil(ln(e)/ln((k - 1)/(k + 1)))
tolerance: 0.5%

### Gradient-descent steps at the best rate?

::: misses
- [any] Each step multiplies by $(\kappa - 1)/(\kappa + 1)$.
:::
:::

::: part
answer: ceil(ln(e)/ln((sqrt(k) - 1)/(sqrt(k) + 1)))
tolerance: 0.5%

### With tuned momentum?

::: misses
- [any] Use √κ.
:::
:::
:::

::: check
kind: parts
given: g = 0.5 | 4; b = 0.9 | 0.95; c = 0.99 | 0.999; t = 2 | 10

## Adam with β₁ = {b} and β₂ = {c}, after {t} steps of constant gradient {g}.

::: part
answer: g*(1 - b^t)
tolerance: 0.5%

### Uncorrected first moment m?

::: misses
- [any] $g(1 - \beta_1^t)$.
:::
:::

::: part
answer: g^2*(1 - c^t)
tolerance: 0.5%

### Uncorrected second moment v?

::: misses
- [any] $g^2(1 - \beta_2^t)$.
:::
:::

::: part
answer: (1 - b^t)/sqrt(1 - c^t)
tolerance: 0.5%

### Ratio of the uncorrected step to the corrected one?

::: misses
- [any] $(1 - \beta_1^t)/\sqrt{1 - \beta_2^t}$.
:::
:::
:::

::: check
kind: parts
given: s = 1 | 6; B = 32 | 256; p = 0.2 | 0.4; e = 0.01 | 0.05; l = 0.001 | 0.01; let n = round(2/(e*l))

## Gradient noise {s} per example with batches of {B}; dropout probability {p}; weight decay λ = {l} at η = {e} for {n} steps.

::: part
answer: s/sqrt(B)
tolerance: 0.5%

### Minibatch gradient standard deviation?

::: misses
- [any] $\sigma/\sqrt{B}$.
:::
:::

::: part
answer: 1/(1 - p)
tolerance: 0.5%

### Inverted-dropout scale?

::: misses
- [any] $1/(1 - p)$.
:::
:::

::: part
answer: (1 - e*l)^n
tolerance: 0.5%

### Fraction of an unsupported weight left?

::: misses
- [any] $(1 - \eta\lambda)^n$.
:::
:::
:::

::: check
kind: parts
given: N = 32 | 64 | 128; K = 3 | 5; P = 1 | 2; S = 1 | 2; C = 3 | 32; F = 16 | 64

## A {K} × {K} convolution, padding {P}, stride {S}, from {C} channels to {F}, on a {N} × {N} input.

::: part
answer: floor((N - K + 2*P)/S) + 1
tolerance: 0.5%

### Output width?

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

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

### Parameters?

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

::: part
answer: (floor((N - K + 2*P)/S) + 1)^2*F*K^2*C
tolerance: 0.5%

### Multiply-accumulates?

::: misses
- [any] Outputs times $K^2C$.
:::
:::
:::

::: check
kind: parts
given: L = 3 | 8; K = 3 | 5; N = 64 | 224; C = 3 | 16; F = 16 | 64

## {L} stacked {K} × {K} stride-1 layers; a fully connected layer mapping an {N} × {N} × {C} input to an {N} × {N} × {F} output.

::: part
answer: L*(K - 1) + 1
tolerance: 0.5%

### Receptive field width?

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

::: part
answer: (N^2*C)*(N^2*F)
tolerance: 0.5%

### Weights in the fully connected layer (no biases)?

::: misses
- [any] Inputs times outputs.
:::
:::

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

### How many times more than one {K} × {K} convolutional layer's weights?

::: misses
- [any] Divide by $K^2CF$.
:::
:::
:::

::: check
kind: parts
given: H = 4 | 20; a = -1.5 | 2; x = 0.5 | 1.5; y = 0 | 1

## A one-input ReLU layer of {H} units; a sigmoid at a = {a}; lesson 24's two-hinge network at the inputs ({x}, {y}).

::: part
answer: H + 1
tolerance: 0.5%

### Maximum number of linear pieces?

::: misses
- [any] One per kink, plus one.
:::
:::

::: part
answer: 1/(1 + exp(-a))*(1 - 1/(1 + exp(-a)))
tolerance: 0.5%

### σ′(a)?

::: misses
- [any] $\sigma(1 - \sigma)$.
:::
:::

::: part
answer: (x + y + abs(x + y))/2 - 2*(x + y - 1 + abs(x + y - 1))/2
tolerance: 0.5%

### Output $\text{ReLU}(x + y) - 2\,\text{ReLU}(x + y - 1)$ at ({x}, {y})?

::: misses
- [any] Evaluate both hinges.
:::
:::
:::

::: check
kind: parts
given: P = 5 | 50; N = 0.1 | 1.2; E = 5 | 30; F = 20 | 300; u = 0.35 | 0.5

## A {P}-million-parameter network trains for {E} epochs on {N} million examples, on hardware sustaining {u} of {F} TFLOP/s.

::: part
answer: 6*P*1e6*N*1e6*E
unit: FLOP
tolerance: 0.5%

### How much arithmetic does training take?

::: misses
- [any] About 6 operations per parameter per example.
:::
:::

::: part
answer: 6*P*1e6*N*1e6*E/(u*F*1e12)/60
unit: min
tolerance: 0.5%

### How many minutes does it take?

::: misses
- [any] Operations over the sustained rate.
:::
:::
:::

::: check
kind: parts
given: a = 2 | -1; b = 1 | 3; c = 0.5 | -2; d = 4 | 1; p = 3 | -1; q = 2 | 0.5; u = 1 | -2; v = 0.5 | 3

## A layer computes $\mathbf y = W\mathbf x$ with $W = \begin{bmatrix}a & b \\ c & d\end{bmatrix}$, a = {a}, b = {b}, c = {c}, d = {d}, on x = ({p}, {q}); the loss's gradient with respect to y is ({u}, {v}).

::: part
answer: v*p
tolerance: 0.5%

### What is $\partial L/\partial c$, the weight from $x_1$ to $y_2$?

::: misses
- [u*p] c feeds $y_2$.
- [any] $g_2x_1$.
:::
:::

::: part
answer: u*q
tolerance: 0.5%

### What is $\partial L/\partial b$, the weight from $x_2$ to $y_1$?

::: misses
- [v*q] b feeds $y_1$.
- [any] $g_1x_2$.
:::
:::

::: part
answer: a*u + c*v
tolerance: 0.5%

### What is $\partial L/\partial x_1$?

::: misses
- [a*u + b*v] Use the first column of W.
- [any] $(W^T\mathbf g)_1$.
:::
:::

::: part
answer: b*u + d*v
tolerance: 0.5%

### And $\partial L/\partial x_2$?

::: misses
- [any] $(W^T\mathbf g)_2$.
:::
:::
:::

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

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

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

### By what factor does a depthwise separable version cut its weights?

::: misses
- [K^2] Count the pointwise layer's CF weights too.
- [any] $K^2CF/(K^2C + CF)$.
:::
:::
:::
:::
