Artificial Intelligence Applications in Engineering (MUH-920), week 10 of 14

Neural Networks: From the Perceptron to Deep Learning

Prof. Dr. Utku Kose, Süleyman Demirel University

From neurons to perceptrons

McCulloch and Pitts described an idealised neuron that sums weighted binary inputs and fires when the sum exceeds a threshold, and showed that networks of such units can compute logical functions [1]. Rosenblatt's perceptron added learning: After each example, the weights move in the direction that corrects a wrong output, w = w + eta (y - y_hat) x [2]. If the two classes can be separated by a straight line, or a hyperplane in more dimensions, the rule finds a separating line in a finite number of steps.

Minsky and Papert analysed what single-layer perceptrons cannot do [3]. The exclusive or, XOR, is the classic example: The points (0, 0) and (1, 1) belong to one class and (0, 1) and (1, 0) to the other, and no straight line separates them. Engineering data are full of such interactions, for example when a fault appears only if temperature is high and speed is low. The remedy is a hidden layer that transforms the inputs into a space where the classes become separable.

Check your understanding. Why can a single perceptron not learn XOR?

Formulas: The threshold neuron and the perceptron rule

A threshold neuron multiplies its inputs x1,…,xnx_1, \ldots, x_n by weights w1,…,wnw_1, \ldots, w_n and fires when the weighted sum reaches a threshold [1]. Writing the threshold as a negative bias bb, the perceptron computes a score zz and a binary output y^\hat{y}:

z=w1x1+w2x2+⋯+wnxn+b=w⋅x+bz = w_1 x_1 + w_2 x_2 + \cdots + w_n x_n + b = w \cdot x + b
y^=1 if z≥0,    y^=0 if z<0\hat{y} = 1 \text{ if } z \ge 0, \;\; \hat{y} = 0 \text{ if } z < 0

The output changes where z=0z = 0, so the decision boundary w⋅x+b=0w \cdot x + b = 0 is a straight line for two inputs and a plane or hyperplane for more. The weight vector ww is perpendicular to the boundary, and an input lies at the distance ∣z∣/∥w∥\vert z \vert / \lVert w \rVert from it. Part A of the interactive lab shows this boundary for a single neuron with adjustable weights, bias and activation.

Rosenblatt's learning rule visits the training examples one at a time [2]. For an example with the target yy, the output y^\hat{y} and the error e=y−y^e = y - \hat{y}, it changes every weight and the bias with a learning rate η>0\eta > 0:

wj←wj+η e xj,    b←b+η ew_j \leftarrow w_j + \eta\, e\, x_j, \;\; b \leftarrow b + \eta\, e

A correct output gives e=0e = 0 and no change, a missed positive example gives e=1e = 1, and a false alarm gives e=−1e = -1. Inserting the new weights into the score of the same input shows why the rule corrects mistakes:

znew=(w+ηex)⋅x+b+ηe=z+ηe(∥x∥2+1)z_{\text{new}} = (w + \eta e x) \cdot x + b + \eta e = z + \eta e (\lVert x \rVert^2 + 1)

The score of the input rises after a missed positive and falls after a false alarm, although one correction may not be enough to change the output. If a hyperplane separates the two classes, the perceptron convergence theorem guarantees that the corrections stop after a finite number of mistakes. If no hyperplane separates them, every pass through the data contains a mistake, and the weights never settle [3].

Worked example: A perceptron learns the AND function

A two-hand control releases a press only when both of its buttons are pressed, which is the AND function of two binary inputs. A perceptron starts with w=(0.5,0.5)w = (0.5, 0.5) and b=0.1b = 0.1. Its scores for the inputs (0, 0), (0, 1), (1, 0) and (1, 1) are 0.1, 0.6, 0.6 and 1.1, so it fires for every input. It learns with η=0.2\eta = 0.2 and visits the examples in this order, with the targets 0, 0, 0 and 1.

Step 1. Input (0, 0), target 0. The score is z=0.5×0+0.5×0+0.1=0.1≥0z = 0.5 \times 0 + 0.5 \times 0 + 0.1 = 0.1 \ge 0, so y^=1\hat{y} = 1 and e=0−1=−1e = 0 - 1 = -1, a false alarm. The inputs are zero, so only the bias changes:

w1=0.5+0.2×(−1)×0=0.5,    w2=0.5+0.2×(−1)×0=0.5w_1 = 0.5 + 0.2 \times (-1) \times 0 = 0.5, \;\; w_2 = 0.5 + 0.2 \times (-1) \times 0 = 0.5
b=0.1+0.2×(−1)=−0.1b = 0.1 + 0.2 \times (-1) = -0.1

Step 2. Input (0, 1), target 0. The score is z=0.5×0+0.5×1−0.1=0.4≥0z = 0.5 \times 0 + 0.5 \times 1 - 0.1 = 0.4 \ge 0, another false alarm with e=−1e = -1:

w1=0.5+0.2×(−1)×0=0.5,    w2=0.5+0.2×(−1)×1=0.3w_1 = 0.5 + 0.2 \times (-1) \times 0 = 0.5, \;\; w_2 = 0.5 + 0.2 \times (-1) \times 1 = 0.3
b=−0.1+0.2×(−1)=−0.3b = -0.1 + 0.2 \times (-1) = -0.3

By the formula above, the score of this input falls by η(∥x∥2+1)=0.2×(1+1)=0.4\eta (\lVert x \rVert^2 + 1) = 0.2 \times (1 + 1) = 0.4, from 0.4 to exactly 0, which still fires. One correction is not enough here.

Step 3. Input (1, 0), target 0. The score is z=0.5×1+0.3×0−0.3=0.2≥0z = 0.5 \times 1 + 0.3 \times 0 - 0.3 = 0.2 \ge 0, a third false alarm:

w1=0.5+0.2×(−1)×1=0.3,    w2=0.3+0.2×(−1)×0=0.3w_1 = 0.5 + 0.2 \times (-1) \times 1 = 0.3, \;\; w_2 = 0.3 + 0.2 \times (-1) \times 0 = 0.3
b=−0.3+0.2×(−1)=−0.5b = -0.3 + 0.2 \times (-1) = -0.5

Step 4. Input (1, 1), target 1. The score is z=0.3×1+0.3×1−0.5=0.1≥0z = 0.3 \times 1 + 0.3 \times 1 - 0.5 = 0.1 \ge 0, so y^=1=y\hat{y} = 1 = y, and nothing changes.

Step 5. The second pass. With w=(0.3,0.3)w = (0.3, 0.3) and b=−0.5b = -0.5, every example is classified correctly, so training stops after a full pass without a mistake:

Input Score zz Output Target
(0, 0) 0.3×0+0.3×0−0.5=−0.50.3 \times 0 + 0.3 \times 0 - 0.5 = -0.5 0 0
(0, 1) 0.3×0+0.3×1−0.5=−0.20.3 \times 0 + 0.3 \times 1 - 0.5 = -0.2 0 0
(1, 0) 0.3×1+0.3×0−0.5=−0.20.3 \times 1 + 0.3 \times 0 - 0.5 = -0.2 0 0
(1, 1) 0.3×1+0.3×1−0.5=0.10.3 \times 1 + 0.3 \times 1 - 0.5 = 0.1 1 1

