๐ข The situation
The team's Friday tech talk. Lena:
"You've already seen that boosting beats neural nets on tables. But ahead of us are images (module 17) and deeper text work โ there neural networks have no alternative. Today we dissect how they work, without magic: the neuron, layers, backprop. Homework โ train a net in PyTorch and overfit it on purpose to see what that looks like. Whoever hasn't overfitted a network doesn't know how to train one."
๐ฏ Your task
- Understand the network's anatomy: neuron, activations, layers, backpropagation.
- Train a network in PyTorch: write the training loop by hand.
- See overfitting on the learning curves and defeat it.
๐ Theory
The neuron and layers
A single neuron = that same linear model: output = activation(wยทx + b). A network is layers of neurons: one layer's outputs become the next layer's inputs. A composition of linear layers with non-linear activations between them can approximate arbitrarily complex functions.
Activations: without them a stack of linear layers collapses into one linear model. The hidden-layer standard is ReLU max(0, x): simple, fast, doesn't "vanish" like sigmoid. At the output: sigmoid (binary classification), softmax (multiclass), nothing (regression).
How the network learns: backpropagation
The same gradient descent from module 4, but the gradients over millions of weights are computed by the chain rule from output to input โ that is backprop. One training step:
- Forward: run the batch, get predictions and the loss.
- Backward:
loss.backward()โ PyTorch computes all gradients itself (autograd). - Optimizer step:
optimizer.step()โ update the weights;optimizer.zero_grad()โ reset the gradients (they accumulate otherwise โ rookie bug #1).
The training vocabulary
- Batch โ a chunk of data per step; epoch โ a full pass over the data.
- Optimizers: SGD with momentum; Adam/AdamW โ adaptive step, the default to start with.
- The learning rate is the most important hyperparameter. Too big โ the loss jumps/explodes; too small โ forever.
- Regularization: Dropout (randomly switch off neurons during training), weight decay, early stopping, data augmentation.
Learning curves โ the training's ECG
- train loss โ, val loss โ โ learning;
- train loss โ, val loss โ โ overfitting has begun, right at this epoch;
- both flat โ underfitting or a too-small LR.