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?
- 1
- 3: three edges can fence in a triangle
- 8
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
- 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.
- 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.
- A tree only compares a column with a cut, so neither problem exists for it: rescaling a column changes no split.
- 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).
- 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 wrong | Network | Boosted trees |
|---|---|---|
| Nothing (scaled, early stopping) | AUC 0.772 (seeds: 0.759 to 0.779) | 0.760 (any seed) |
| Columns not scaled | 0.725 | 0.760 (scale doesn't matter to splits) |
| 100 columns of noise added | 0.748 | 0.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?
- Too few hidden units
- Overfitting
- The target wasn't scaled: the output must reach millions from weights that start near 1
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
- Useless columns. Every input gets weights, and noise columns add noise to every hidden unit; trees just never split on them. Select columns first, or add an L1 penalty on the first layer.
- Irregular patterns. Tables often have sharp jumps ("pdays below 16") that trees capture in one split; a smooth network needs many units to make a cliff. Studies comparing the two on dozens of tables (Grinsztajn, Oyallon and Varoquaux, 2022) found this, and useless columns, to be the main reasons trees win.
- Categories. One-hot columns work; with thousands of values, learn an embedding per category (stage 5's word vectors, for store ids or zip codes), which is where networks pull ahead on huge tables.
- Small data. With hundreds of rows, a network's many weights overfit; a penalty (
alpha), early stopping and a small hidden layer help, but logistic regression usually wins. - Explaining it. No rules to print, no odds ratios: only after-the-fact tools (permutation importance, SHAP).
5. The knobs that matter
| Knob | In scikit-learn (or PyTorch) | What it does |
|---|---|---|
| Scaling | StandardScaler; TransformedTargetRegressor for the target | Not optional: inputs and numeric targets on a unit scale. |
| Width and depth | hidden_layer_sizes | One or two layers of 32 to 256 units is plenty for most tables. |
| Penalty | alpha (L2 on weights; weight decay in PyTorch) | Shrinks weights; essential on small data. |
| Early stopping | early_stopping=True, validation_fraction | Stops when validation loss stops falling. |
| Optimizer and rate | solver="adam", learning_rate_init | Toolkit 3: Adam at 1e-3 is the usual start. |
| Seeds | random_state | Try 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