The boundary 0.3x1+0.3x2−0.5=00.3 x_1 + 0.3 x_2 - 0.5 = 0, or x1+x2=5/3≈1.6667x_1 + x_2 = 5/3 \approx 1.6667, leaves only (1, 1) on the firing side. Part B of the interactive lab replays these steps and tries other functions.

Worked example: Why the exclusive or needs a hidden layer

Step 1. No single perceptron computes it. Suppose that weights w1,w2w_1, w_2 and a bias bb computed the exclusive or, XOR. The outputs 0 for (0, 0) and (1, 1) and the outputs 1 for (0, 1) and (1, 0) would require, in this order,

b<0,    w1+w2+b<0,    w2+b≥0,    w1+b≥0b < 0, \;\; w_1 + w_2 + b < 0, \;\; w_2 + b \ge 0, \;\; w_1 + b \ge 0

Adding the last two inequalities gives w1+w2+2b≥0w_1 + w_2 + 2b \ge 0, while adding the first two gives w1+w2+2b<0w_1 + w_2 + 2b < 0. Both cannot hold, so no such weights exist [3].

Step 2. Two hidden units. The network of the notebook has a hidden unit h1h_1 with both weights equal to 1 and the bias −0.5, which computes OR, and a hidden unit h2h_2 with both weights equal to 1 and the bias −1.5, which computes AND. With step activations, the hidden layer gives

x1x_1 x2x_2 z1=x1+x2−0.5z_1 = x_1 + x_2 - 0.5 h1h_1 z2=x1+x2−1.5z_2 = x_1 + x_2 - 1.5 h2h_2
0 0 0+0−0.5=−0.50 + 0 - 0.5 = -0.5 0 0+0−1.5=−1.50 + 0 - 1.5 = -1.5 0
0 1 0+1−0.5=0.50 + 1 - 0.5 = 0.5 1 0+1−1.5=−0.50 + 1 - 1.5 = -0.5 0
1 0 1+0−0.5=0.51 + 0 - 0.5 = 0.5 1 1+0−1.5=−0.51 + 0 - 1.5 = -0.5 0
1 1 1+1−0.5=1.51 + 1 - 0.5 = 1.5 1 1+1−1.5=0.51 + 1 - 1.5 = 0.5 1

Step 3. The output unit. The output unit has the weights 1 and −1 and the bias −0.5:

Input h1h_1 h2h_2 z=h1−h2−0.5z = h_1 - h_2 - 0.5 y^\hat{y} XOR
(0, 0) 0 0 0−0−0.5=−0.50 - 0 - 0.5 = -0.5 0 0
(0, 1) 1 0 1−0−0.5=0.51 - 0 - 0.5 = 0.5 1 1
(1, 0) 1 0 1−0−0.5=0.51 - 0 - 0.5 = 0.5 1 1
(1, 1) 1 1 1−1−0.5=−0.51 - 1 - 0.5 = -0.5 0 0

Step 4. What the hidden layer does. In the plane of (h1,h2)(h_1, h_2), the four inputs become three points: (0, 0) for the input (0, 0), (1, 0) for both (0, 1) and (1, 0), and (1, 1) for the input (1, 1). The line h1−h2−0.5=0h_1 - h_2 - 0.5 = 0 separates the point (1, 0) from the other two, so the output unit, an ordinary perceptron, finishes the job. The hidden layer has transformed the inputs into a space in which the classes are linearly separable. Part B of the lab shows the perceptron rule failing on XOR, and Part C trains a network that finds such a transformation by itself.

Multilayer networks and backpropagation

A multilayer perceptron stacks layers of units. Each unit computes a weighted sum of the outputs of the previous layer and applies a nonlinear activation function: the logistic sigmoid, the hyperbolic tangent, or the rectified linear unit, ReLU, which returns max(0, z) and eases the training of deep networks [4]. Without nonlinear activations, any stack of layers collapses into one linear map. With them, a network with one sufficiently wide hidden layer can approximate any continuous function on a bounded domain to any accuracy [5, 6], although the theorem says nothing about how many units are needed or whether training finds them.

Training minimises a loss, such as the mean squared error for regression or the cross-entropy for classification, by gradient descent. Backpropagation computes the gradient of the loss with respect to every weight efficiently by applying the chain rule layer by layer from the output back to the input [7]. Each weight then moves a small step against its gradient. Modern frameworks such as PyTorch record the operations of the forward pass and compute these gradients automatically [8].

Animation: A small network learns a curved boundary

A network with one hidden layer is trained by gradient descent on points from two classes. Change the number of hidden units and the learning rate, and watch the boundary bend while the loss falls.

Check your understanding. What does backpropagation compute?

Formulas: Layers, activation functions and backpropagation

A layer ll receives the outputs ak(l−1)a^{(l-1)}_k of the previous layer, where a(0)=xa^{(0)} = x is the input. Each unit jj of the layer forms a weighted sum with its weights wjk(l)w^{(l)}_{jk} and its bias bj(l)b^{(l)}_j and applies an activation function gg:

zj(l)=∑kwjk(l) ak(l−1)+bj(l),    aj(l)=g(zj(l))z^{(l)}_j = \sum_k w^{(l)}_{jk}\, a^{(l-1)}_k + b^{(l)}_j, \;\; a^{(l)}_j = g(z^{(l)}_j)

The weight wjk(l)w^{(l)}_{jk} connects unit kk of layer l−1l - 1 to unit jj of layer ll. The common activation functions are the logistic sigmoid, the hyperbolic tangent and the rectified linear unit [4, 9]:

σ(z)=11+e−z,    tanh⁡z=ez−e−zez+e−z,    ReLU(z)=max⁡(0,z)\sigma(z) = \frac{1}{1 + e^{-z}}, \;\; \tanh z = \frac{e^{z} - e^{-z}}{e^{z} + e^{-z}}, \;\; \text{ReLU}(z) = \max(0, z)

Backpropagation needs their derivatives. For the sigmoid, the quotient rule and e−z/(1+e−z)=1−σ(z)e^{-z}/(1 + e^{-z}) = 1 - \sigma(z) give

σ′(z)=e−z(1+e−z)2=11+e−z×e−z1+e−z=σ(z) (1−σ(z))\sigma'(z) = \frac{e^{-z}}{(1 + e^{-z})^2} = \frac{1}{1 + e^{-z}} \times \frac{e^{-z}}{1 + e^{-z}} = \sigma(z)\,(1 - \sigma(z))

and for the other two

ddztanh⁡z=1−tanh⁡2z,    ReLU′(z)=1 for z>0 and 0 for z<0\frac{d}{dz} \tanh z = 1 - \tanh^2 z, \;\; \text{ReLU}'(z) = 1 \text{ for } z > 0 \text{ and } 0 \text{ for } z < 0

with the value 0 used in practice at z=0z = 0. The sigmoid has its largest slope, 0.25, at z=0z = 0, and the slopes of the sigmoid and of tanh approach zero for large ∣z∣\vert z \vert, where the unit saturates. ReLU passes the gradient unchanged wherever the unit is active.

