Fully connected networks:
a class and a number

Session 21 · Wed Nov 18 · Book sections 4.1–4.2

🎯

Today’s question

A table of features has no order between its columns.

What does a fully connected network add to logistic regression and a forest, for a class and for a number?

Reading: 4.1 Neural networks (classification) · 4.2 Regression with a neural network · training and evaluating any network: lecture 24a

Four canonical networks: today, fully connected

The structure of the data chooses the network. A table of features has no order: every input connects to every neuron, and the network assumes nothing.

This lecture in the literature

From one neuron to deep networks, then back to one neuron, and to trees.

Rosenblatt (1958) Psychol. Rev. 65, 386–408 · Rumelhart, Hinton & Williams (1986) Nature 323, 533–536 · Cybenko (1989) Math. Control Signals Syst. 2, 303–314; Hornik, Stinchcombe & White (1989) Neural Netw. 2, 359–366 · DeVries et al. (2018) Nature 560, 632–634 · Mignan & Broccardo (2019) Nature 574, E1–E3 · Grinsztajn, Oyallon & Varoquaux (2022) NeurIPS 35. AUC values: our rerun of the open release (figs/lit_aftershock_2018/numbers.json).

Inside an MLP: every input to every neuron

61 features in, 32 hidden neurons with ReLU, 4 scores out: 2,116 weights (notebook 4.1). The softmax turns the scores into probabilities.

One dense layer by hand

Each neuron: a weighted sum of all inputs plus its bias, then ReLU. Here z1 = 1.60 passes; z2 = −1.55 becomes 0. In matrix form, z = W x + b.

Each hidden neuron cuts the input space

One ReLU neuron draws one straight line: 66% of the training samples right on a ring. Three lines enclose the core: 98%. Sixteen smooth the edge: 98%.

How many weights to fit?

weights = outputs × (inputs + 1): 2,116 in 4.1, 1,313 in 4.2. Start small, one hidden layer of 16 to 64 neurons, and grow only if the training loss stays high.

Wide and shallow, or deep and narrow?

At about 17,000 weights each: 256 neurons in one layer, or 64 in each of four. Depth composes cuts of cuts; for a table of features, one or two hidden layers are usually enough.

An MLP sees no order

Shuffle the samples of every window in the same way and the MLP scores the same, 57.6% and 57.8%: it never used the shape of the waveform. Right for a table, wasteful for a waveform.

Scale the inputs first

In km and magnitude units the loss is a narrow valley: after 60 steps the magnitude weight is −0.03 of −0.76. Scaled to unit variance, the same 60 steps reach the lowest loss.

Dropout: train with random neurons switched off

In each training batch, each hidden neuron is off with probability 0.3, so no neuron can rely on one particular other; at evaluation every neuron works.

Classification or regression: the head decides

Classification: one score per class, softmax, cross-entropy (4.1). Regression: one number, no activation, squared, absolute or Huber error (4.2). The hidden layers can be identical.

Two problems, one loop

Classification (4.1) Regression (4.2)
Sample, input X seismic event: 61 features GNSS window: 6 numbers
Y, to predict source type, 4 classes position in 30 days (mm)
Last layer, loss 4 scores; cross-entropy 1 number; MSE, MAE, Huber
Score, baseline accuracy; logistic error in mm; linear

P395: a station that keeps moving

📍 In-situ sensor 🪨 Solid Earth 📐 Geodesy

1,482 samples, one every 5 days, 2006–2026. Train on the first 70%, validate on the next 15%, test on the last 15%, in time order, with a gap so that no 30-day window overlaps.

The loss for a number: what a large error costs

Squared error makes large errors dominate the mean; absolute error weights every millimeter the same; Huber is squared near zero and absolute beyond.

The loss for a class: the probability given to the truth

Cross-entropy only looks at the probability the network gives to the observed class: 0.9 costs 0.11, a uniform guess over 4 classes costs 1.39, confident and wrong costs without bound.

Classification: loss curves, confusion matrix

📍 In-situ sensor 🪨 Solid Earth 📈 Seismology

One hidden layer: 87.1%. Three hidden layers with dropout: 89.2%. Without dropout the validation loss climbs after epoch 11. Earthquakes and explosions are confused most.

Leaderboard 1: four source types (4.1)

Model Weights Test accuracy, 800 events
three hidden layers, dropout 18,852 89.2%
random forest, 300 trees — 88.5%
three hidden layers, no dropout 18,852 87.6%
one hidden layer of 32 2,116 87.1%
logistic regression 248 86.8%
majority class 0 25%

One split, one seed; one standard error is 1.2 points. The best network ties the forest, and the one-layer network ties logistic regression.

Regression: the test samples lie outside the training range

Every test observation, −132 to −111 mm, is below the training range, −94 to 0 mm. A network that predicts the position itself must extrapolate.

Leaderboard 2: P395, 30 days ahead (4.2)

Model Weights Test error (mm), 3 seeds
linear regression 7 0.63
network on the change, absolute error 1,313 0.70 ± 0.01
network on the change, Huber 1,313 0.72 ± 0.01
network on the change, squared error 1,313 0.73 ± 0.02
persistence 0 1.02
network on the position 1,313 2.45 ± 1.88

The linear model wins; the three losses tie; predicting the position loses to persistence. On six hand-built numbers the network adds weights and no skill.

Best practices: the fully connected network

Step Practice Rubric
Frame split along the deployment axis A1–A4
Check inside the training range? A7
Baseline no learning; linear or logistic A6
Design small; last layer fits Y C1
Train seed; loss; curves; checkpoint D1–D6
Evaluate test once; units; three seeds D7–E3

Open 4.1 sections 7–9, then 4.2

  1. 4.1: dropout and batch normalization; compare the learning curves
  2. 4.1: save the best model with a checkpoint
  3. 4.2: build X, Y and the time split; compute persistence
  4. 4.2: predict the change; compare the three losses

pixi run jupyter lab · PyTorch names: nn.MSELoss, nn.L1Loss, nn.HuberLoss, nn.CrossEntropyLoss, optim.Adam, state_dict

Four canonical networks: where fully connected fits

Fully connected: for features already computed. It tied a forest on 61 features (4.1) and lost to a linear model on 6 numbers (4.2).