Learning and baselines: minimize the training loss, judge on held-out data, and always beat the mean.
Linear models and ridge: \(\hat{\mathbf y} = X\mathbf w\); adding \(\lambda\lVert\mathbf w\rVert^2\) to the loss shrinks the weights and tames overfitting.
Descriptors we design: fraction-weighted mean, deviation and range of elemental properties; structure fingerprints.
Gaussian processes, trees and forests: flexible models with error bars or averaging; strong on small data.
Honest validation and unsupervised learning: grouped splits, baselines, PCA and clustering.
Every model so far had either a fixed form (a line, a kernel) or worked on features we designed. Can a model learn its own features from raw input, given enough data?
left: a linear model of our features · right: a linear model of learned features
Neural networks, backpropagation by hand, training and regularization, PyTorch. Team project teams are formed this week.
Why does a stack of ten linear layers have no more power than a single one?
A network that reads only element fractions predicts formation energies well. Why does a random forest on hand-made descriptors still win on a few hundred compounds?
A network can have far more weights than training points. Why does it not always overfit, and how do we stop it when it does?
Why must the weights start as small random numbers, and not all at zero?
One straight boundary, \(\mathbf w^{\mathsf T}\mathbf x + w_0 = 0\). Anything that needs a bent or broken boundary is out of reach.
\(\sigma(0) = 0.5 \Rightarrow \sigma'(0) = 0.5\times0.5 = 0.25\) (the maximum)
\(\sigma(2) = 0.881 \Rightarrow \sigma'(2) = 0.881\times0.119 = 0.105\)
Through ten sigmoid layers the gradient can shrink a million-fold (the vanishing gradient), so the early layers barely learn. An active ReLU unit has slope exactly 1.
Each unit with a bend is a new feature learned from data. The output is a linear model of these learned features, so the network is as flexible as its hidden layers.
A wide-gap oxide conducts with a donor or an acceptor, but not with both: they compensate each other.
| D | A | h₁ | h₂ | ŷ |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 1 | 2 | 1 | 0 |
14 simulated measurements (noise 0.02 eV) for x ≤ 0.8, from the standard direct (Γ) and indirect (X) gap curves at 300 K. Network: 1 → 2 → 1, ReLU.
Each hidden unit is one hinge. The first kink sits at x = 0.44, next to the real crossover at 0.45. Beyond the data it just extends its last straight piece: 0.26 eV too low for AlAs. Trained on x ≤ 0.4 only, it predicts 2.9 eV at x = 0.8 (true 2.1 eV).
Shapes: δₕ (2×1) times xᵀ (1×2) is 2×2, like W₁. Link: a sigmoid output with log loss gives δ = p − y, the logistic-regression gradient of lecture 5.
One backward pass costs about as much as one forward pass, and gives the gradient of every weight at once: millions of weights for the price of two passes.
Given: two scaled features of one compound, x = (1.0, 0.5), and its scaled target y = 2.0 (illustrative numbers).
\(W_1 = \begin{bmatrix}0.5 & 1.0\\ -0.5 & 1.0\end{bmatrix},\ \mathbf b_1 = \begin{bmatrix}0\\0.5\end{bmatrix},\ \mathbf w_2 = \begin{bmatrix}1.0\\0.5\end{bmatrix},\ b_2 = 0.25\)
Both z are positive, so both units are active and the gradient will pass through both. The prediction is 0.5 too low.
| before | after one step | |
|---|---|---|
| prediction ŷ | 1.500 | 1.760 |
| loss ℓ | 0.125 | 0.029 |
Change W₁₁ by ±10⁻⁴ and recompute the loss:
\[ \frac{\ell(+) - \ell(-)}{2\times10^{-4}} = -0.5000 \]the same as backpropagation.
All gradients are negative, so every weight increases (becomes more positive) to raise ŷ. Weights fed by x₁ = 1.0 move twice as much as those fed by x₂ = 0.5. A unit with z < 0 would get zero gradient: it is switched off for this example.
Gradient descent 0.028 · momentum 0.0005 · Adam (η = 0.15) 0.29. On a smooth bowl momentum wins; Adam's strength is different: it rescales each weight separately.
| gradient g | step t | SGD η = 0.01 | momentum η = 0.01, β = 0.9 | Adam η = 0.001 |
|---|---|---|---|---|
| 0.5 | 1 | 0.005 | 0.005 | 0.001 |
| 0.5 | 2 | 0.005 | 0.0095 | 0.001 |
| 0.5 | 3 | 0.005 | 0.01355 | 0.001 |
| 50 | 1 | 0.5 | 0.5 | 0.001 |
Size of the downhill step taken by each optimizer, for a constant gradient.
A weight with 100 times larger gradients (g = 50) gets a 100 times larger SGD step, but the same Adam step. Adam gives every weight its own scale, which matters in deep networks, where gradients of different layers differ by orders of magnitude.
12 simulated points of the lecture-1 InGaAs bowing curve (noise 0.05 eV), 200 validation points. Network 1 → 64 → 64 → 1: 4 353 parameters, Adam.
Early stopping: keep the weights with the lowest validation error (epoch 1 155: 0.059 eV), not the last ones (0.090 eV).
| method | what it does | link |
|---|---|---|
| weight decay | shrinks all weights | ridge, lecture 2 |
| early stopping | stops before memorizing | validation set, lecture 6 |
| dropout | no unit can rely on another; like averaging many thinned networks | random forests, lecture 5 |
| more data, augmentation | the best regularizer of all | lecture 10 |
Regularization does not change what a network can represent. It changes which of the many functions that fit the training data it chooses: the smoother, simpler ones.
The fraction of each element: Fe₂O₃ → Fe 0.4, O 0.6, all others 0. No descriptors, no structure.
Dense ReLU layers (ElemNet used 17). The example drawn, 86 → 256 → 128 → 64 → 1, has 63 489 parameters.
Trained on DFT formation energies from the OQMD (of order 10⁵ compounds): test MAE about 0.05 eV/atom, better than models on hand-made descriptors at that data size.
The first layers turn 86 fractions into learned chemical features, playing the role of our lecture-3 descriptors, but only for chemistry like the training set; anything else is extrapolation.
Simulated formation energies of binary and ternary compounds of 28 elements (pair interactions plus chemistry that no descriptor encodes; noise 0.03 eV/atom). Test: 2 000 held-out compounds.
| model (input) | N = 100 | N = 4000 |
|---|---|---|
| mean (baseline) | ||
| ridge (15 descriptors) | ||
| random forest (15 descriptors) | ||
| network (28 fractions) |
Descriptors carry our chemical knowledge, so the forest needs few examples, but it cannot learn what they leave out. The network starts from nothing: poor on small data, best on large data.
import torch x = torch.tensor([1.0, 0.5]); y = torch.tensor(2.0) W1 = torch.tensor([[0.5, 1.0], [-0.5, 1.0]], requires_grad=True) b1 = torch.tensor([0.0, 0.5], requires_grad=True) w2 = torch.tensor([1.0, 0.5], requires_grad=True) b2 = torch.tensor(0.25, requires_grad=True) h = torch.relu(W1 @ x + b1) yhat = w2 @ h + b2 # 1.5 loss = 0.5 * (yhat - y) ** 2 # 0.125 loss.backward() # backprop print(W1.grad) # [[-0.50, -0.25], [-0.25, -0.125]]
net = torch.nn.Sequential( torch.nn.Linear(86, 256), torch.nn.ReLU(), torch.nn.Linear(256, 128), torch.nn.ReLU(), torch.nn.Linear(128, 1)) opt = torch.optim.Adam(net.parameters(), lr=1e-3, weight_decay=1e-5) # with Adam, prefer optim.AdamW: true weight decay for epoch in range(200): for xb, yb in loader: # batches of 32 opt.zero_grad() # clear old grads yh = net(xb).squeeze() loss = ((yh - yb) ** 2).mean() loss.backward() # all gradients opt.step() # Adam update
import torch, numpy as np from lab09_utils import load_fractions, magpie, mlp, group_split X, y = load_fractions("formation_subset.csv") # n x 86 # split by chemical system, not at random (lecture 6): # polymorphs of one formula never straddle the split tr, va, te = group_split(X, by="chemical_system", sizes=(0.8, 0.1, 0.1), seed=0) from sklearn.ensemble import RandomForestRegressor rf = RandomForestRegressor(300, n_jobs=-1) rf.fit(magpie(X[tr]), y[tr]) net = mlp([86, 256, 128, 1]) best = net.fit(X[tr], y[tr], X[va], y[va], lr=1e-3, weight_decay=1e-5, patience=20) # early stopping for model, Xte in [(rf, magpie(X[te])), (net, X[te])]: mae = np.abs(model.predict(Xte) - y[te]).mean() print(type(model).__name__, "MAE", round(mae, 3))
Teams of three are formed this week. One-page proposal due in week 10.
A product of matrices is one matrix: \(W_{10}\cdots W_1 = W'\). Without a bend, depth adds no power. ReLU units make hinges, and hinges add up to any shape.
Descriptors carry chemical knowledge; the network must learn it from examples. In our simulation the forest won below about 500 compounds, the network above.
Training from small weights fits the smooth trend first and the noise later. Stop early (0.059 vs 0.090 eV), decay the weights, use dropout, add data.
Identical units get identical gradients and stay identical forever: the layer acts like one unit. With all weights zero, h = 0 and w₂ = 0, so W₁ and w₂ never get a gradient: only the output bias learns, and the network predicts a constant. Random starts break the symmetry.
Lab 9 · project teams of three formed · Problem set 3 (weeks 9–10) due in week 11.
Convolution as a learned filter, by hand; CNNs, U-Net segmentation of micrographs, transfer learning, and 1-D networks for XRD patterns.
All schematics and plots were drawn for this course. The AlGaAs, InGaAs and formation-energy data are simulated (labelled on the slides); all numbers were computed with numpy for this lecture. The 2-2-1 network numbers are illustrative.