Without an activation function, two layers with the weight matrices W1,W2W_1, W_2 and the bias vectors b1,b2b_1, b_2 collapse into a single linear map:

W2(W1x+b1)+b2=(W2W1) x+(W2b1+b2)W_2 (W_1 x + b_1) + b_2 = (W_2 W_1)\, x + (W_2 b_1 + b_2)

Stacking more linear layers adds nothing. With a nonlinear activation, one hidden layer of NN units computes

f(x)=∑j=1Ncj g(wj⋅x+bj)f(x) = \sum_{j=1}^{N} c_j\, g(w_j \cdot x + b_j)

and Cybenko and Hornik and colleagues proved that such sums with sigmoid-shaped activations approximate any continuous function on a bounded domain as closely as desired when NN is large enough [5, 6].

Training minimises a loss averaged over the nn training examples, usually the mean squared error for regression and, for an output probability p^\hat{p}, the cross-entropy of Week 8:

L=1n∑i=1n(y^i−yi)2L = \frac{1}{n} \sum_{i=1}^{n} (\hat{y}_i - y_i)^2
L=−1n∑i=1n[ yiln⁡p^i+(1−yi)ln⁡(1−p^i) ]L = -\frac{1}{n} \sum_{i=1}^{n} [\, y_i \ln \hat{p}_i + (1 - y_i) \ln(1 - \hat{p}_i)\,]

Backpropagation computes the derivative of the loss with respect to every weight with the chain rule [7]. The central quantity is the error signal δj=∂L/∂zj\delta_j = \partial L / \partial z_j of each unit, the sensitivity of the loss to the weighted sum of the unit. For an output unit,

δj=∂L∂aj g′(zj)\delta_j = \frac{\partial L}{\partial a_j}\, g'(z_j)

and a hidden unit jj of layer ll, whose output feeds the units ii of layer l+1l + 1, collects the error signals of these units through the connecting weights:

δj(l)=g′(zj(l))∑iwij(l+1) δi(l+1)\delta^{(l)}_j = g'(z^{(l)}_j) \sum_i w^{(l+1)}_{ij}\, \delta^{(l+1)}_i

The gradient of a weight is the error signal of the unit that the weight enters times the activation that it carries, and the gradient of a bias is the error signal itself:

∂L∂wjk(l)=δj(l) ak(l−1),    ∂L∂bj(l)=δj(l)\frac{\partial L}{\partial w^{(l)}_{jk}} = \delta^{(l)}_j\, a^{(l-1)}_k, \;\; \frac{\partial L}{\partial b^{(l)}_j} = \delta^{(l)}_j

The error signals are computed from the output backwards, which gives the method its name, and then every parameter takes a gradient descent step w←w−η ∂L/∂ww \leftarrow w - \eta\, \partial L / \partial w. For a sigmoid output with the cross-entropy of one example, the output error signal becomes very simple:

δ=(−yp^+1−y1−p^) p^ (1−p^)\delta = \Big( -\frac{y}{\hat{p}} + \frac{1 - y}{1 - \hat{p}} \Big)\, \hat{p}\, (1 - \hat{p})
δ=−y (1−p^)+(1−y) p^=p^−y\delta = -y\, (1 - \hat{p}) + (1 - y)\, \hat{p} = \hat{p} - y

This is the error signal at the output of the notebook's network. Frameworks such as PyTorch record the operations of the forward pass and apply the chain rule automatically [8].

Worked example: One backpropagation step by hand

A network with two inputs, two sigmoid hidden units and one sigmoid output receives the example x=(0.5,1)x = (0.5, 1) with the target y=1y = 1. Hidden unit jj has the weights wj1,wj2w_{j1}, w_{j2} and the bias bjb_j, and the output unit has the weights v1,v2v_1, v_2 and the bias bob_{\text{o}}:

Unit Weights Bias
Hidden unit 1 w11=0.4w_{11} = 0.4, w12=−0.2w_{12} = -0.2 b1=0.1b_1 = 0.1
Hidden unit 2 w21=0.3w_{21} = 0.3, w22=0.6w_{22} = 0.6 b2=−0.1b_2 = -0.1
Output unit v1=0.7v_1 = 0.7, v2=−0.5v_2 = -0.5 bo=0.2b_{\text{o}} = 0.2

The loss of the example is L=12(y^−y)2L = \frac{1}{2} (\hat{y} - y)^2, where the factor 1/2 cancels the 2 of the derivative, and the learning rate is η=0.5\eta = 0.5. For this network, the formulas of backpropagation read

δo=(y^−y) y^ (1−y^),    δj=vj δo hj(1−hj)\delta_{\text{o}} = (\hat{y} - y)\, \hat{y}\, (1 - \hat{y}), \;\; \delta_j = v_j\, \delta_{\text{o}}\, h_j (1 - h_j)
∂L∂vj=δo hj,    ∂L∂wjk=δj xk\frac{\partial L}{\partial v_j} = \delta_{\text{o}}\, h_j, \;\; \frac{\partial L}{\partial w_{jk}} = \delta_j\, x_k

All values are rounded to six decimals, and every later step uses the rounded values.

Step 1. Forward pass through the hidden layer.

z1=0.4×0.5+(−0.2)×1+0.1=0.2−0.2+0.1=0.1z_1 = 0.4 \times 0.5 + (-0.2) \times 1 + 0.1 = 0.2 - 0.2 + 0.1 = 0.1
h1=σ(0.1)=11+e−0.1=11+0.904837=0.524979h_1 = \sigma(0.1) = \frac{1}{1 + e^{-0.1}} = \frac{1}{1 + 0.904837} = 0.524979
z2=0.3×0.5+0.6×1+(−0.1)=0.15+0.6−0.1=0.65z_2 = 0.3 \times 0.5 + 0.6 \times 1 + (-0.1) = 0.15 + 0.6 - 0.1 = 0.65
h2=σ(0.65)=11+e−0.65=11+0.522046=0.65701h_2 = \sigma(0.65) = \frac{1}{1 + e^{-0.65}} = \frac{1}{1 + 0.522046} = 0.65701

Step 2. Output and loss.

zo=0.7×0.524979+(−0.5)×0.65701+0.2z_{\text{o}} = 0.7 \times 0.524979 + (-0.5) \times 0.65701 + 0.2
zo=0.367485−0.328505+0.2=0.23898z_{\text{o}} = 0.367485 - 0.328505 + 0.2 = 0.23898
y^=σ(0.23898)=11+e−0.23898=11+0.787431=0.559462\hat{y} = \sigma(0.23898) = \frac{1}{1 + e^{-0.23898}} = \frac{1}{1 + 0.787431} = 0.559462
L=12(0.559462−1)2=12×(−0.440538)2L = \frac{1}{2} (0.559462 - 1)^2 = \frac{1}{2} \times (-0.440538)^2
L=12×0.194074=0.097037L = \frac{1}{2} \times 0.194074 = 0.097037

