← govind p lal
AI and ML Bootcamp · Lecture 3 · lecture notes

Neural Networks from Scratch: Neurons, Loss, Backpropagation, Training Loop

In short — How does a machine actually learn a pattern? This lecture builds a neural network step by step. It covers the neuron, activation functions, loss, backpropagation, Hyperparameter controlling how big each weight update step is.">learning rate and the training loop. It ends with a working XOR network in NumPy and PyTorch.

Network Anatomy

builds on Lecture 1: Week 0 - AI and ML | Orientation | 09/01/2026

Circles in a network diagram -> Neurons (also called parameters). Smallest unit of the architecture.

Input2 unitsHidden4 unitsOutput
Simple network with one hidden layer.

Learning as Guess, Error, Adjust

adds to earlier · House-price example and the exact four steps with forward/backward arrows of training · builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026, Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026
Example

Data: 1000 sq ft -> $200K, 1500 -> $300K, 2000 -> $400K. Price of 2500 sq ft? Humans spot the pattern ($200 per sq ft) and say $500K.

Human version: you answer 2+2=5, teacher says wrong, answer is 4. You adjust. Same loop.

What a Neuron Does

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026

A neuron is a plain mathematical function, like sum or subtraction. Not a biological neuron.

Example

Input 1000 with weight 0.1 gives 100. Do the same for every input, then sum.

Inputs x1..xnMultiply by weightsSum + biasActivation functionOutput
Inside one neuron.

The Linearity Problem and Activation Functions

The Problem

Two layers collapse into one. A million layers also collapse into one. Depth gives nothing. (Linearity Problem)

Visual intuition: red and blue points that no single straight line can separate. A line with bends can.

The Solution

!

Interview favourite: why do we need activation functions? Without them any depth collapses into one linear layer.

Common Activation Functions

FunctionBehaviour
SigmoidSquashes to range 0 to 1; too restrictive
TanhRange -1 to 1; more room but still had problems
ReLUNegative input -> 0; positive input -> same number (-2 gives 0, 2 gives 2)
GELU / ELUReLU-like with smoother tweaks (small negative values instead of hard zero)

Whichever function performs best across huge numbers of experiments becomes the industry standard. ReLU was standard for a long time.

Loss Function

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026

Loss function - single number measuring how wrong the model is. Lower is better. Training goal -> minimise loss.

Example

Actual $400K, predicted $350K, error 50K, squared error is large. Predict 410K, error 10K, much smaller. Predict 400K, loss 0 -> learned.

Backpropagation, Forward and Backward Pass

After computing loss, the network needs to know which weights to change. Backpropagation -> assigns blame to weights.

Example

Profit drops. CEO asks deputy CEO, who asks managers, who ask employees. The report flows back and finds employee 3 responsible. Likewise the loss flows back and finds neuron 3's weight responsible.

!

The lecture treats it as intuition only. The calculus is partial differentiation, to be covered later if needed.

Loss Landscape, Learning Rate, Gradient Descent

Loss landscape -> visualising loss for all weight settings as hills and valleys. Training starts at a random point and must reach the lowest valley.

Analogy: blindfolded on a hill. Gravity under your feet shows the downhill direction (backprop). Your step size is the learning rate.

Hyperparameter - setting a human chooses (learning rate, iterations). Weights are changed by backprop; hyperparameters are not.

!

Gradient descent - the full algorithm of stepping downhill using the backprop direction and the learning rate.

The Full Training Loop

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026
repeatForward passget predictionCompute lossBackward passfind blameUpdate weightsgradient descent
The training loop.

Scale this to a billion-parameter network and a big chunk of internet text, repeat millions of times, and you get an LLM.

Coding the XOR Network in NumPy

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026

XOR problem - exclusive OR. Different inputs give 1, same inputs give 0. Not separable by one straight line, so it needs an activation function.

x1x2target
000
011
101
110

Architecture: 2 inputs -> 4 hidden -> 1 output (output is 0 or 1).

Weights start random, so the first predictions are garbage and initial loss is about 0.2557.

import numpy as np
X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([[0],[1],[1],[0]])
sigmoid = lambda z: 1/(1+np.exp(-z))
# forward pass
# z_h = X @ W1 + b1 ; a_h = sigmoid(z_h)
# z_o = a_h @ W2 + b2 ; out = sigmoid(z_o)
# loss = np.mean((out - y)**2)

PyTorch Version

import torch, torch.nn as nn
X = torch.tensor([[0.,0],[0,1],[1,0],[1,1]])
y = torch.tensor([[0.],[1],[1],[0]])
model = nn.Sequential(nn.Linear(2,4), nn.Sigmoid(), nn.Linear(4,1), nn.Sigmoid())
loss_fn = nn.MSELoss()
opt = torch.optim.SGD(model.parameters(), lr=2)
for i in range(10000):
    pred = model(X)
    loss = loss_fn(pred, y)
    opt.zero_grad()
    loss.backward()
    opt.step()

Glossary

Weight
Learnable number saying how important an input is.
Bias
Extra learnable number added to a neuron's weighted sum.
Activation function
Non-linear function applied to a neuron's output so layers do not collapse.
ReLU
Rectified linear unit: outputs 0 for negatives and the same number for positives.
Loss function
Single number measuring how wrong a prediction is.
Backpropagation
Algorithm that sends the loss backward to find which weights caused the error.
Learning rate
Hyperparameter controlling how big each weight update step is.
Gradient descent
Stepping downhill on the loss using backprop direction and learning rate.
Hyperparameter
Setting chosen by a human, such as learning rate or iterations.