Neural networks
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A neural network wires simple neurons together in layers, and every link carries a weight. Data flows forward through the layers to an answer; the loss says how wrong it is; backpropagation sends the error backwards to give every weight its gradient; and gradient descent nudges each weight a little. Follow a tiny 2-2-1 network through one full round of that loop, with every number worked out, then watch it learn XOR, a task no single neuron can learn.
Neurons in layers
A neural network wires simple neurons together in layers. Ours has an input layer of 2, a hidden layer of 2 and an output layer of 1. Each link carries a weight: the signal travelling along it is multiplied by that number.
input layer x1, x2 the numbers we feed in
hidden layer h1, h2 neither input nor answer
output layer y the network's answer
each neuron: sum of (input x weight) + bias,
then its activation function
links: 6 weights, plus 3 biases (h1, h2, y)اقرأ الدرس خطوة بخطوة
Neurons in layers
A neural network wires simple neurons together in layers. Ours has an input layer of 2, a hidden layer of 2 and an output layer of 1. Each link carries a weight: the signal travelling along it is multiplied by that number.
input layer x1, x2 the numbers we feed in hidden layer h1, h2 neither input nor answer output layer y the network's answer each neuron: sum of (input x weight) + bias, then its activation function links: 6 weights, plus 3 biases (h1, h2, y)The task: XOR
The task is XOR: answer 1 when exactly one input is 1, otherwise 0. No single neuron can learn it, because no one straight line splits the 1s from the 0s. A hidden layer can: its neurons learn useful in-between features.
x1 x2 XOR 0 0 0 0 1 1 1 0 1 1 1 0 on a grid, the two 1s sit on opposite corners: one straight line can never put them on one sideForward: the hidden layer
Forward pass, with input (1, 1). Each hidden neuron multiplies every input by its weight, adds them up, then adds its bias. h1: 1×1 + 1×1 + 0 = 2. h2: 1×1 + 1×1 − 1 = 1.
y, z, h = forward(p, x) x = (1, 1) z1 = x1*1 + x2*1 + 0 = 1 + 1 + 0 = 2 z2 = x1*1 + x2*1 + (-1) = 1 + 1 - 1 = 1 weights on the links, bias inside each neuronThe activation: ReLU
Next each neuron applies its activation function. Ours is ReLU: keep a positive sum, turn a negative one into 0. Both sums are positive, so h1 = 2 and h2 = 1. For input (0, 0), h2's sum, −1, would become 0.
relu(z) = max(0, z) h1 = relu(2) = 2 h2 = relu(1) = 1 input (0, 0): z2 = -1 -> h2 = relu(-1) = 0 without this bend, any stack of layers would add up to one straight-line model againForward: the output
The output neuron does the same with the hidden values: 2×1 + 1×(−1) + 0 = 1. So the network answers 1 for (1, 1), but XOR says 0: it is wrong. Its weights are not trained yet.
y = h1*1 + h2*(-1) + 0 = 2 - 1 + 0 = 1 target t = 0 (1 XOR 1 = 0) untrained outputs for all four inputs: 00->0 01->1 10->1 11->1 only (1, 1) is wrongLoss: how wrong?
The loss measures how wrong the answer is. We use half the squared error, (y − t)² / 2, where t = 0 is the target. (1 − 0)² / 2 = 0.5. Learning means changing the weights so the loss gets smaller.
loss = (y - t) ** 2 / 2 = (1 - 0) ** 2 / 2 = 0.5 the 1/2 makes the slope simple: d loss / d y = y - t = 1 (the error)Backward: share the blame
Backpropagation sends the error, y − t = 1, backwards. Each weight's gradient is the error reaching its neuron times the signal it carried: 1 × 2 = 2 for h1's output weight. The chain rule passes the error back through every weight.
g = backward(p, x, t) error e = y - t = 1 output weights: e*h1 = 2 e*h2 = 1 bias: 1 error at h1 = e * 1 * relu'(2) = 1 * 1 * 1 = 1 error at h2 = e * -1 * relu'(1) = 1 * -1 * 1 = −1 hidden weights: into h1 1, 1 into h2 −1, −1 (each = error at the neuron x the input, here 1)Update: a small step
Gradient descent then moves every weight against its gradient, scaled by the learning rate 0.05: 1 − 0.05 × 2 = 0.9. The hidden weights become 0.95 and 1.05. Run (1, 1) again: the output is now 0.41, closer to 0.
p = update(p, g, lr) lr = 0.05 h1 -> y: 1 - 0.05 * 2 = 0.9 h2 -> y: -1 - 0.05 * 1 = −1.05 into h1: 1 - 0.05 * 1 = 0.95 into h2: 1 - 0.05 * (-1) = 1.05 biases: h1 −0.05, h2 −0.95, y −0.05 again: y = 0.4075, loss 0.083 (was 0.5)Repeat: the network learns
Training repeats this for every example, again and again. After 200 passes (epochs) over the four examples, the loss for a pass falls from 0.5 to 0.000047, and the outputs are 0, 1, 1, 0: the network has learned XOR.
epoch loss over the 4 examples 1 0.5 10 0.11 50 0.014 200 0.000047 outputs now: 00->0.01 01->1 10->1 11->0 weights now: 1 1 1.24 1.24 0.99 −1.6Deep learning: scale it up
Deep learning runs this same loop, forward, loss, backward, update, on networks with many hidden layers and millions or billions of weights: GPT-3 has 175 billion. Libraries such as PyTorch work out every gradient automatically.
the same four lines, at any size: 1 y, z, h = forward(p, x) 2 loss = (y - t) ** 2 / 2 3 g = backward(p, x, t) 4 p = update(p, g, lr) our network: 9 numbers to learn (6 weights, 3 biases) GPT-3 (2020): 175 billion
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A hidden neuron has weights 2 (from x1) and −1 (from x2) and bias 0.5, and uses ReLU. What does it output for the input x1 = 1, x2 = 3?
The weighted sum is 2×1 + (−1)×3 + 0.5 = −0.5. ReLU turns a negative sum into 0, so the neuron outputs 0.
02Why does the network need a hidden layer to compute XOR?
The 1s of XOR, (0, 1) and (1, 0), sit on opposite corners of the square, so no single straight line splits them from the 0s. The hidden neurons build new features that make the answer easy for the output neuron.
03What would happen if the hidden neurons had no activation function (just the weighted sum plus bias)?
A linear function of a linear function is still linear. The bend in ReLU is what lets a stack of layers model shapes a single straight line cannot.
04During backpropagation, the error reaching the output neuron is y − t = 1, and the hidden neuron h1 sent it the value 2. What is the gradient of the weight on the h1 → output link?
A weight's gradient is the error at the neuron it feeds times the signal it carried: 1 × 2 = 2. With a learning rate of 0.05 the weight then moves by −0.05 × 2 = −0.1.
05Which list gives one round of the training loop in the right order?
First run the input forward to get an answer, measure the loss, send the error backward to get every gradient, then update the weights. Training repeats this round many times.
المسرد
- Neural network
- A model made of simple units (neurons) arranged in layers, where each link between neurons carries a weight that training adjusts.
- Layer
- A group of neurons that work side by side. The input layer holds the data, hidden layers compute in-between features, and the output layer gives the answer.
- Weight and bias
- The numbers a network learns. A weight multiplies the signal on one link; a bias is added to a neuron's sum.
- Activation function
- The function a neuron applies to its sum. ReLU, max(0, z), keeps positive sums and turns negative ones into 0.
- Forward pass
- Running an input through the layers, one after another, to get the network's output.
- Loss
- A number that measures how wrong the output is. Here, half the squared error: (y − t)² / 2.
- Backpropagation
- Using the chain rule to send the error backwards through the network, giving every weight its gradient.
- Epoch
- One full pass over all the training examples. Training usually runs for many epochs.
الملاحظات والمصادر
- The starting weights are the XOR solution from the Deep Learning book (section 6.1) with one output weight changed from −2 to −1, so the numbers stay simple. Real networks start from small random weights.
- This network updates after every example (stochastic gradient descent). Large networks usually average the gradient over a small batch of examples first.
- The Python program in the lesson is real: run it and it prints the loss at epochs 1, 10, 50, 200 and then the outputs 0, 1, 1, 0.
- Goodfellow, Bengio & Courville, Deep Learning (MIT Press, 2016), ch. 6 Deep Feedforward Networks: section 6.1 learning XOR, ReLU, section 6.5 back-propagation
- Goodfellow, Bengio & Courville, Deep Learning, ch. 8: stochastic gradient descent and minibatches
- Stanford CS231n notes, Neural Networks Part 1: Setting up the Architecture (layers, neurons, ReLU)
- Stanford CS231n notes, Backpropagation, Intuitions (the chain rule on a computational graph)
- Rumelhart, Hinton & Williams, Learning representations by back-propagating errors, Nature 323 (1986)
- Glorot, Bordes & Bengio, Deep Sparse Rectifier Neural Networks (AISTATS 2011)
- Stanford CS229 Machine Learning, lecture notes (A. Ng, T. Ma): squared-error loss and gradient descent
- Brown et al., Language Models are Few-Shot Learners (2020): GPT-3, 175 billion parameters
- PyTorch tutorials, Automatic Differentiation with torch.autograd
اختبار يتاح بعد الخطوة الأخيرة.