Step 3. Error signal of the output. The derivative of the loss is ∂L/∂y^=y^−y=−0.440538\partial L / \partial \hat{y} = \hat{y} - y = -0.440538, and the slope of the sigmoid is y^(1−y^)=0.559462×0.440538=0.246464\hat{y}(1 - \hat{y}) = 0.559462 \times 0.440538 = 0.246464, so

δo=−0.440538×0.246464=−0.108577\delta_{\text{o}} = -0.440538 \times 0.246464 = -0.108577

Step 4. Gradients of the output unit.

∂L∂v1=δo h1=−0.108577×0.524979=−0.057001\frac{\partial L}{\partial v_1} = \delta_{\text{o}}\, h_1 = -0.108577 \times 0.524979 = -0.057001
∂L∂v2=δo h2=−0.108577×0.65701=−0.071336\frac{\partial L}{\partial v_2} = \delta_{\text{o}}\, h_2 = -0.108577 \times 0.65701 = -0.071336

and ∂L/∂bo=δo=−0.108577\partial L / \partial b_{\text{o}} = \delta_{\text{o}} = -0.108577.

Step 5. Error signals of the hidden units. The slopes of the hidden units are h1(1−h1)=0.524979×0.475021=0.249376h_1 (1 - h_1) = 0.524979 \times 0.475021 = 0.249376 and h2(1−h2)=0.65701×0.34299=0.225348h_2 (1 - h_2) = 0.65701 \times 0.34299 = 0.225348. The output error travels back through the output weights as they were before the update:

δ1=0.7×(−0.108577)×0.249376=−0.018954\delta_1 = 0.7 \times (-0.108577) \times 0.249376 = -0.018954
δ2=(−0.5)×(−0.108577)×0.225348=0.012234\delta_2 = (-0.5) \times (-0.108577) \times 0.225348 = 0.012234

The second hidden unit receives a positive error signal because its output weight is negative: Increasing its weighted sum would lower y^\hat{y} and raise the loss.

Step 6. Gradients of the hidden units. With x1=0.5x_1 = 0.5 and x2=1x_2 = 1:

Parameter Gradient
w11w_{11} δ1x1=−0.018954×0.5=−0.009477\delta_1 x_1 = -0.018954 \times 0.5 = -0.009477
w12w_{12} δ1x2=−0.018954×1=−0.018954\delta_1 x_2 = -0.018954 \times 1 = -0.018954
b1b_1 δ1=−0.018954\delta_1 = -0.018954
w21w_{21} δ2x1=0.012234×0.5=0.006117\delta_2 x_1 = 0.012234 \times 0.5 = 0.006117
w22w_{22} δ2x2=0.012234×1=0.012234\delta_2 x_2 = 0.012234 \times 1 = 0.012234
b2b_2 δ2=0.012234\delta_2 = 0.012234

Step 7. The update. Every parameter moves against its gradient with η=0.5\eta = 0.5:

Parameter Old value Gradient New value
w11w_{11} 0.4 −0.009477 0.4−0.5×(−0.009477)=0.40473850.4 - 0.5 \times (-0.009477) = 0.4047385
w12w_{12} −0.2 −0.018954 −0.2−0.5×(−0.018954)=−0.190523-0.2 - 0.5 \times (-0.018954) = -0.190523
b1b_{1} 0.1 −0.018954 0.1−0.5×(−0.018954)=0.1094770.1 - 0.5 \times (-0.018954) = 0.109477
w21w_{21} 0.3 0.006117 0.3−0.5×0.006117=0.29694150.3 - 0.5 \times 0.006117 = 0.2969415
w22w_{22} 0.6 0.012234 0.6−0.5×0.012234=0.5938830.6 - 0.5 \times 0.012234 = 0.593883
b2b_{2} −0.1 0.012234 −0.1−0.5×0.012234=−0.106117-0.1 - 0.5 \times 0.012234 = -0.106117
v1v_{1} 0.7 −0.057001 0.7−0.5×(−0.057001)=0.72850050.7 - 0.5 \times (-0.057001) = 0.7285005
v2v_{2} −0.5 −0.071336 −0.5−0.5×(−0.071336)=−0.464332-0.5 - 0.5 \times (-0.071336) = -0.464332
bob_{\text{o}} 0.2 −0.108577 0.2−0.5×(−0.108577)=0.25428850.2 - 0.5 \times (-0.108577) = 0.2542885

Step 8. The effect. A forward pass with the new parameters, with the products rounded to six decimals, gives

z1=0.4047385×0.5+(−0.190523)×1+0.109477z_1 = 0.4047385 \times 0.5 + (-0.190523) \times 1 + 0.109477
z1=0.202369−0.190523+0.109477=0.121323z_1 = 0.202369 - 0.190523 + 0.109477 = 0.121323
h1=11+e−0.121323=11+0.885748=0.530294h_1 = \frac{1}{1 + e^{-0.121323}} = \frac{1}{1 + 0.885748} = 0.530294
z2=0.2969415×0.5+0.593883×1+(−0.106117)z_2 = 0.2969415 \times 0.5 + 0.593883 \times 1 + (-0.106117)
z2=0.148471+0.593883−0.106117=0.636237z_2 = 0.148471 + 0.593883 - 0.106117 = 0.636237
h2=11+e−0.636237=11+0.52928=0.653902h_2 = \frac{1}{1 + e^{-0.636237}} = \frac{1}{1 + 0.52928} = 0.653902
zo=0.7285005×0.530294+(−0.464332)×0.653902+0.2542885z_{\text{o}} = 0.7285005 \times 0.530294 + (-0.464332) \times 0.653902 + 0.2542885
zo=0.386319−0.303628+0.2542885=0.3369795z_{\text{o}} = 0.386319 - 0.303628 + 0.2542885 = 0.3369795
y^=11+e−0.3369795=11+0.713923=0.583457\hat{y} = \frac{1}{1 + e^{-0.3369795}} = \frac{1}{1 + 0.713923} = 0.583457
L=12×(−0.416543)2=0.086754L = \frac{1}{2} \times (-0.416543)^2 = 0.086754

The loss has fallen from 0.097037 to 0.086754, and the output has moved from 0.559462 towards the target 1, to 0.583457.

Step 9. A numerical check. A gradient can be checked with a central difference, whose error shrinks with the square of the step hh:

∂L∂v1≈L(v1+h)−L(v1−h)2h\frac{\partial L}{\partial v_1} \approx \frac{L(v_1 + h) - L(v_1 - h)}{2h}

Recomputing the loss without rounding for v1=0.701v_1 = 0.701 and v1=0.699v_1 = 0.699, with h=0.001h = 0.001, gives L=0.0969797332L = 0.0969797332 and L=0.0970937343L = 0.0970937343, so that

0.0969797332−0.09709373430.002=−0.00011400110.002=−0.0570005\frac{0.0969797332 - 0.0970937343}{0.002} = \frac{-0.0001140011}{0.002} = -0.0570005

which agrees with δoh1=−0.057001\delta_{\text{o}} h_1 = -0.057001 from Step 4. The notebook checks a gradient of its XOR network in the same way, and Part D of the interactive lab repeats this example number by number for any weights.

Optimisation in practice

