Deep Learning
How neural networks learn, in plain language – enough to read a training log, know why a model is stuck, and understand what sits under every LLM.
Neural networks and perceptrons
A neural network is layers of very simple units. Each unit multiplies its inputs by weights, adds them up, and passes the result through a function. That is the whole idea – the power comes from stacking it.
- A perceptron is one unit: inputs, weights, a sum, a threshold.
- A layer is many units side by side; a deep network is many layers.
- The weights are what the model learns. Training means nudging millions of them until the output stops being wrong.
- Early layers learn crude patterns (edges, word fragments), later layers combine them into meaningful ones. Nobody programs what each layer looks for.
A hospital scan classifier has no rule for “tumour”. It saw thousands of labelled scans and adjusted weights until it mostly agreed with the radiologists. Which means your test strategy is agreement rates and failure analysis, not expected values.
Activation and loss functions
Two choices that decide whether a network can learn at all.
- Activation functions add the non-linearity. Without them, a hundred layers collapse into one straight line.
- ReLU (keep positives, zero the rest) is the default for hidden layers – simple and fast.
- Sigmoid squashes to 0-1 for a yes/no output; softmax turns scores into probabilities across several classes that add up to 1.
- The loss function measures how wrong the output is: cross-entropy for classification, mean squared error for numbers.
- Training is nothing more than making the loss smaller.
When an LLM gives you “confidence 0.92”, that number came out of a softmax. It describes how the model distributed its own scores – not how likely the statement is to be true in the real world. Never quote it to a stakeholder as accuracy.
Forward propagation and backpropagation
The two-step loop that repeats millions of times.
- Forward pass: data goes in, flows through the layers, a prediction comes out, the loss says how wrong it was.
- Backward pass: the error is traced back through the network to work out how much each weight contributed to it.
- Each weight is then adjusted a little in the direction that reduces the error.
- One pass over the whole dataset is an epoch. Training runs for many epochs.
Epoch 1/20 loss 0.692 val_loss 0.688 <- barely learning yet Epoch 5/20 loss 0.412 val_loss 0.430 Epoch 12/20 loss 0.180 val_loss 0.298 <- gap opening: overfitting starts Epoch 20/20 loss 0.061 val_loss 0.377 <- training better, validation worse Read the two columns together, never loss alone.
That log is the single most useful thing to be able to read in deep learning. Training loss falling while validation loss rises means the model is memorising. The fix is to stop around epoch 10 – which is literally a setting called early stopping.
Gradient descent and optimizers
How big each nudge should be. This is where most training failures actually come from.
- Gradient descent walks downhill on the loss surface. The learning rate is the step size.
- Too large and the loss bounces around or becomes NaN; too small and training crawls or stalls.
- Adam is the sensible default optimizer – it adapts the step size per weight.
- Batch size is how many examples are used per step. Larger is faster per epoch but needs more memory.
- A loss that turns into NaN almost always means the learning rate is too high or the inputs were not scaled.
“We trained it for three days and it learned nothing” is usually a learning rate problem, not a data problem. Asking for the loss curve before accepting that explanation is a reasonable quality question.
TensorFlow/Keras and PyTorch fundamentals
Two frameworks, the same ideas. Keras is the quickest to read; PyTorch is what most research and most LLM code uses.
- The structure is always: define layers, compile with an optimizer and loss, fit, evaluate.
- Dropout and early stopping are the two regularisers you will see most often.
- Set the random seed if you need reproducible runs – otherwise two runs give different numbers.
- For table data, try a gradient-boosted tree first. Neural networks earn their keep on images, audio and text.
# Keras - a small binary classifier
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Input(shape=(8,)),
layers.Dense(32, activation="relu"),
layers.Dropout(0.2), # randomly switch off units: fights overfitting
layers.Dense(16, activation="relu"),
layers.Dense(1, activation="sigmoid"),
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
stop = keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True)
model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=50, batch_size=32, callbacks=[stop])
print(model.evaluate(X_test, y_test))A tester does not have to write this. But being able to open train.py and say “there is no validation split here” or “early stopping is missing” is worth more in a design review than any number of manual test cases.
CNNs, sequence models and transformers
Three architectures for three kinds of data – and the one that changed everything.
- CNNs (convolutional networks) handle images by scanning small patches: defect photos, scans, damaged-parcel detection.
- RNNs and LSTMs read sequences one step at a time – the old approach for text and time series. Slow, and they forget long context.
- Transformers (2017) read the whole sequence at once and use attention to decide which words matter to which. That is why they scale.
- Every modern language model – GPT, Claude, Gemini, Llama – is a transformer.
- Attention is also why context windows have a limit and why long prompts cost more.
“Transfer the amount to the account I used last time.” An LSTM may have lost “amount” by the time it reaches “account”. Attention lets the model link the two directly – the same mechanism that lets it keep track of a 50-message conversation, up to the context limit.
Project: build a basic classifier and read its training curve
- Use the dataset from Module 4 and build the small Keras network above.
- Train without early stopping for 50 epochs and plot training vs validation loss. Mark the epoch where they separate.
- Add dropout and early stopping, retrain, and compare the test scores from both runs.
- Compare both against the random forest from Module 4. Note which was better and how much longer the network took.
- Write the conclusion in one honest line – on table data it is often “the simpler model won”.