Applications of Machine Learning

Lecture 7 · Animations

Neural networks

Today's lecture is one idea repeated: an affine map followed by a non-linearity. Here is a single layer taken apart into its operations. Press Step to advance one operation and read what happened; press Play to let it run.

All data on this page is synthetic, generated in your browser from a fixed seed, so Reset always gives back the same picture.

1What one layer does to the plane

A layer $\mathbf{H} = \phi(\mathbf{X}\mathbf{W} + \mathbf{b})$ with two inputs and two units, applied to a grid of the plane and to a mini-batch of four points (the rows of $\mathbf{X}$). Each Step performs one operation: the matrix product, then the bias, then the activation. The faint grid is the previous stage. Below, the same four rows as numbers, with every shape written out.

The mathematics

The deck's layer, for a mini-batch of $m$ rows:

$$\underbrace{\mathbf{X}}_{m\times d}\,\underbrace{\mathbf{W}}_{d\times n} + \underbrace{\mathbf{b}}_{1\times n} \;=\; \underbrace{\mathbf{Z}}_{m\times n},$$ $$\mathbf{H} = \phi(\mathbf{Z})\quad (m\times n),$$

with $\mathbf{b}$ added to every row (broadcast) and $\phi$ applied entry by entry. Here $m = 4$, $d = n = 2$. The map $\mathbf{x}^{\mathsf T} \mapsto \mathbf{x}^{\mathsf T}\mathbf{W} + \mathbf{b}$ is affine, and an affine map sends straight lines to straight lines and parallel lines to parallel lines (the image of $\mathbf{p} + t\mathbf{v}$ is $\mathbf{p}'+ t\,\mathbf{v}'$). Only $\phi$ can bend.

Watch for
  • After the product and after the bias the grid is still made of straight, parallel, equally spaced lines: squares become parallelograms, nothing more.
  • The activation is where the picture changes character: tanh and sigmoid squash the far grid lines together into a bounded square; ReLU flattens every point with a negative coordinate onto an axis, folding the grid.
  • The shape strip never changes: $(4\times 2)(2\times 2) + (1\times 2) \to (4\times 2)$, and $\phi$ keeps it $(4\times 2)$.
Try this
  1. With ReLU, step to the end and count the points with a zero in their row of $\mathbf{H}$: they all sit on an axis of the plot.
  2. Choose the singular $\mathbf{W}$: after the product the whole plane lies on one line, and no later step can recover what was lost.
  3. Choose identity: the last step changes nothing, which is why a stack of such layers is one affine map.