Robot Learning · Theory · M2
M2-T2 — Features and Normalization
The model only ever sees the numbers you give it. What those numbers mean, how big they are and which frame they are in decides whether learning can work at all.
Module M1 found that a linear policy fitted to expert demonstrations reached the goal in 0 of 100 episodes with raw [dx, dy] inputs and 88 of 100 from easy starts with unit-direction inputs. Same data, same learner, same fitting method. This lesson explains what a feature is, why that change mattered (a representation problem, not a scaling one), and what scaling does fix, which is the speed and stability of gradient descent in lesson M2-T3. It also covers one-hot encoding of categories, the leak that comes from computing statistics on the wrong rows, and the units and frames that quietly corrupt a dataset.
Learning objectives
After this lesson, you should be able to:
- Distinguish a representation problem (the model cannot express the answer) from a scaling problem (the optimiser struggles), using the Scout results.
- Standardise features with z-scores, fitting the mean and standard deviation on training data only.
- Compute the condition number of X′X for a feature set and explain how scaling changes it.
- Encode a categorical input one-hot, and check units and coordinate frames before training.
The intuition
A feature is a number you hand the model, computed from raw sensor data. Two things can go wrong with it, and they are different.
The feature may not contain what the model needs. The expert in Scout’s world steers a fixed-length step toward the goal. A linear model of [dx, dy] says “steer more when farther away”, because it can only scale its inputs. The right answer does not change with distance, so the model cannot express it. Dividing [dx, dy] by the distance gives a unit direction, and now a linear model can. No amount of rescaling the raw inputs changes this.
The features may be fine but badly scaled. If one feature is in metres (0 to 9) and another in millimetres (thousands), the loss surface is a long narrow valley. Gradient descent (lesson M2-T3) must use a step small enough for the steep direction and so crawls along the flat one. Standardising each feature to zero mean and unit spread turns the valley into a bowl.
The mathematics
Standardisation
For feature j with training mean μj and standard deviation σj, the z-score of a value is:
After this transform the training column has mean 0 and standard deviation 1. Other common scalings: min-max, x′ = (x − min)/(max − min) into [0, 1]; and robust scaling with the median and interquartile range, which resists outliers.
- μj, σj
- mean and standard deviation of feature j, computed on the training rows
- X
- n × d matrix of features, one row per example (a column of ones for the bias)
- X′X
- d × d Gram matrix; the Hessian of the squared-error loss is (2/n) X′X
- λmax, λmin
- largest and smallest eigenvalues of X′X
- κ = λmax / λmin
- condition number
The condition number
The squared-error loss of a linear model is a quadratic bowl whose curvature in direction vi is proportional to the eigenvalue λi of X′X. Contours are ellipses with semi-axes proportional to 1/√λi, so their elongation is √κ. A large κ has three costs:
- Slow gradient descent: the stable step size is set by λmax, and progress along the flat direction is governed by λmin. The error shrinks by about a factor (κ − 1)/(κ + 1) per step at best (derived in lesson M2-T3).
- Numerical sensitivity: solving the normal equations loses about log10(κ) digits, because forming X′X squares the conditioning of X itself.
- Awkward regularisation: a ridge penalty λ‖w‖² treats all weights alike, so it shrinks a weight on a small-valued feature (which needs a large weight) far more than one on a large-valued feature.
Two things standardisation does not do. It does not change the best linear predictions for plain least squares: scaling a column rescales its weight inversely and the fitted function is identical. And it cannot add information that the features lack.
Leakage of statistics
μ and σ are parameters estimated from data, so they must be estimated from the training rows only and then applied unchanged to validation, test and deployment data. Computing them on the whole dataset lets information about held-out rows influence training. The leak is usually small for a mean, but it breaks the rule that the test set plays no part in any choice, and it can be large when statistics drift with time or per robot (lesson M2-T6). In deployment the robot has no future data, so it must use the stored training statistics.
Categorical features: one-hot encoding
If the input is a category (door A, B or C; floor tile type), do not code it 1, 2, 3: that implies B is between A and C. Use one binary column per category, with exactly one 1 per row.
| Door | is_A | is_B | is_C |
|---|---|---|---|
| A | 1 | 0 | 0 |
| B | 0 | 1 | 0 |
| C | 0 | 0 | 1 |
With a bias column the three columns sum to the bias column, so X′X is singular. Drop one category or add a small ridge penalty. Binary indicators do not need z-scoring.
Units and frames
- Units: metres, millimetres and radians must be consistent within a column and understood across columns. Convert at the boundary and write the unit in the column name.
- Angles: a heading of 179° and −179° are 2° apart, but 358° apart as numbers. Encode angles as (cos θ, sin θ).
- Frames: [dx, dy] in the world frame and in the robot frame carry the same information but make different problems: in the robot frame “the goal is ahead and left” is the same for every pose. In RLCore’s world there is no heading, so the observation is in the world frame.
- Missing and special values: a range sensor’s “no return” often arrives as 0 or a huge number; handle it explicitly, not as a measurement.
A worked example
All numbers come from running RLCore: 30 expert demonstrations from starts(1, 30, 'all'), giving 945 (observation, action) pairs, scored on 100 held-out starts per set from seed 500, as in lesson M1-T6.
1. The M1 finding: representation, not scale
| Features | Training MSE | Easy starts | All starts | Near pillar line |
|---|---|---|---|---|
| Raw [dx, dy, 1] | 0.0745 | 0 / 100 | 0 / 100 | 0 / 100 |
| Unit direction [dx/|d|, dy/|d|, 1] | 0.0593 | 88 / 100 | 45 / 100 | 0 / 100 |
The raw fit gives weights of about 0.864 on the bias for x and almost nothing on dx and dy: the policy says “go east” everywhere and stops against the wall. Rescaling the raw columns cannot help; I fitted the same model with dy in millimetres and the training MSE is 0.0745 again, to the digits printed. Unit direction features help because the expert’s step length is fixed. They still fail near the pillar (0 of 100) because they contain nothing about the pillar, which the pillar-aware features of lesson M1-T6 add (100 of 100).
2. What scaling fixes: the condition number
Take the features [dx, dy, 1] on the 945 pairs. dx ranges from 0.45 to 8.50 m with mean 3.956 and standard deviation 2.047; dy ranges from −2.50 to 2.05 m with mean −0.144 and standard deviation 1.217. The eigenvalues of X′X:
| Feature set | λ₁ | λ₂ | λ₃ | κ = λ₁/λ₃ |
|---|---|---|---|---|
| Raw: dx (m), dy (m), 1 | 19 537 | 1 388 | 191.6 | 101.9 |
| Raw: dx (m), dy (mm), 1 | 1.42 × 10⁹ | 19 096 | 191.7 | 7.41 × 10⁶ |
| Z-scored dx, dy, and 1 | 1 029.9 | 945.0 | 860.1 | 1.197 |
The same data with dy accidentally in millimetres has a condition number of 7.4 million. Worse, the plain metre version is already at 102 because the columns have large nonzero means: the bias column and dx are nearly parallel (dx averages 3.96 m), and standardising removes that overlap. After z-scoring the three eigenvalues are 1030, 945 and 860, so κ = 1.20. For example, dx = 8.496 m becomes (8.496 − 3.956)/2.047 = 2.22 and dy = −2.496 m becomes (−2.496 + 0.144)/1.217 = −1.93.
The payoff is in lesson M2-T3: for the target ax, the largest stable learning rate is 0.0484 on the raw metre features and 0.918 on the z-scored ones, and gradient descent needs about 265 steps on the raw set but 2 on the z-scored set to remove 99.9% of the excess loss.
3. Leakage of statistics: how much, here?
Fit the scaler on the first 20 demonstrations (635 rows) and compare with the other 10 (310 rows). dx: mean 3.972 and standard deviation 2.043 on the 20, 3.925 and 2.055 on the held-out 10, 3.956 and 2.047 on all 30. The three differ by about 1% or less, so the leak would change nothing measurable in this simulation. That is an honest finding, not a licence: Scout’s world has no drift. A real log with battery sag, a new floor or a recalibrated lidar has statistics that move, and a scaler fitted on all days can quietly use the test days.
What the simulation simplifies. The features here are clean, noise-free and in one frame. The condition numbers are exact for this dataset but would be different for another set of starts; the pattern (huge for unit mismatches, near 1 after z-scoring) is general.
Engineering implications and limitations
A scaler is part of the model
Save μ and σ with the weights. A policy deployed without them receives inputs the model never saw. Version them together, and write a test that feeds one known input through the full pipeline.
Fit on training only, apply everywhere
Split first, compute statistics second. In a cross-validation loop, refit the scaler inside every fold.
Check ranges before training
Print min, max, mean and standard deviation for every column. A column that is constant has σ = 0 and divides by zero; a column with values near 1e9 is probably a timestamp; a minimum of −999 is a missing-value code.
Clip or guard at inference
A z-score of 40 in deployment means the input is far outside the training range, and a linear or neural model will extrapolate wildly. Clip, or better, flag it and fall back to a safe controller.
Prefer engineered features that encode what you know
Distance and bearing to the goal, a unit direction, the robot-frame coordinates of an obstacle: these cost little and often matter more than a larger model. Remember the cost: each is a hypothesis about what the policy needs, and the pillar-blind unit-direction policy shows what a missing feature does.
Mind the frame
When a model trained on one robot’s frame is used on another, a sign or axis convention can flip an output without any error message. Plot a few predictions against hand-computed values after every change of pipeline.
Common misconceptions
“Normalising the inputs would have fixed the 0% policy.”
No. Plain least squares gives the same predictions after any affine rescaling of a column. The raw-feature failure is about what a linear model of [dx, dy] can express; unit-direction features changed that.
“Compute the mean and standard deviation on all the data once, it is simpler.”
The held-out rows then influence training. Fit on training rows only and reuse the stored values; the effect is small when nothing drifts and large when something does.
“Encode categories as 1, 2, 3.”
That tells the model door B is between A and C and twice as far from A as C is. Use one-hot columns, and drop one when there is also a bias term.
“Scaling is only for neural networks.”
It matters for any gradient-based fit, including linear models, for regularised fits and for distance-based methods. It matters little for tree-based models.
Engineering takeaways
- A feature set must contain the information the answer depends on and be expressible by the model; raw [dx, dy] failed (0 of 100) where unit direction (88 of 100 on easy starts) succeeded.
- Standardise with z = (x − μ)/σ using training statistics only, and store them with the model.
- Scaling does not change plain least-squares predictions but does change the condition number: 101.9 and 7.4 × 10⁶ for the raw sets, 1.20 after z-scoring.
- Encode categories one-hot, angles as (cos, sin), and write units into column names; check frames before training.
- Compute statistics on training rows only; on drifting real logs the leak is not negligible.
References and further reading
General sources on feature scaling, conditioning and preprocessing.
- T. Hastie, R. Tibshirani and J. Friedman, “The Elements of Statistical Learning”, 2nd ed., Springer, 2009.
- L. N. Trefethen and D. Bau, “Numerical Linear Algebra”, SIAM, 1997.
- Y. LeCun, L. Bottou, G. B. Orr and K.-R. Müller, “Efficient BackProp”, in Neural Networks: Tricks of the Trade, Springer, 1998.
Knowledge check
Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
A dataset has distance in metres (0 to 10) and a wheel encoder count (0 to 50 000). What happens to the condition number of X′X, and what do you do?
Explanation
It becomes huge, because the eigenvalues scale with the squared column sizes (roughly 5 000² times apart), so gradient descent is slow or unstable. Standardise both columns with training statistics so each has mean 0 and spread 1.
Question 2
Why was rescaling [dx, dy] not the fix for the 0% policy in Scout’s hall?
Explanation
The model is linear in its inputs, so rescaling gives identical predictions. The expert’s step has fixed length, which a linear map of raw [dx, dy] cannot produce; dividing by the distance (unit direction) changes what the model can express.
Question 3
You standardise using the mean and standard deviation of train plus test rows. Is this a problem?
Explanation
Yes in principle: the test rows influenced a quantity used in training, so the test score is slightly optimistic. Fit the scaler on training rows and reuse the stored values. The effect is tiny when nothing drifts but can be large with time-varying data.
What’s next
Next: lesson M2-T3 turns the loss surface into an algorithm. It derives the gradient of the squared error, the learning-rate limit 2/λmax, and why the condition number set the step counts quoted above.
Related content
Nothing below is required to finish this lesson.
- BeforeM2-T1: Regression and Classification for Robots
Say which robot prediction problems are regression and which are classification, and what the inputs and outputs are.
- LabM2-L1: Fit Steering with Gradient Descent
Predict Scout's steering command from its state with a linear model trained by gradient descent that currently diverges.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.