Week 12 · Capstone · Diagnose a neural network with an ablation

Train a nonlinear model and reproduce a failed run with one change.

← Week 11 · Subject contents

Train a nonlinear model and reproduce a failed run with one change.

Work through the problem

Classify the signs of two inputs using XOR. The classes occupy alternating quadrants. A single linear decision boundary cannot separate all four. A hidden layer with a nonlinear activation can combine several linear regions. Removing the activation leaves a composition of affine maps, which is still affine.

Train the same architecture twice with identical weights, data, optimizer and update budget. Change only whether ReLU is active. Report held-out accuracy, inspect the loss curves, and include a failed case near a quadrant boundary. The exercise tests one architectural hypothesis on one data distribution.

An activation changes what the network can learn. Training cross-entropy by update. Series: ReLU, Identity.

An activation changes what the network can learn.

Run the Python lab

import torch
from torch import nn
torch.set_num_threads(1)
torch.manual_seed(33)
X = torch.rand(640,2)*2-1
y = ((X[:,0]>0) ^ (X[:,1]>0)).float()
curves, accuracy = {},{}
for name, activation in [("ReLU",nn.ReLU()),("Identity",nn.Identity())]:
    torch.manual_seed(16)
    model = nn.Sequential(nn.Linear(2,12),activation,nn.Linear(12,1))
    opt = torch.optim.Adam(model.parameters(),lr=.03)
    trace = []
    for _ in range(240):
        opt.zero_grad()
        loss = nn.functional.binary_cross_entropy_with_logits(model(X[:480])[:,0],y[:480])
        trace.append(loss.item())
        loss.backward()
        opt.step()
    pred = model(X[480:])[:,0]>0
    accuracy[name] = (pred==y[480:]).float().mean().item()
    curves[name]=trace
    errors = X[480:][pred!=y[480:]]
    print(name,round(accuracy[name],3),"closest mistaken inputs:",errors[errors.abs().min(dim=1).values.argmin()].tolist() if len(errors) else [])
assert accuracy["ReLU"] > accuracy["Identity"] + .2

Output from the lab

ReLU 0.988 closest mistaken inputs: [-0.0063468217849731445, -0.7321820259094238]
Identity 0.494 closest mistaken inputs: [0.4903963804244995, 0.0010576248168945312]

Practice

  1. Why can the linear run still do better than random on one finite split?

Check your reasoning

  1. A line can classify some quadrants and the empirical class balance fluctuates. Compare the observed gap with more seeds and test samples.

← Week 11 · Subject contents

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