Plain gradient descent uses the whole training set for every step. Stochastic gradient descent uses small random mini-batches, which is faster and adds noise that can help to escape poor regions. Momentum accumulates a moving average of gradients and damps oscillation across narrow valleys. Adam adapts the step size of every weight from running estimates of the first and second moments of its gradient and is a robust default [10]. The learning rate remains the most important setting: Too small and training crawls, too large and the loss oscillates or diverges.

Initialisation matters because signals must neither vanish nor explode as they pass through many layers. Glorot and Bengio proposed scaling the initial weights with the number of inputs and outputs of a layer [11]. Batch normalisation standardises the inputs of a layer over each mini-batch and often allows larger learning rates [12]. Inputs of different units, such as temperatures in degrees and pressures in millibars, should be standardised before training, as for k-means and support vector machines.

Formulas: Gradient descent, momentum, Adam, initialisation and normalisation

Training minimises the average loss L(θ)=1n∑iℓi(θ)L(\theta) = \frac{1}{n} \sum_{i} \ell_i(\theta) of the nn training examples, where θ\theta collects all weights and biases and ℓi\ell_i is the loss of example ii. Gradient descent takes steps against the gradient ∇L\nabla L, the vector of all partial derivatives. Stochastic gradient descent estimates the gradient from a random mini-batch BB of ∣B∣\vert B \vert examples [9]:

g=1∣B∣∑i∈B∇ℓi(θ),    θ←θ−η gg = \frac{1}{\vert B \vert} \sum_{i \in B} \nabla \ell_i(\theta), \;\; \theta \leftarrow \theta - \eta\, g

One pass through all mini-batches is an epoch, so an epoch takes n/∣B∣n / \vert B \vert steps. Momentum keeps a velocity vv, a decaying sum of past steps with a factor α\alpha, typically 0.9 [7, 9]:

v←α v−η g,    θ←θ+vv \leftarrow \alpha\, v - \eta\, g, \;\; \theta \leftarrow \theta + v

Steps that point the same way accumulate, and steps that alternate across a narrow valley cancel. Adam keeps running means of the gradient and of its square for every parameter, corrects their bias towards zero in the first steps, and divides the first by the square root of the second [10]:

m←β1m+(1−β1) g,    s←β2s+(1−β2) g2m \leftarrow \beta_1 m + (1 - \beta_1)\, g, \;\; s \leftarrow \beta_2 s + (1 - \beta_2)\, g^2
m^=m1−β1t,    s^=s1−β2t,    θ←θ−η m^s^+ϵ\hat{m} = \frac{m}{1 - \beta_1^t}, \;\; \hat{s} = \frac{s}{1 - \beta_2^t}, \;\; \theta \leftarrow \theta - \eta\, \frac{\hat{m}}{\sqrt{\hat{s}} + \epsilon}

Here tt counts the steps, mm and ss start at zero, and the defaults are β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999 and ϵ=10−8\epsilon = 10^{-8}. All operations act on every parameter separately.

The learning rate decides whether gradient descent converges at all. For the loss L(w)=a(w−w⋆)2L(w) = a (w - w^{\star})^2 with a>0a > 0, the gradient is 2a(w−w⋆)2a (w - w^{\star}), and one step changes the error e=w−w⋆e = w - w^{\star} to

e←e−2aη e=(1−2aη) ee \leftarrow e - 2 a \eta\, e = (1 - 2 a \eta)\, e

The error shrinks steadily for 0<η≤1/(2a)0 < \eta \le 1/(2a), shrinks with an alternating sign for 1/(2a)<η<1/a1/(2a) < \eta < 1/a, and does not shrink for η≥1/a\eta \ge 1/a. In many dimensions, the steepest direction limits the learning rate, while the flattest direction sets the speed, which is the difficulty of the narrow valley in Part E of the lab.

Initialisation sets the scale of the signals. For a weighted sum z=∑k=1ninwkakz = \sum_{k=1}^{n_{\text{in}}} w_k a_k of independent inputs with mean zero and independent weights with mean zero, the variances multiply:

Var(z)=nin Var(w) Var(a)\text{Var}(z) = n_{\text{in}}\, \text{Var}(w)\, \text{Var}(a)

Keeping the variance of the signals from layer to layer requires Var(w)=1/nin\text{Var}(w) = 1/n_{\text{in}}, and keeping the variance of the error signals on the way back requires Var(w)=1/nout\text{Var}(w) = 1/n_{\text{out}}, where noutn_{\text{out}} is the number of units of the layer. Glorot and Bengio proposed the compromise [11]

Var(w)=2nin+nout,    w∼U(−r,r) with r=6nin+nout\text{Var}(w) = \frac{2}{n_{\text{in}} + n_{\text{out}}}, \;\; w \sim U(-r, r) \text{ with } r = \sqrt{\frac{6}{n_{\text{in}} + n_{\text{out}}}}

since a uniform distribution on the interval from −r-r to rr has the variance r2/3r^2/3.

Batch normalisation standardises the weighted sums x1,…,xmx_1, \ldots, x_m of a unit over each mini-batch of mm examples and then rescales them with two learned parameters, γ\gamma and β\beta [12]:

μB=1m∑i=1mxi,    σB2=1m∑i=1m(xi−μB)2\mu_B = \frac{1}{m} \sum_{i=1}^{m} x_i, \;\; \sigma_B^2 = \frac{1}{m} \sum_{i=1}^{m} (x_i - \mu_B)^2
x^i=xi−μBσB2+ϵ,    yi=γ x^i+β\hat{x}_i = \frac{x_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}, \;\; y_i = \gamma\, \hat{x}_i + \beta

The small constant ϵ\epsilon prevents a division by zero. At test time, running averages of μB\mu_B and σB2\sigma_B^2 collected during training take the place of the statistics of the batch.

Worked example: Three optimisers on one loss

The loss L(w)=(w−3)2L(w) = (w - 3)^2 has its minimum at w=3w = 3 and the gradient g=2(w−3)g = 2(w - 3). All three optimisers start at w0=0w_0 = 0 with η=0.1\eta = 0.1. Momentum uses α=0.9\alpha = 0.9, and Adam uses its defaults.

Step 1. Gradient descent.

g1=2×(0−3)=−6,    w1=0−0.1×(−6)=0.6g_1 = 2 \times (0 - 3) = -6, \;\; w_1 = 0 - 0.1 \times (-6) = 0.6
g2=2×(0.6−3)=−4.8,    w2=0.6−0.1×(−4.8)=1.08g_2 = 2 \times (0.6 - 3) = -4.8, \;\; w_2 = 0.6 - 0.1 \times (-4.8) = 1.08

Step 2. Momentum. With v0=0v_0 = 0, the first step equals that of gradient descent, so the second gradient is again g2=−4.8g_2 = -4.8:

v1=0.9×0−0.1×(−6)=0.6,    w1=0+0.6=0.6v_1 = 0.9 \times 0 - 0.1 \times (-6) = 0.6, \;\; w_1 = 0 + 0.6 = 0.6
v2=0.9×0.6−0.1×(−4.8)=0.54+0.48=1.02v_2 = 0.9 \times 0.6 - 0.1 \times (-4.8) = 0.54 + 0.48 = 1.02
w2=0.6+1.02=1.62w_2 = 0.6 + 1.02 = 1.62

