A neural network is a function with many adjustable numbers, fitted to examples. This unit opens the neural-networks day with the smallest networks we can draw.
One neuron = a weighted sum, then a bend: it draws a line through feature space. It can do AND and OR, but not XOR.
A network is matrix × vector, then a bend, repeated. You have done the matrix part since the first unit.
Without the bend, any number of layers collapses to one matrix.
With the bend, simple hinges add up to curves, and lines add up to any boundary.
Many classes: one output per class, turned into probabilities by softmax.
Training = walking downhill on an error, one small step at a time.
Slides titled The math give the formulas and proofs; the ideas are complete without them.
Thresholding (day 12) decides “nucleus if intensity > t”: a cut on one measurement. Day 33 put objects as points in a feature space and let a classifier carve it up.
Two measurements per object (say area and mean intensity): each object is a point in the plane.
The simplest rule that uses both: a straight line, w_1x_1 + w_2x_2 + b > 0 means “nucleus”.
The numbers w_1, w_2, b are what we will learn from examples.
One neuron
A neuron adds its inputs with weights, adds a bias, then bends the result with an activation\varphi:
\text{out} = \varphi(w_1x_1 + w_2x_2 + b)
Step\varphi: a hard yes/no, the perceptron (1958).
Sigmoid\varphi(s) = 1/(1 + e^{-s}): a probability between 0 and 1.
ReLU\varphi(s) = \max(0, s): the workhorse of today’s networks.
The weights point across the line, to the positive side; the bias moves the line.
\mathbf w = (2, 1), b = -4: (2, 1) and (3, 0) are positive.
The math: a neuron is a half-plane
\text{out} = \varphi(\mathbf w^\top\mathbf x + b)
The decision boundary \mathbf w^\top\mathbf x + b = 0 is a line (a hyperplane in \mathbb R^n) with normal\mathbf w.
Its distance from the origin is |b|/\|\mathbf w\|; the signed distance of a point is (\mathbf w^\top\mathbf x + b)/\|\mathbf w\|.
With the sigmoid, the output is an S-shaped function of that signed distance times \|\mathbf w\|: larger \|\mathbf w\|, steeper transition, same line.
Example: \mathbf w = (2, 1), b = -4: distance 4/\sqrt5 = 1.79.
AND, OR, but not XOR
Inputs x_1, x_2 \in \{0, 1\}, step activation (1 if the sum > 0):
AND
OR
XOR
weights, bias
1, 1, -1.5
1, 1, -0.5
none exists
corners that are 1
(1,1)
all but (0,0)
(1,0) and (0,1)
XOR wants (1,0) and (0,1) on one side, (0,0) and (1,1) on the other: no single line does that.
This observation (Minsky and Papert, 1969) stalled neural networks for a decade. The answer: more than one neuron.
Each hidden unit is a neuron with its own line (green); the output combines them.
The hidden layer has turned the four corners into points that one line can separate (right).
A layer: matrix, then bend
\mathbf h = \text{ReLU}(A\mathbf x + \mathbf b), \qquad \text{out} = C\mathbf h + d, \qquad \text{ReLU}(s) = \max(0, s)
Each line in the picture is one number of A or C (a weight).
The bend (ReLU) acts on each entry separately: negatives become 0.
The entries of \mathbf h are the hidden units: every row of A is one neuron.
Later units write W^{(1)} for A and W^{(2)} for C: layer l computes \mathbf a^{(l)} = \varphi(W^{(l)}\mathbf a^{(l-1)} + \mathbf b^{(l)}).
A network as pseudocode
for every hidden unit i: # one row of A
s = b[i]
for every input j:
s = s + A[i][j] · x[j]
h[i] = max(0, s) # the bend
out = d + sum over i of C[i] · h[i]
Two loops of weighted sums, one bend in between: nothing more.
Leave out max(0, ·) and the two steps collapse into one matrix (two slides on).
One input through the network, by hand
A = \begin{pmatrix} 1 & 1 \\ 1 & -1 \\ -1 & 2 \end{pmatrix}, \mathbf b = \mathbf 0, C = \begin{pmatrix} 1 & 2 & 1 \end{pmatrix}, d = 0, input \mathbf x = (1, 2).
Check: (2, 1)\cdot(1, 2) = 4, the “no bend” answer.
2 or 100 layers with no bend in between are one matrix: a straight-line (linear) map.
The bend between the matrices is what lets a network draw curves.
Hinges add up to curves
A line scan across two nuclei. One input (position), three hidden units, one output.
Each hidden unit is a hinge: flat, then a straight rise (or the reverse), at its own position.
The output adds the hinges with weights: a bent line through the data.
More units → more hinges → any curve, as closely as we like.
Training: walk downhill on the error
How do we find the weights? Measure how wrong the network is (the loss, e.g. the mean squared error over the data), then nudge every weight a little in the direction that lowers it. Repeat thousands of times.
Picture the loss as a landscape over the weights: training walks downhill.
The learning rate is the step length: too small takes forever, too large overshoots to the other side of the valley.
The walk can stop in a dip that is not the deepest one: two trainings from different starts can give different networks.
The math: one gradient step
Step against the gradient: \mathbf w \leftarrow \mathbf w - \eta\, \nabla_{\mathbf w} L.
One weight, L(w) = (y - wx)^2 with x = 2, y = 3, start w = 0: L'(w) = -2x(y - wx), so L'(0) = -12. With \eta = 0.1: w \to 1.2, and L drops from 9 to 0.36.
Distance to the best w = 1.5 after one step: w_\text{new} - 1.5 = (1 - 8\eta)(w - 1.5). Converges for 0 < \eta < 0.25; \eta = 0.5 makes L grow to 81.
For a network, \nabla L comes from the chain rule applied layer by layer, from the output back to the input (backpropagation).
Try it: a network classifies points
Many classes: softmax
One output scorez_k per class; turn the scores into probabilities:
p_k = \frac{e^{z_k}}{\sum_j e^{z_j}}
All p_k are positive and sum to 1; the largest score gets the largest probability.
Scores (2, 1, 0) → probabilities (0.67, 0.24, 0.09). The predicted class is the largest.
Two classes with softmax are the same as one sigmoid output.
The math: softmax
Adding a constant to all scores does not change p: e^{z_k + c}/\sum_j e^{z_j + c} = e^{z_k}/\sum_j e^{z_j}. So (102, 101, 100) gives the same (0.665, 0.245, 0.090), and computers subtract \max_k z_k first to avoid overflow.
Two classes: p_1 = e^{z_1}/(e^{z_0} + e^{z_1}) = 1/(1 + e^{-(z_1 - z_0)}), the sigmoid of the score difference.
Universal approximation (Cybenko 1989 for sigmoids; the same holds for ReLU): one hidden layer with enough units approximates any continuous function on a bounded region as closely as we like. It says nothing about how many units, or whether training finds them.
Try it: fit a line scan
Images are long vectors
A 28 \times 28 image is a vector in \mathbb R^{784}. A layer from the image to 100 hidden units has
784 \times 100 + 100 = 78{,}500 \text{ numbers.}
Image networks easily have hundreds of thousands of weights. Training adjusts all of them at once, with the same downhill steps.
Many weights need many training images, or the network memorises instead of learning.
Such a layer has no idea that neighbouring pixels belong together: shift the digit by one pixel and most of the inputs that carry ink change. The convolutional networks of this day fix exactly that.
Summary
Idea
In one line
Neuron
weighted sum + bias, then a bend: a line in feature space
XOR
needs a hidden layer: no single line separates it
Layer
matrix × vector + shift, then ReLU on each entry
No bend
stacked matrices collapse to one matrix: a linear map
Hidden unit
one hinge; hinges add up to curves
Loss
how wrong the network is, e.g. mean squared error
Training
step against the gradient, \mathbf w \leftarrow \mathbf w - \eta\nabla L, many times
Softmax
scores → class probabilities that sum to 1
Learning rate
step size: too small is slow, too large overshoots
Next: how training really works (losses, gradients, backpropagation), then networks whose input is a whole image.