Generalization

Hold out a house, watch train vs held-out error, and name overfitting — why train loss alone is not success.

What generalization is

A model that only looks good on the houses it trained on has not learned the market — it has memorized the homework. Generalization means doing well on examples it did not see while the weights were moving.

This closes the supervised path from First Learning Model through Price Tier Classifier: always keep a held-out check, even when the dataset is tiny.

Training set
The examples you may run train / step on. Weights may look at these labels.
Held-out / test
Examples you score only with predict. If you train on them, the exam is leaked.
Overfitting
Train loss looks great; held-out error is poor. Too much memorization (often too much capacity or too many epochs on too little data).
Underfitting
Train loss stays high. The model is too simple or not trained enough to capture the pattern.

Habit

Split the five houses

Smallest honest protocol on HOUSES: train on four, check the fifth. Step through the story.

Train vs held-out on HOUSES
H1both
H2both
H3both
H4both
H5both
StatusAll five houses

Until now every lesson trained and scored on the same five rows. Loss going down felt like success — but the model had already seen every test.

What happens in this step

H1 H2 H3 H4 H5
all used for training and for “how good are we?”
Step 1 of 4

Why it bites

Capacity, epochs, and five rows

A wide network ( Price Tier Classifier with many hidden units) can drive training loss to zero on four houses and still miss H5. More epochs on the same four rows make memorization easier, not harder.

More knobs ≠ automatically smarter
Too smallunderfitEnoughboth errors ↓Too bigoverfit
  • regime
StatusUnderfit

Train loss stays high. Linear with one feature on a bent market — not enough shape.

Step 1 of 3
Feature scale is part of this story. Unscaled sqft forces tiny learning rates and uneven learning across weights. Scaling inputs ( sqft/1000) is not a separate “advanced” topic — it is how you give every feature a fair vote so the model can generalize.

Solution in TypeScript

The code change is tiny. The habit is the lesson: never judge a model only on the rows that updated its weights.

generalization.tsTypeScript
type Vector = number[];

const HOUSES: [Vector, number][] = [
  [[1200, 2, 15], 245_000], // H1 train
  [[1800, 3,  8], 310_000], // H2 train
  [[2200, 4,  3], 420_000], // H3 train
  [[ 900, 2, 40], 180_000], // H4 train
  [[1500, 3, 10], 295_000], // H5 held out
];

const train = HOUSES.slice(0, 4);
const heldOut = HOUSES[4];

// Train your model ONLY on `train`.
// Then compare:
//   trainLoss  = average error on H1–H4
//   heldOutErr = error on H5 alone
//
// If trainLoss → 0 but heldOutErr is large, you overfit the four houses.

console.log("train size", train.length);
console.log("held out", heldOut);

Next

Supervised spine is complete

You can predict a price, name the optimizer, bend with neurons, classify a tier, and refuse to trust train loss alone. Optional paths from here: online / RL loops, or deeper architectures — not required to understand the engine.

Trail: First Learning ModelVectorsLinear RegressionGradient DescentNeuronNetworkClassifier → Generalization.

Keep reading

TopicDescription
Machine LearningHow a model improves from examples: labels, features, training, and the difference between fitting data and predicting on new data.
Your First Learning ModelOne feature, one weight, and a training loop that discovers dollars per square foot from a sold house.
Vectors and WeightsPack sqft, bedrooms, and age into a feature vector, pair it with one weight per feature, and predict with a dot product.
Linear RegressionTrain a linear model with weights and bias on all five houses, watch MSE fall, then predict a new listing.
Gradient DescentWalk downhill on error: one weight on a loss bowl, then the same step on every knob — with a learning-rate dial.
A Single NeuronAdd ReLU to the weighted sum you already know — one neuron that can bend, and why XOR still needs a network.