Key terms for this tutorial
Here are the words that matter most in this tutorial. Come back here if a term feels fuzzy.
- activation
- A non-linear function (like ReLU or Sigmoid) that lets a neural network learn curves and boundaries instead of just straight lines.
- backpropagation
- The algorithm that computes gradients through a neural network by applying the chain rule from the loss back to the inputs.
- chain rule
- The calculus rule that lets backpropagation compute how earlier weights affect the final loss.
- loss
- A number that measures how wrong the model is. Training tries to minimize loss.
- model
- A function with learnable parameters that maps inputs to outputs.
- neural network
- A stack of layers that transform data through learned weights and non-linearities.
- ReLU
- A common activation function that outputs the input directly if it is positive, and zero otherwise.
- weight
- A learnable number inside a model that is adjusted during training.
A single neuron
A neuron does three things: take a weighted sum of inputs, add a bias, and pass the result through an activation function. The activation makes the model non-linear, which is what lets it learn curves and complex patterns.
Without an activation, no matter how many layers you stack, the whole network is still just a linear map. That cannot learn XOR or any non-trivial decision boundary.
Core idea: non-linear activations are the secret sauce. They bend an otherwise straight model into a flexible shape-fitter.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Neural Networks from Scratch'. Explain the section 'A single neuron' in plain English. Include simple analogies, vocabulary definitions, and a short example.
Forward pass for a tiny network
Here is a 2-2-1 multilayer perceptron (MLP) that learns XOR, the classic non-linear problem.
import numpy as np
def relu(x):
return np.maximum(0, x)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Random initialization
W1 = np.random.randn(2, 2)
b1 = np.zeros((1, 2))
W2 = np.random.randn(2, 1)
b2 = np.zeros((1, 1))
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
# Forward pass
Z1 = X @ W1 + b1
A1 = relu(Z1)
Z2 = A1 @ W2 + b2
pred = sigmoid(Z2)
print("Initial predictions:\n", pred.round(3))
Initial predictions: [[0.512] [0.489] [0.503] [0.470]]
Line-by-line: xor_forward.py
Read this slowly. Each line does one small, concrete thing.
import numpy as np— Import thenumpylibrary so we can use its built-in tools.def relu(x):— Define therelufunction, which describes a step in the data or model pipeline.def sigmoid(x):— Define thesigmoidfunction, which describes a step in the data or model pipeline.pred = sigmoid(Z2)— Generate predictions by running inputs through the model.print("Initial predictions:\n", pred.round(3))— Print a result so we can see what the model learned.
Pop-in: Read the code one block at a time
Industry code imports many libraries. Focus on the sequence: prepare data, define the model, run the training loop, and evaluate. The rest is setup.
At first the predictions are random. The magic is in the backward pass: using the chain rule to figure out how every weight contributed to the error.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Neural Networks from Scratch'. Explain the section 'Forward pass for a tiny network' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Backpropagation by the chain rule
Backpropagation is the chain rule from calculus applied to a computational graph. If loss = f(g(h(weight))), the derivative with respect to the weight is the product of the derivatives along the path.
Frameworks like PyTorch build this graph automatically. But the first time you see it, it helps to trace the errors backwards: output error → weight 2 → hidden activation → weight 1.
import numpy as np
def relu(x): return np.maximum(0, x)
def sigmoid(x): return 1 / (1 + np.exp(-x))
W1 = np.random.randn(2, 2)
b1 = np.zeros((1, 2))
W2 = np.random.randn(2, 1)
b2 = np.zeros((1, 1))
X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([[0],[1],[1],[0]])
for _ in range(5000):
# forward
z1 = X @ W1 + b1
a1 = relu(z1)
z2 = a1 @ W2 + b2
pred = sigmoid(z2)
# backward
dL_dpred = 2 * (pred - y) / len(X)
dz2 = dL_dpred * pred * (1 - pred)
dW2 = a1.T @ dz2
db2 = np.sum(dz2, axis=0, keepdims=True)
da1 = dz2 @ W2.T
dz1 = da1 * (z1 > 0)
dW1 = X.T @ dz1
db1 = np.sum(dz1, axis=0, keepdims=True)
# update
lr = 0.1
W1 -= lr * dW1
b1 -= lr * db1
W2 -= lr * dW2
b2 -= lr * db2
print("Final predictions:\n", pred.round(3))
Final predictions: [[0.002] [0.998] [0.998] [0.002]]
Line-by-line: xor_train.py
Read this slowly. Each line does one small, concrete thing.
import numpy as np— Import thenumpylibrary so we can use its built-in tools.def relu(x): return np.maximum(0, x)— Define therelufunction, which describes a step in the data or model pipeline.def sigmoid(x): return 1 / (1 + np.exp(-x))— Define thesigmoidfunction, which describes a step in the data or model pipeline.for _ in range(5000):— Loop over the data one batch at a time.pred = sigmoid(z2)— Generate predictions by running inputs through the model.dL_dpred = 2 * (pred - y) / len(X)— Generate predictions by running inputs through the model.print("Final predictions:\n", pred.round(3))— Print a result so we can see what the model learned.
Pop-in: Read the code one block at a time
Industry code imports many libraries. Focus on the sequence: prepare data, define the model, run the training loop, and evaluate. The rest is setup.
After training, the network outputs almost 0 for the first and last rows and almost 1 for the middle two. It learned XOR from only four examples.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Neural Networks from Scratch'. Explain the section 'Backpropagation by the chain rule' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Check yourself
Why must we use an activation function?
What would happen if you removed relu and sigmoid from the network above?
Without activations, a multilayer network collapses to a single linear transformation. It would be unable to learn XOR, no matter how many layers or neurons you add.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Neural Networks from Scratch'. The practice question 'Why must we use an activation function?' asks: What would happen if you removed relu and sigmoid from the network above? The answer is: Without activations, a multilayer network collapses to a single linear transformation. It would be unable to learn XOR, no matter how many layers or neurons you add.. Explain the concept behind this in simple terms.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Neural Networks from Scratch'. Explain the section 'Check yourself' in plain English. Include simple analogies, vocabulary definitions, and a short example.
Three questions
1. What does backpropagation compute?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Neural Networks from Scratch'. The quiz asks: What does backpropagation compute? The correct answer is: The gradient of the loss with respect to every weight. Explain why that answer is right and why the other choices are wrong, using a simple example.
2. Why is ReLU a popular activation function?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Neural Networks from Scratch'. The quiz asks: Why is ReLU a popular activation function? The correct answer is: It is cheap to compute and helps gradients flow in positive regions. Explain why that answer is right and why the other choices are wrong, using a simple example.
3. A 2-2-1 network has how many weight matrices?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Neural Networks from Scratch'. The quiz asks: A 2-2-1 network has how many weight matrices? The correct answer is: 2. Explain why that answer is right and why the other choices are wrong, using a simple example.
Keep learning
Dig deeper into activations, initialization, batch norm, dropout, and vanishing gradients.
Part of the CodeHS tutorial library.