Step 3. Adam, first step. With g1=−6g_1 = -6 and g12=36g_1^2 = 36:

m1=0.9×0+0.1×(−6)=−0.6,    s1=0.999×0+0.001×36=0.036m_1 = 0.9 \times 0 + 0.1 \times (-6) = -0.6, \;\; s_1 = 0.999 \times 0 + 0.001 \times 36 = 0.036
m^1=−0.61−0.9=−6,    s^1=0.0361−0.999=36\hat{m}_1 = \frac{-0.6}{1 - 0.9} = -6, \;\; \hat{s}_1 = \frac{0.036}{1 - 0.999} = 36
w1=0−0.1×−636+10−8=0.1w_1 = 0 - 0.1 \times \frac{-6}{\sqrt{36} + 10^{-8}} = 0.1

The bias correction undoes the start at zero, and the first step has the length η\eta whatever the size of the gradient, because m^1/s^1=g1/∣g1∣\hat{m}_1 / \sqrt{\hat{s}_1} = g_1 / \vert g_1 \vert.

Step 4. Adam, second step. Now g2=2×(0.1−3)=−5.8g_2 = 2 \times (0.1 - 3) = -5.8 and g22=33.64g_2^2 = 33.64:

m2=0.9×(−0.6)+0.1×(−5.8)=−0.54−0.58=−1.12m_2 = 0.9 \times (-0.6) + 0.1 \times (-5.8) = -0.54 - 0.58 = -1.12
s2=0.999×0.036+0.001×33.64=0.035964+0.03364=0.069604s_2 = 0.999 \times 0.036 + 0.001 \times 33.64 = 0.035964 + 0.03364 = 0.069604
m^2=−1.121−0.92=−1.120.19=−5.894737\hat{m}_2 = \frac{-1.12}{1 - 0.9^2} = \frac{-1.12}{0.19} = -5.894737
s^2=0.0696041−0.9992=0.0696040.001999=34.81941\hat{s}_2 = \frac{0.069604}{1 - 0.999^2} = \frac{0.069604}{0.001999} = 34.81941

With 34.81941=5.900797\sqrt{34.81941} = 5.900797, the step is

w2=0.1−0.1×−5.8947375.900797=0.1−0.1×(−0.998973)=0.199897w_2 = 0.1 - 0.1 \times \frac{-5.894737}{5.900797} = 0.1 - 0.1 \times (-0.998973) = 0.199897

Step 5. Comparison. After two steps:

Optimiser w2w_2 L(w2)=(w2−3)2L(w_2) = (w_2 - 3)^2
Gradient descent 1.08 (−1.92)2=3.6864(-1.92)^2 = 3.6864
Momentum 1.62 (−1.38)2=1.9044(-1.38)^2 = 1.9044
Adam 0.199897 (−2.800103)2=7.840577(-2.800103)^2 = 7.840577

Momentum is fastest here because its steps accumulate. Adam is slowest because its steps have a length of about η=0.1\eta = 0.1, so with Adam the learning rate is roughly the largest change of a weight in one step [10].

Step 6. The learning rate. Here a=1a = 1, so the error factor is 1−2η1 - 2\eta. With η=0.1\eta = 0.1, the factor 0.8 turns the error e0=0−3=−3e_0 = 0 - 3 = -3 into −2.4 and −1.92, the values of Step 1. With η=0.75\eta = 0.75, the factor is −0.5: The errors are 1.5 and −0.75, so ww jumps to 4.5 and back to 2.25 while it converges. With η=1.1\eta = 1.1, the factor is −1.2: The errors are 3.6 and −4.32, so ww goes to 6.6 and then to −1.32, and the iteration diverges. Part F of the interactive lab runs all three optimisers on any loss of this form.

Worked example: Initial weights and a normalised mini-batch

Step 1. The Glorot range. The first layer of a network for the eight inputs of the concrete data of Week 7 has 16 units, so nin=8n_{\text{in}} = 8 and nout=16n_{\text{out}} = 16:

r=68+16=0.25=0.5,    Var(w)=0.523=112≈0.083333r = \sqrt{\frac{6}{8 + 16}} = \sqrt{0.25} = 0.5, \;\; \text{Var}(w) = \frac{0.5^2}{3} = \frac{1}{12} \approx 0.083333

which equals 2/(8+16)=2/24=1/122/(8 + 16) = 2/24 = 1/12.

Step 2. The signal of one unit. For standardised inputs, with the variance 1, the weighted sum of a unit has

Var(z)=8×112×1=23≈0.666667,    2/3=0.816497\text{Var}(z) = 8 \times \frac{1}{12} \times 1 = \frac{2}{3} \approx 0.666667, \;\; \sqrt{2/3} = 0.816497

A tanh unit whose weighted sum lies one standard deviation from zero, at z=0.816497z = 0.816497, has the output tanh⁡0.816497=0.673159\tanh 0.816497 = 0.673159 and the slope 1−0.6731592=0.5468571 - 0.673159^2 = 0.546857, so it passes gradients well. With weights drawn from U(−3,3)U(-3, 3) instead, Var(w)=32/3=3\text{Var}(w) = 3^2/3 = 3 and Var(z)=8×3=24\text{Var}(z) = 8 \times 3 = 24, so the standard deviation is 24=4.898979\sqrt{24} = 4.898979. At this typical value, tanh⁡4.898979=0.999889\tanh 4.898979 = 0.999889 and the slope is 1−0.9998892=0.0002221 - 0.999889^2 = 0.000222: The unit is saturated and passes almost no gradient back.

Step 3. Batch normalisation. One unit produces the weighted sums 2, 4, 6 and 8 for a mini-batch of four examples. With ϵ=10−5\epsilon = 10^{-5}, the default of PyTorch:

μB=2+4+6+84=204=5\mu_B = \frac{2 + 4 + 6 + 8}{4} = \frac{20}{4} = 5
σB2=(−3)2+(−1)2+12+324=9+1+1+94=5\sigma_B^2 = \frac{(-3)^2 + (-1)^2 + 1^2 + 3^2}{4} = \frac{9 + 1 + 1 + 9}{4} = 5
σB2+ϵ=5.00001=2.23607\sqrt{\sigma_B^2 + \epsilon} = \sqrt{5.00001} = 2.23607
x^1=−32.23607=−1.34164,    x^2=−12.23607=−0.447213\hat{x}_1 = \frac{-3}{2.23607} = -1.34164, \;\; \hat{x}_2 = \frac{-1}{2.23607} = -0.447213

and by symmetry x^3=0.447213\hat{x}_3 = 0.447213 and x^4=1.34164\hat{x}_4 = 1.34164. With γ=2\gamma = 2 and β=1\beta = 1:

y1=2×(−1.34164)+1=−1.68328,    y2=2×(−0.447213)+1=0.105574y_1 = 2 \times (-1.34164) + 1 = -1.68328, \;\; y_2 = 2 \times (-0.447213) + 1 = 0.105574
y3=2×0.447213+1=1.894426,    y4=2×1.34164+1=3.68328y_3 = 2 \times 0.447213 + 1 = 1.894426, \;\; y_4 = 2 \times 1.34164 + 1 = 3.68328

