AI from scratch · Model zoo 9 of 10 · Neural networks on tables

Multilayer perceptrons on tables

Stage 2 built a neural network neuron by neuron and trained it with backpropagation. Stage 5, next on the path, shows what networks are unbeatable at: pixels, sound, text. This page asks the narrower question an ML engineer faces weekly: on an ordinary table of columns, is a network worth it? Usually not, and the reasons are worth knowing exactly, because they're also the reasons networks need so much care everywhere else.

1. The idea in one picture: many straight edges, blended

A one-hidden-layer network on two columns: each hidden unit is a logistic regression of its own (page 2), with a straight edge across the plane (the thin lines). The output unit adds their soft yes/no answers with weights, and the result can bend into any shape the edges can be combined into. Change the number of hidden units and watch the lines and the map.

Pick ring around a disc. Predict first: what's the fewest hidden units that can draw a closed loop around the disc?

With 1 or 2 straight edges the "inside" region is never closed; with 3, their soft answers can add up to "inside all three edges", a rounded triangle around the disc, and more units round it off further. Each hidden unit adds one edge to work with. That's the universal approximation idea: enough units can approximate any boundary, which says nothing about whether training finds it. Switch to the checkerboard at 3 units and move the seed slider: the same network, started from different random weights, lands anywhere from 69% to 90%.

A hidden layer is a set of logistic regressions whose answers the output combines, so a network can draw any boundary; finding it depends on training, scaling and the starting seed.

2. How it learns: stage 2, in one formula

\[ h = \tanh(W_1 x + b_1), \qquad p = \sigma(w_2 \cdot h + b_2) \]

Trained by gradient descent on log-loss, with gradients from backpropagation (chapter 4). Everything you know about training applies: the learning rate, Adam (toolkit 3), initialization and normalization (toolkit 4), early stopping. That's the first practical difference from trees: a forest has one or two knobs and fits the same way every time; a network has a dozen, and a random start.

Why scaling and seeds matter so much more for a network than for a tree
  1. A hidden unit computes \(\tanh(w \cdot x + b)\). If a column runs in the thousands (pdays is 999 for most calls), \(w \cdot x\) is huge at the start, tanh sits flat at ±1, and its slope there is near 0 (calculus 1): the unit barely learns. Scaled columns keep units in the sloped middle.
  2. The gradient step for a weight is proportional to its input, so columns in big units take big steps and columns in small units crawl: the loss surface is a long narrow valley (toolkit 3), which a single learning rate handles badly.
  3. A tree only compares a column with a cut, so neither problem exists for it: rescaling a column changes no split.
  4. The loss of a network is not a bowl (calculus 5's relabeled-twins bump): different random starts end in different minima, with different test scores. A tree's greedy fit has no random start (forests have seeds, but averaging hundreds of trees makes them matter little).
  5. So for a network on a table: scale the inputs, scale a numeric target, try a few seeds and average or pick on validation data, and use early stopping. Section 4 measures what skipping each one costs.

3. Build it: a two-layer network in NumPy

4. Where it shines, where it breaks (on tables)

Shines: a network learns its own features, which is decisive when the columns are raw pixels, audio samples or tokens. On a table it can still be competitive: on the bank calls, a 64-unit network scaled and early-stopped averaged AUC 0.772 over 10 seeds, level with boosted trees (0.760) and logistic regression (0.779). It also mixes easily with other inputs: a network can take a table's columns and a text embedding in the same model, and it trains on streaming data a batch at a time. And it's what to use when you need one model for many outputs at once.

But it asks more of you. Measured on the same data:

What went wrongNetworkBoosted trees
Nothing (scaled, early stopping)AUC 0.772 (seeds: 0.759 to 0.779)0.760 (any seed)
Columns not scaled0.7250.760 (scale doesn't matter to splits)
100 columns of noise added0.7480.769

A network predicting daily subway riders (about 2.5 million a day) had a test error of 3.2 million riders a day, worse than predicting 0. The inputs were scaled. What's the most likely cause?

The output unit starts out predicting numbers near 0, and gradient steps sized for scaled inputs take forever to reach 2.5 million (and huge errors make huge, unstable gradients). Measured: 3.2 million unscaled; with the target standardized (TransformedTargetRegressor), 0.26 million. Trees never see this problem: a leaf just predicts an average of targets.

On tables a network can match the others, but it needs scaled inputs and targets, several seeds and early stopping, and it suffers more from useless columns; boosted trees give the same score with less care.

Where it breaks, and what people do

5. The knobs that matter

KnobIn scikit-learn (or PyTorch)What it does
ScalingStandardScaler; TransformedTargetRegressor for the targetNot optional: inputs and numeric targets on a unit scale.
Width and depthhidden_layer_sizesOne or two layers of 32 to 256 units is plenty for most tables.
Penaltyalpha (L2 on weights; weight decay in PyTorch)Shrinks weights; essential on small data.
Early stoppingearly_stopping=True, validation_fractionStops when validation loss stops falling.
Optimizer and ratesolver="adam", learning_rate_initToolkit 3: Adam at 1e-3 is the usual start.
Seedsrandom_stateTry several; average their predictions (an ensemble) or report the spread.

Next: every model in the zoo side by side, and how to choose.

All chapters: AI from scratch. Bank marketing: Moro, Cortez and Rita (2014), UCI, CC BY 4.0. Ridership: MTA, data.ny.gov.

Last updated