Prerequisites: shapes and code

You should be comfortable with vectors, matrices, inner products, eigenvectors of a symmetric matrix, and the chain rule for vector-valued functions. If any are rusty, DD1420 Module 1 has a checklist. Mathematics for Machine Learning by Deisenroth, Faisal and Ong covers linear algebra in chapter 2, inner products in chapter 3, eigenvectors in chapter 4, and vector calculus in chapter 5.

Deep learning expresses these ideas using tensors: arrays with any number of axes, including vectors and matrices. A batch of $B$ RGB images has shape $B \times 3 \times H \times W$, CT volumes $B \times 1 \times D \times H \times W$, and transformer tokens $B \times n \times d$. Here $H,W,D$ are spatial dimensions, $n$ is the number of tokens and $d$ their feature dimension. Networks combine affine maps, nonlinearities, normalization, pooling and attention. A fully connected layer uses a matrix product and bias; convolution uses shared local weights; attention mixes tokens with input-dependent weights (section 4). The loss gradient with respect to a parameter tensor has the same shape as that tensor. The chain rule propagates gradients through the network using Jacobians (DD1420 lessons 4.1 to 4.4).

Tensor shapes in the training loop below. Arrows show data flow, not necessarily a change of shape: ReLU keeps it. The labels y enter only the loss, and each gradient has the shape of its parameter.

Tensor shapes in the training loop below. Arrows show data flow, not necessarily a change of shape: ReLU keeps it. The labels y enter only the loss, and each gradient has the shape of its parameter.

This course requires familiarity with PyTorch and NumPy. We will also use MONAI for 3D medical imaging, nibabel for NIfTI, SimpleITK for medical-image processing and DICOM, and timm or Hugging Face for pretrained backbones. You need not know these additional libraries in advance, but you should be able to read a training loop. Figure 0 traces the shapes below. CrossEntropyLoss takes raw logits and, here, integer torch.long labels: do not apply softmax first. For validation, model.eval() changes dropout and batch-normalization behavior; torch.no_grad() disables gradient recording.

import torch, torch.nn as nn
model = nn.Sequential(nn.Conv2d(1, 16, 3, padding=1), nn.ReLU(),
                      nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(16, 2))
opt = torch.optim.AdamW(model.parameters(), lr=1e-3)
loss_fn = nn.CrossEntropyLoss()
model.train()
for x, y in train_loader:  # x: B x 1 x H x W; y: B, torch.long
    logits = model(x)     # B x 2; raw logits
    loss = loss_fn(logits, y)
    opt.zero_grad()
    loss.backward()
    opt.step()

Supervised learning

Figure 1. (a) The supervised learning loop. (b) Training and validation loss over epochs (schematic). Early stopping with patience stops a little later and restores the best checkpoint. (c) A new scanner changes p(x), one kind of domain shift.

Figure 1. (a) The supervised learning loop. (b) Training and validation loss over epochs (schematic). Early stopping with patience stops a little later and restores the best checkpoint. (c) A new scanner changes p(x), one kind of domain shift.

Supervised learning starts from four ingredients, arranged in Figure 1(a): a dataset $\mathcal{D}=\{(x_i,y_i)\}{i=1}^{N}$ of inputs (images, volumes, slides) and targets (a diagnosis or mask); a model $f\theta$ with parameters $\theta$ that predicts $\hat y=f_\theta(x)$; a loss $\ell(\hat y,y)$ that measures prediction error; and an optimizer that adjusts $\theta$ to minimize the average loss,

$$ \theta^* = \arg\min_\theta \; L(\theta), \qquad L(\theta) = \frac{1}{N} \sum_{i=1}^{N} \ell\big(f_\theta(x_i), y_i\big) $$

DD1420 called $L$ the empirical risk. Common per-example losses are squared error, $\ell(\hat y,y)=(\hat y-y)^2$ for scalar regression, and cross-entropy, $\ell(p,y)=-\log p_y=-\sum_{c=1}^{C}\mathbf{1}[y=c]\log p_c$, where $p_c$ is the predicted class probability and $y$ the true class. Segmentation applies a classification loss at each pixel or voxel and usually averages it spatially. Section 5 explains the likelihood interpretation.

Training uses stochastic gradient-based optimization, such as SGD or AdamW. SGD estimates $\nabla_\theta L$ on a random mini-batch and steps against it with learning rate $\eta$; momentum and Adam-style rescaling modify this update. The learning rate and its schedule strongly affect training: too high can make it unstable, too low can make it slow. The loss surface is nonconvex, so finding a global minimum is not guaranteed. What matters is whether the resulting model generalizes.

Generalization

The goal is good predictions on unseen data. The standard assumption is that training and future examples are independent draws from the same distribution (i.i.d.). Use a training set to fit $\theta$, a validation set to select hyperparameters and checkpoints, and a test set kept out of model selection for final evaluation. Figure 1(b) shows one common pattern: training loss falls while validation loss eventually rises. Their difference estimates the generalization gap when measured under comparable conditions; training-time augmentation and dropout can make the losses less comparable. Early stopping, weight decay, dropout, augmentation and pretraining aim to improve held-out performance, not simply shrink this gap: an underfitting model can have a small gap.

<aside> 💡 Leakage and shift. Leakage happens when information from held-out data reaches training or model selection. Keep every image of a patient in the same split, fit preprocessing statistics on the training split only, and hold out whole sites or time periods when the model will meet new ones. The data distribution can change between training and use: in the images, $p(x)$, as with a new scanner (Figure 1(c)); in the label frequencies, $p(y)$, as when a model trained in a referral clinic is used for screening; or in the relation between them, $p(y\mid x)$, as when the definition of the disease changes. Some shift is always present. We call it domain shift when it starts to hurt performance (modules 3 and 6).

</aside>

2. Training deep networks

This course assumes that you can train a fully connected network with backpropagation and know why its gradients can vanish or explode (DD1420 lessons 4.1 to 4.4). This section covers only the parts of modern networks that later modules build on.

Figure 2. (a) A plain block computes F(a). (b) A residual block adds its input back to F(a), so the gradient has a direct path to earlier layers.

Figure 2. (a) A plain block computes F(a). (b) A residual block adds its input back to F(a), so the gradient has a direct path to earlier layers.

Residual connections. Very deep plain networks can have higher training error than shallower ones, an optimization problem rather than overfitting. A residual block learns a correction to its input: $a^{(l)}=a^{(l-1)}+F(a^{(l-1)})$ (Figure 2(b)). Its Jacobian is $I+J_F$, providing a direct gradient path that often makes deep networks easier to train, without guaranteeing stable gradients. Addition requires matching shapes; a projection can align them.

Normalization. BatchNorm normalizes each channel over batch and spatial dimensions, usually using running statistics at inference. With small batches of 3D volumes, GroupNorm (channel groups and spatial positions within each example) or InstanceNorm (spatial positions within each example and channel) avoids dependence on batch statistics. In transformers, LayerNorm centers and scales each token across features; RMSNorm instead divides by the root mean square without subtracting the mean, then applies a learned scale. These per-example methods also work with batch size one (module 4).