The normalised values have the mean 0 and the variance 5/5.00001=0.9999985/5.00001 = 0.999998, so the outputs have the mean β=1\beta = 1 and a standard deviation very close to γ=2\gamma = 2. Because γ\gamma and β\beta are learned, the network can undo the normalisation where that helps. Part G of the lab follows signals through many layers and normalises any mini-batch.

Generalisation and regularisation

Networks with many weights can memorise their training data. The standard defences are a validation set with early stopping, which keeps the weights from the epoch with the lowest validation loss; weight decay, which penalises large weights like ridge regression; and dropout, which randomly switches off units during training so that the network cannot rely on any single unit [13]. Learning curves of training and validation loss over epochs show whether a network underfits, with both losses high, or overfits, with a growing gap.

On tabular data of moderate size, the tree ensembles of Weeks 7 and 8 are often as accurate as networks and easier to tune, so a network must earn its place by a fair comparison. Networks have long been applied in engineering: Yeh modelled concrete strength with them [14], and metaheuristics such as the ant lion optimiser have been used to train them where gradient methods struggle [15]. Their decisive advantages appear with images, signals, sequences and text, where they learn features that would otherwise have to be designed by hand [16]. Weeks 11 to 13 follow this path.

Check your understanding. Training loss keeps falling, while validation loss has been rising for 30 epochs. What should be done?

Formulas: Parameters, weight decay, dropout and early stopping

A fully connected layer with nl−1n_{l-1} inputs and nln_l units has nl−1nln_{l-1} n_l weights and nln_l biases. A network with the layer sizes n0,n1,…,nKn_0, n_1, \ldots, n_K, from the inputs to the outputs, therefore has

P=∑l=1K(nl−1+1) nlP = \sum_{l=1}^{K} (n_{l-1} + 1)\, n_l

trainable parameters. When PP approaches the number of training examples, the network can fit the training data closely, noise included.

Weight decay adds the squared weights to the loss with a factor λ\lambda, as ridge regression does in Week 7 [9]:

Lλ(w)=L(w)+λ2∑jwj2,    ∂Lλ∂wj=∂L∂wj+λ wjL_{\lambda}(w) = L(w) + \frac{\lambda}{2} \sum_j w_j^2, \;\; \frac{\partial L_{\lambda}}{\partial w_j} = \frac{\partial L}{\partial w_j} + \lambda\, w_j

A gradient step therefore shrinks every weight by the factor 1−ηλ1 - \eta \lambda before it applies the gradient of the data:

wj←wj−η(∂L∂wj+λwj)=(1−ηλ) wj−η ∂L∂wjw_j \leftarrow w_j - \eta \Big( \frac{\partial L}{\partial w_j} + \lambda w_j \Big) = (1 - \eta \lambda)\, w_j - \eta\, \frac{\partial L}{\partial w_j}

Weights that the data do not support decay towards zero.

Dropout switches units off at random during training [13]. Each output hjh_j of a layer is multiplied by an independent mask value mjm_j, which is 1 with the keep probability qq and 0 otherwise. Deep learning libraries such as PyTorch also divide by qq, so that the expected value of every output stays the same:

h~j=mjhjq,    E[h~j]=q×hjq+(1−q)×0=hj\tilde{h}_j = \frac{m_j h_j}{q}, \;\; \text{E}[\tilde{h}_j] = q \times \frac{h_j}{q} + (1 - q) \times 0 = h_j

At test time, all units are used without a mask. Srivastava and colleagues multiplied the outgoing weights by qq at test time instead, which gives the same expected values. In PyTorch, the argument pp of the dropout layer is the probability of dropping a unit, p=1−qp = 1 - q.

Early stopping computes the validation loss Lval(e)L_{\text{val}}(e) after every epoch ee and keeps the weights of the epoch with the lowest value,

e⋆=arg⁡min⁡eLval(e)e^{\star} = \arg\min_{e} L_{\text{val}}(e)

It stops training when the best value has not improved for kk consecutive epochs, the patience. The difference Lval−LtrainL_{\text{val}} - L_{\text{train}}, the generalisation gap, grows when the network starts to memorise its training data.

Worked example: Parameters and weight decay of a small network

Step 1. Counting parameters. A network for the concrete data of Week 7, with the seven ingredients and the age as its eight inputs, two hidden layers of 16 units and one output, has the layer sizes 8, 16, 16 and 1:

P=(8+1)×16+(16+1)×16+(16+1)×1=144+272+17=433P = (8 + 1) \times 16 + (16 + 1) \times 16 + (16 + 1) \times 1 = 144 + 272 + 17 = 433

Step 2. Data per parameter. The data set has 1030 mixes, which gives 1030/433=2.381030 / 433 = 2.38 examples per parameter. A training split of 80% keeps 0.8×1030=8240.8 \times 1030 = 824 mixes and 824/433=1.9824 / 433 = 1.9 examples per parameter. Such a network can fit noise unless it is regularised and checked on validation data.

Step 3. One step with weight decay. A weight w=0.8w = 0.8 has the gradient ∂L/∂w=0.1\partial L / \partial w = 0.1, the learning rate is η=0.1\eta = 0.1, and λ=0.01\lambda = 0.01. The shrink factor is 1−0.1×0.01=0.9991 - 0.1 \times 0.01 = 0.999, so

w←0.999×0.8−0.1×0.1=0.7992−0.01=0.7892w \leftarrow 0.999 \times 0.8 - 0.1 \times 0.1 = 0.7992 - 0.01 = 0.7892

Without weight decay, the step would give 0.8−0.01=0.790.8 - 0.01 = 0.79.

Step 4. Decay in the long run. A weight that receives no gradient from the data keeps the fraction 0.999 of its value in every step. After 1000 steps it keeps

0.9991000=e1000ln⁡0.999=e1000×(−0.0010005)=e−1.0005=0.3676950.999^{1000} = e^{1000 \ln 0.999} = e^{1000 \times (-0.0010005)} = e^{-1.0005} = 0.367695

of its value, close to e−ηλt=e−0.001×1000=e−1e^{-\eta \lambda t} = e^{-0.001 \times 1000} = e^{-1} after tt steps. Part H of the interactive lab counts parameters, applies weight decay and draws dropout masks.

Worked example: Dropout and early stopping

Step 1. One dropout mask. A hidden layer has the outputs h=(0.8,0.3,0.5,0.9)h = (0.8, 0.3, 0.5, 0.9), and a unit of the next layer has the weights (0.5,−0.4,0.3,0.2)(0.5, -0.4, 0.3, 0.2). At test time, its weighted sum is

z=0.5×0.8+(−0.4)×0.3+0.3×0.5+0.2×0.9z = 0.5 \times 0.8 + (-0.4) \times 0.3 + 0.3 \times 0.5 + 0.2 \times 0.9
z=0.4−0.12+0.15+0.18=0.61z = 0.4 - 0.12 + 0.15 + 0.18 = 0.61

