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.

An image is a vector

Read the pixels row by row.

$\cx{\bx} = (\cx{x_1}, \cx{x_2}, \dots, \cx{x_{784}})$

The model receives a list of 784 numbers, not a picture.

We want a function $f(\cx{\bx})$ that returns the digit.

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}$.

What one line cannot do

Logistic regression draws one straight boundary.

$\cw{\btheta}^{\!\top}\cx{\bx} + \cw{b} = 0$

For the XOR, ring, and spiral data no line separates the classes.

The fix is new features. A neural network learns them.

One neuron

$\ch{a} = \varphi\big(\cw{\theta_1}\cx{x_1} + \cw{\theta_2}\cx{x_2} + \cw{b}\big)$

A neuron is a weighted sum followed by a nonlinear function $\varphi$.

The weights set the direction of the boundary. The bias shifts it.

With a sigmoid $\varphi$, one neuron is logistic regression.

Activation functions

Solid: $\varphi(z)$. Dashed: the derivative $\varphi'(z)$, used in learning.

Without a nonlinear $\varphi$, stacked layers collapse to one linear map.

$\cw{\bTheta_2}(\cw{\bTheta_1}\cx{\bx}) = (\cw{\bTheta_2}\cw{\bTheta_1})\,\cx{\bx}$

A hidden layer

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}$.

The same network on MNIST

$\cx{\bx}\in\R^{784} \;\to\; \ch{\bh}\in\R^{m} \;\to\; \cp{\bp}\in\R^{10}$
$\cw{\bTheta_1}\in\R^{m\times 784},\quad \cw{\bTheta_2}\in\R^{10\times m}$

Two inputs become 784. Two classes become ten.

With $m = 64$ hidden units: 50,890 parameters, against 7,850 for logistic regression.

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.

Backpropagation through the whole network

Pretrained network on a test digit.
$\cl{\bdelta_2} = \cp{\bp} - \mathbf{e}_{y}, \qquad \nabla_{\cw{\bTheta_2}} L = \cl{\bdelta_2}\,\ch{\bh}^{\!\top}$
$\cl{\bdelta_1} = (\cw{\bTheta_2}^{\!\top}\cl{\bdelta_2}) \odot \varphi'(\bz_1)$
$\nabla_{\cw{\bTheta_1}} L = \cl{\bdelta_1}\,\cx{\bx}^{\!\top}$

The output error is probability minus truth.

Each weight gradient is an outer product: error signal times the layer's input.

Stochastic gradient descent

$\btheta \;\leftarrow\; \btheta - \eta\,\frac{1}{|B|}\sum_{i\in B} \cl{\nabla L_i(\btheta)}$

A mini-batch gradient is a noisy, cheap estimate of the full gradient.

One pass over all the data is an epoch.

Smaller batches: more steps per epoch, noisier path.

Train on MNIST, live

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.

Toward images: convolution

$(\cw{K} * \cx{\bx})_{ij} = \sum_{u,v} \cw{K_{uv}}\;\cx{x_{i+u,\,j+v}}$

A convolution slides one small kernel over every position.

Nine weights instead of 784 per unit, and the same feature detector everywhere.

Convolutional networks learn the filters. Next lecture.

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

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.html is the 13-slide version.

Reading and credits

Goodfellow, Bengio, Courville, Deep Learning (MIT Press, 2016), chapters 6 and 8. Free online.

Hastie, Tibshirani, Friedman, The Elements of Statistical Learning, chapter 11.

Nielsen, Neural Networks and Deep Learning (online book), chapters 1 and 2.

LeCun, Bottou, Bengio, Haffner (1998), gradient-based learning applied to document recognition. Proc. IEEE. The MNIST paper.

Data. MNIST, Yann LeCun, Corinna Cortes, Christopher J.C. Burges. 5,000 training and 1,000 test images are embedded in this deck.

Code. Every figure is plain JavaScript in js/. The two pretrained models were fit with build/train_pretrained.py on all 60,000 images.

Maths. Rendered with KaTeX (MIT licence).

Open index.html in any browser. Nothing leaves your computer.

Harsh Parikh · harsh.parikh@yale.edu · Amazon SCOT and Yale University