Introduction to
Neural Networks

BIS 527 · Yale School of Public Health
Harsh Parikh
November 12, 2026
Claude was used to design these notes.
Every figure is live. → next   ← back   F fullscreen

Reading Handwritten Digits

MNIST test images. Click one.
Hover over the large digit to read a pixel value.

Each image: 28 × 28 = 784 numbers.

Each pixel between 0 (paper) and 1 (ink).

$\cx{\bx} \in [0,1]^{784}$
$y \in \{0,1,\dots,9\}$

4,000 training, 1,000 validation, 1,000 test images in this deck.

Start from logistic regression

Click a weight map to enlarge it.
$\cp{p_k} = \Pr(y = k \mid \cx{\bx})$
$\cp{p_k} = \dfrac{\exp(\cw{\btheta_k}^{\!\top}\cx{\bx} + \cw{b_k})}{\sum_{j} \exp(\cw{\btheta_j}^{\!\top}\cx{\bx} + \cw{b_j})}$

Class $k$ has a weight vector $\cw{\btheta_k} \in \R^{784}$, one weight per pixel, and an intercept $\cw{b_k}$.

Its weight map: red pixels raise the score for $k$, blue lower it.

The softmax turns the ten scores into probabilities.

90% test accuracy. Each boundary between two classes is flat in $\cx{\bx}$.

A hidden layer learns features

Two inputs here instead of 784, so the boundary can be drawn.

$\ch{\bh} = \varphi(\cw{\bTheta_1}\cx{\bx} + \cw{\bb_1})$
$\cp{\bp} = \mathrm{softmax}(\cw{\bTheta_2}\ch{\bh} + \cw{\bb_2})$

$\ch{\bh}(\cx{\bx})$: the hidden layer's output, the learned features of $\cx{\bx}$.

Each hidden unit: a weighted sum, then $\varphi(z) = \max(0, z)$.

Its sum is zero along one line (small panels).

Softmax on $\ch{\bh}$: straight in $\ch{\bh}$, bent in $\cx{\bx}$.

Forward Pass

Draw in the left box with the mouse.
$\cx{\bx} \;\xrightarrow{\;\cw{\bTheta_1},\;\varphi\;}\; \ch{\bh} \;\xrightarrow{\;\cw{\bTheta_2},\;\mathrm{softmax}\;}\; \cp{\bp}$

Pretrained: 64 ReLU units, 97% test accuracy.

Draw a digit: 784 inputs, 64 features, 10 probabilities.

The loss: negative log-likelihood

$L(\btheta) = -\frac{1}{n}\sum_{i=1}^{n} \log \cp{p_{y_i}}(\cx{\bx_i};\btheta)$

$\cp{p_{y_i}}$: the probability the network assigns to the true class.

Maximum likelihood, as in logistic regression.

A confident wrong answer costs the most: $-\log p \to \infty$ as $p \to 0$.

Accuracy cannot be the objective: its gradient is zero almost everywhere.

Learning: gradient descent

Click the plot to choose a start.
$\btheta \;\leftarrow\; \btheta - \eta\,\cl{\nabla_{\btheta} L(\btheta)}$

Not convex, 50,890 parameters, no closed form: take steps.

$-\cl{\nabla L}$ is the steepest way down near $\btheta$. It does not point at the minimum.

$\cl{\nabla L} = \mathbf{0}$ at every minimum, but also at saddle points.

Too small an $\eta$ is slow. Too large overshoots.

Backpropagation: the chain rule, organized

$\dfrac{\partial L}{\partial \cw{\theta}} = \dfrac{\partial L}{\partial \ch{a}}\cdot\dfrac{\partial \ch{a}}{\partial z}\cdot\dfrac{\partial z}{\partial \cw{\theta}}$

$z = \cw{\theta}\cx{x} + \cw{b}$,   $\ch{a} = \sigma(z) = \frac{1}{1+e^{-z}}$,   $L = -\log \ch{a}$ for $y = 1$.

Forward: each node from the nodes before it.

Backward: multiply local derivatives back to each weight.

Train on MNIST, live

Each step uses a random mini-batch of 32 images, and one epoch is one pass over the 4,000. The epoch is chosen on validation, and test is reported once.

What the hidden units learned

Click a tile.

Each tile: one row of $\cw{\bTheta_1}$ as a 28 × 28 image.

Each weight map is concentrated on a stroke, not a whole digit.

Right: the validation digits that activate it, and its ten output weights.

Overfitting

$L_\lambda(\btheta) = L(\btheta) + \frac{\lambda}{2}\big(\|\cw{\bTheta_1}\|^2 + \|\cw{\bTheta_2}\|^2\big)$

500 images, 101,770 parameters: training hits 100%, validation stalls.

Remedies: more data, weight decay $\lambda$, early stopping on validation.

On this run, weight decay at these values does not help. 4,000 training images give 94% validation accuracy.

With its features fixed, a network is a weighting estimator

$\hat y(\bx) = \sum_{i=1}^{n} w_i(\bx)\, Y_i$
$w_i(\bx) = \tfrac{1}{n} + \ch{\tilde{\bh}}(\bx)^{\!\top} S^{-1}\, \ch{\tilde{\bh}}(\bx_i)$

$\ch{\bh}(\bx)$: the 64 hidden features of $\bx$. $\ch{\tilde{\bh}}(\bx) = \ch{\bh}(\bx) - \ch{\bar{\bh}}$, where $\ch{\bar{\bh}}$ is their average over the $n = 4{,}000$ training images. $S = \sum_i \ch{\tilde{\bh}}(\bx_i)\,\ch{\tilde{\bh}}(\bx_i)^{\!\top} + \lambda I$. $Y_i$ is the one-hot label.

Freeze the features and ridge-regress the labels on them.

$w_i(\bx)$ ignores the labels: a linear smoother, like ridge and splines.

The $\tfrac{1}{n}$ makes the weights sum to one. Some are negative.

Summary and reading

A neuron: a weighted sum, then a nonlinearity.

A hidden layer learns features, and softmax regression uses them.

Training: gradient descent on the average negative log-likelihood.

Backpropagation: the chain rule, one backward pass.

Many parameters: validation set, weight decay, early stopping.

Reading: ISLR §10.1–10.2 and §10.7. Goodfellow, Bengio, Courville, Deep Learning (2016), ch. 6 and 8. Hastie, Tibshirani, Friedman, ESL, ch. 11. Nielsen, Neural Networks and Deep Learning (online).

Data: MNIST (LeCun, Cortes, Burges). Maths: KaTeX. Every figure is plain JavaScript in this folder. index_full.html has nine more slides.