During training with q=0.5q = 0.5, the mask m=(1,0,1,0)m = (1, 0, 1, 0) keeps the first and third units and doubles their outputs, h~=(0.8/0.5,0,0.5/0.5,0)=(1.6,0,1,0)\tilde{h} = (0.8/0.5, 0, 0.5/0.5, 0) = (1.6, 0, 1, 0), so

z~=0.5×1.6+0.3×1=0.8+0.3=1.1\tilde{z} = 0.5 \times 1.6 + 0.3 \times 1 = 0.8 + 0.3 = 1.1

Step 2. The expected value. The complementary mask (0,1,0,1)(0, 1, 0, 1) gives h~=(0,0.6,0,1.8)\tilde{h} = (0, 0.6, 0, 1.8) and

z~=(−0.4)×0.6+0.2×1.8=−0.24+0.36=0.12\tilde{z} = (-0.4) \times 0.6 + 0.2 \times 1.8 = -0.24 + 0.36 = 0.12

The two masks are equally likely, and their mean (1.1+0.12)/2=0.61(1.1 + 0.12)/2 = 0.61 is the test value. The mean over all 16 equally likely masks is also exactly 0.61, since every unit is kept in half of them. Single masks give very different sums, and this noise keeps the network from relying on any single unit.

Step 3. Early stopping. A network is trained with a patience of three epochs:

Epoch Training loss Validation loss Gap Best so far Epochs without improvement
1 0.50 0.52 0.52−0.50=0.020.52 - 0.50 = 0.02 0.52 (epoch 1) 0
2 0.38 0.41 0.41−0.38=0.030.41 - 0.38 = 0.03 0.41 (epoch 2) 0
3 0.31 0.36 0.36−0.31=0.050.36 - 0.31 = 0.05 0.36 (epoch 3) 0
4 0.27 0.34 0.34−0.27=0.070.34 - 0.27 = 0.07 0.34 (epoch 4) 0
5 0.24 0.35 0.35−0.24=0.110.35 - 0.24 = 0.11 0.34 (epoch 4) 1
6 0.21 0.37 0.37−0.21=0.160.37 - 0.21 = 0.16 0.34 (epoch 4) 2
7 0.19 0.36 0.36−0.19=0.170.36 - 0.19 = 0.17 0.34 (epoch 4) 3

After epoch 7, the validation loss has not improved for three epochs, so training stops and the weights of epoch 4, with the validation loss 0.34, are restored. Epoch 7 is better than epoch 6 but not better than the best value, so the counter keeps rising.

Step 4. Reading the curves. The training loss falls in every epoch, while the gap grows from 0.07 to 0.17 after epoch 4: The network has started to fit the noise of its training data. Two high losses close to each other would instead signal underfitting, which calls for a larger network or longer training rather than more regularisation. Part I of the lab applies the rule to any losses and trains a small network until it overfits.

Python step 10: Matrices, shapes and tensors

The Python step of this week is part of the Colab notebook, where every explanation stands next to a cell that runs it and the step closes with a quick check and exercises with immediate feedback. The printable lecture notes contain the same step together with the outputs of its code. The Colab button below opens the notebook at the step. Run the setup cell just above the step first, since the exercises use its check function.

Open Python step 10 in Colab View the notebook on GitHub

Review cards

Select a card to turn it over.

Perceptron
A threshold unit with learned weights; converges on linearly separable data [2].
Backpropagation
Chain-rule computation of all weight gradients in one backward pass [7].
ReLU
max(0, z); a simple activation that eases training of deep networks [4].
Adam
Adaptive moment estimation; per-weight step sizes from running gradient moments [10].
Early stopping
Keep the weights from the epoch with the lowest validation loss.
nn.Module
PyTorch base class for networks: layers in __init__, computation in forward.

Continue the week

The week continues with the simulation and the self-assessment of the interactive lab and with the Python step and the hands-on work of the Colab notebook. The week overview lists the discipline challenges, the weekly task and the research assignment.

Interactive lab Colab notebook Self-assessment Week overview and tasks

References

[1] McCulloch, W. S., & Pitts, W. (1943). A logical calculus of the ideas immanent in nervous activity. The Bulletin of Mathematical Biophysics, 5(4), 115-133. https://doi.org/10.1007/BF02478259

[2] Rosenblatt, F. (1958). The perceptron: A probabilistic model for information storage and organization in the brain. Psychological Review, 65(6), 386-408. https://doi.org/10.1037/h0042519

[3] Minsky, M., & Papert, S. (1969). Perceptrons: An Introduction to Computational Geometry. MIT Press.

[4] Nair, V., & Hinton, G. E. (2010). Rectified linear units improve restricted Boltzmann machines. In Proceedings of the 27th International Conference on Machine Learning (pp. 807-814).

[5] Cybenko, G. (1989). Approximation by superpositions of a sigmoidal function. Mathematics of Control, Signals and Systems, 2(4), 303-314. https://doi.org/10.1007/BF02551274

[6] Hornik, K., Stinchcombe, M., & White, H. (1989). Multilayer feedforward networks are universal approximators. Neural Networks, 2(5), 359-366. https://doi.org/10.1016/0893-6080(89)90020-8

[7] Rumelhart, D. E., Hinton, G. E., & Williams, R. J. (1986). Learning representations by back-propagating errors. Nature, 323(6088), 533-536. https://doi.org/10.1038/323533a0

[8] Paszke, A., Gross, S., Massa, F., et al. (2019). PyTorch: An imperative style, high-performance deep learning library. In Advances in Neural Information Processing Systems 32 (pp. 8024-8035). https://arxiv.org/abs/1912.01703

[9] Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press.

[10] Kingma, D. P., & Ba, J. (2015). Adam: A method for stochastic optimization. In 3rd International Conference on Learning Representations (ICLR 2015). https://arxiv.org/abs/1412.6980

[11] Glorot, X., & Bengio, Y. (2010). Understanding the difficulty of training deep feedforward neural networks. In Proceedings of the Thirteenth International Conference on Artificial Intelligence and Statistics, PMLR 9 (pp. 249-256).

[12] Ioffe, S., & Szegedy, C. (2015). Batch normalization: Accelerating deep network training by reducing internal covariate shift. In Proceedings of the 32nd International Conference on Machine Learning, PMLR 37 (pp. 448-456). https://arxiv.org/abs/1502.03167

[13] Srivastava, N., Hinton, G., Krizhevsky, A., Sutskever, I., & Salakhutdinov, R. (2014). Dropout: A simple way to prevent neural networks from overfitting. Journal of Machine Learning Research, 15(56), 1929-1958.

[14] Yeh, I.-C. (1998). Modeling of strength of high-performance concrete using artificial neural networks. Cement and Concrete Research, 28(12), 1797-1808. https://doi.org/10.1016/S0008-8846(98)00165-3

[15] Kose, U. (2018). An ant-lion optimizer-trained artificial neural network system for chaotic electroencephalogram (EEG) prediction. Applied Sciences, 8(9), 1613. https://doi.org/10.3390/app8091613

[16] LeCun, Y., Bengio, Y., & Hinton, G. (2015). Deep learning. Nature, 521(7553), 436-444. https://doi.org/10.1038/nature14539