Week 06 · Regularization and training diagnostics

Compare weight decay, dropout, and validation-based checkpoint selection.

← Week 5 · Subject contents · Week 7 →

Compare weight decay, dropout, and validation-based checkpoint selection.

Work through the problem

A penalty λ‖w‖²/2 adds λw to the gradient. In SGD with no momentum this multiplies the weight by 1−ηλ before adding the data update. In inverted dropout a retained activation is scaled by 1/(1−p), so its expected value during training stays constant. Dropout is turned off at evaluation.

Use the validation curve to select an epoch for each configuration. A fall in training error with a rising validation error suggests the later fit transfers worse. Several random seeds would be needed to compare these techniques as policies.

Compare validation curves on the same split. Validation MSE by epoch. Series: Plain, Decay, Dropout.

Compare validation curves on the same split.

Run the Python lab

import torch
from torch import nn
torch.set_num_threads(1)
torch.manual_seed(5)
x = torch.linspace(-2,2,80)[:,None]
y = torch.sin(3*x) + .2*torch.randn_like(x)
train, val = torch.arange(0,80,2),torch.arange(1,80,2)
curves = {}
for name, dropout, decay in [("Plain",0.,0.),("Decay",0.,.03),("Dropout",.2,0.)]:
    torch.manual_seed(9)
    model = nn.Sequential(nn.Linear(1,24),nn.Tanh(),nn.Dropout(dropout),nn.Linear(24,1))
    opt = torch.optim.AdamW(model.parameters(),lr=.03,weight_decay=decay)
    errors = []
    for _ in range(160):
        model.train()
        opt.zero_grad()
        (model(x[train])-y[train]).square().mean().backward()
        opt.step()
        model.eval()
        with torch.no_grad():
            errors.append((model(x[val])-y[val]).square().mean().item())
    curves[name] = errors
    print(name,"best epoch",1+errors.index(min(errors)),"validation",round(min(errors),3))
assert all(min(v)<v[0] for v in curves.values())

Output from the lab

Plain best epoch 160 validation 0.068
Decay best epoch 160 validation 0.069
Dropout best epoch 154 validation 0.186

Practice

  1. Why is the best validation epoch not a final performance estimate?

Check your reasoning

  1. The validation results informed the choice. Measure final error on data held apart from those decisions.

← Week 5 · Subject contents · Week 7 →

Further reading · Goodfellow, Bengio, and Courville · Deep Learning