Feature Scaling & Weight Bias

By Updated

Adjust the sliders below to pass different values. Notice how raw, unscaled inputs (like Salary = 80,000) cause massive color shifts in the connections, leading to an imbalanced network. Use the scaler to normalize the data and stabilize training.

Overview

Why scale breaks gradient descent

The gradient of the loss with respect to a weight is proportional to that weight’s input. So a feature measured in hundreds of thousands produces gradients roughly four orders of magnitude larger than a feature measured in tens.

That leaves no workable learning rate. Set it small enough to keep the salary weight stable and the age weight barely moves; set it large enough for age and salary diverges. The loss surface is a long narrow valley, and gradient descent bounces across it instead of running down it.

Scaling makes the valley round. With comparable input ranges the gradients are comparable, one learning rate suits every weight, and convergence takes a fraction of the steps.

35
75k

Network Analysis

Network Status
STABLE
Weights are updating evenly.

Current Input Data Passed

Age (Node 1)
Raw: 0 0.0
Salary (Node 2)
Raw: 0 0.0

Avg Weight Magnitudes

Age Weights (W1) 0.100
Salary Weights (W2) 0.100
Drag to Pan | Scroll to Zoom

Feature Scaling & Weight Bias: A Practical Guide

Age runs from 18 to 80; salary runs from 20,000 to 200,000. Feed both into a network unscaled and the salary weight receives gradients thousands of times larger - so no single learning rate can train both.

Standardisation and normalisation

Standardisation (z-score) centres on zero with unit variance:

x′ = (x − μ) / σ

Output is unbounded but typically within −3 to +3. It preserves the shape of the distribution and handles outliers without letting them compress everything else. This is the sensible default for neural networks.

Min-max normalisation maps to a fixed range, usually [0, 1]:

x′ = (x − min) / (max − min)

Useful when a bounded input is required, and fragile: a single extreme outlier sets the maximum and squashes every ordinary value into a narrow band near zero.

Robust scaling uses the median and interquartile range instead of mean and standard deviation, which is the right choice when outliers are present and genuine.

Work the numbers

Take age 30 and salary 60,000 with weights of 0.5 each. The salary term contributes 30,000 to the weighted sum and the age term contributes 15 — the age feature is invisible, and it would take a weight around 1000× larger to compete.

Standardise first. If age has mean 45 and standard deviation 15, and salary has mean 80,000 with standard deviation 40,000:

age′    = (30 − 45) / 15 = −1.0

salary′ = (60000 − 80000) / 40000 = −0.5

Both are now around 1 in magnitude, both contribute comparably, and both weights receive gradients of a similar size.

Why unscaled inputs break training

A network's first layer computes w · x + b. If one feature ranges over 0–1 and another over 0–200,000, then the second feature dominates that sum entirely, and the gradient with respect to its weight is hundreds of thousands of times larger than the gradient for the first.

Three things go wrong at once.

The loss surface becomes a ravine. Steep in one direction, nearly flat in another. A learning rate small enough to be stable in the steep direction is far too small to make progress in the flat one, so training crawls.

Activations saturate. A weighted sum in the tens of thousands pushes a sigmoid or tanh flat against its asymptote, where the derivative is essentially zero and the unit stops learning.

Initialisation assumptions break. He and Xavier initialisation both assume inputs of roughly unit variance. Feed a variance of 10⁹ and the first layer's activations are enormous from the first forward pass.

The fix is one line, and it is not optional. Unlike tree models, which compare features individually and are indifferent to units, every neural network needs its inputs on a comparable scale.

The three methods

MethodFormulaResultUse when
Standardisation(x − μ) / σMean 0, sd 1The default for networks
Min-max(x − min) / (max − min)Range 0–1Bounded inputs such as pixels
Robust(x − median) / IQRMedian 0Outliers present

Standardisation is the usual choice. It is unbounded, so an outlier does not compress everything else into a narrow band, and zero-centred inputs suit the symmetric initialisation schemes.

Min-max guarantees a range, which is why pixel values are divided by 255. Its weakness is sensitivity to outliers: one extreme value squashes every other value towards zero.

Robust scaling uses the median and interquartile range, so a handful of extreme values cannot distort the transformation.

For heavily skewed features — income, counts, populations — a log transform before scaling usually helps more than the choice of scaler, because it addresses the shape rather than just the range.

Fit on training data only

This is the rule that makes the difference between an honest evaluation and a leak.

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)     # compute mu and sigma HERE
X_val   = scaler.transform(X_val)           # apply the same numbers
X_test  = scaler.transform(X_test)

Calling fit_transform on validation or test data computes their means and standard deviations, which means information from those sets has influenced the training pipeline. The reported score is then optimistic, sometimes substantially.

Two further practical points. The fitted scaler must be saved and shipped with the model — a model served with unscaled inputs, or scaled by recomputed statistics, produces confident nonsense. And for neural networks, the target is often scaled too, in which case the predictions must be inverse-transformed before any error is reported, or the RMSE is in the wrong units.

Why an unscaled input slows everything down

Two features on different scales make the loss surface an ellipse, and gradient descent is bad at ellipses. This measures the condition number, then fixes it three ways.

example_01.pyNumPy
Output

Guided experiments

  1. Train unscaled. Set Data Normalization Method to none, put Salary Input near 200000 and Age Input near 30, and press Start Training. Training is unstable or crawls, because one input dominates every gradient.
  2. Standardise and repeat. Switch Data Normalization Method to standardisation, press Reset Weights and train again with the same inputs. Convergence is faster and far smoother.
  3. Compare the scalers. Run min-max against standardisation on the same values. Min-max compresses everything into [0, 1]; standardisation centres on zero and keeps the spread — which is why it pairs better with activations symmetric about zero.
  4. Change the learning rate under each. With scaling off, raise Learning Rate and watch it diverge. With scaling on, the same rate is stable. Scaling widens the range of learning rates that work at all.

Where this goes wrong

  • Fitting the scaler on the full dataset. Computing the mean and standard deviation before splitting leaks test information into training. Fit on train, then transform validation and test with those same statistics.
  • Refitting the scaler at inference. Production data must use the training statistics. Fitting a new scaler on a single incoming batch produces silently wrong inputs.
  • Scaling one-hot columns. They are already on a comparable scale, and standardising them destroys sparsity by making every zero non-zero.
  • Min-max with outliers. One extreme value compresses everything else into a sliver of the range. Use standardisation or robust scaling.
  • Scaling the target without inverting it. If you scale y for training, predictions come back in scaled units and must be transformed back before they mean anything.

In one line

Gradients scale with input magnitude, so unscaled features give some weights enormous gradients and others negligible ones, and no single learning rate serves both. Standardisation is the default for neural networks; min-max suits bounded inputs and breaks on outliers. Fit the scaler on the training split only, keep those statistics for validation, test and production, and leave one-hot columns alone.

Which models care

ModelNeeds scaling?Why
Neural networksYesGradients, initialisation, saturation
KNN, k-means, SVMYesBuilt on distances
Linear/logistic with regularisationYesThe penalty is on raw coefficient size
PCAYesIt maximises variance, which depends on units
Decision trees, forests, boostingNoSplits compare one feature at a time

The last row is the useful contrast. A tree asks "is income above 40,000?", and the answer does not change if income is measured in thousands. That is why gradient boosting needs almost no preprocessing and a neural network needs a pipeline.

Note that scaling does not help underfitting or overfitting directly — it makes optimisation work. A model that will not train at all often starts training the moment the inputs are scaled, which is why it is the first thing to check.

Scaling different kinds of input

Images. Divide by 255, then standardise per channel with the dataset's statistics — for ImageNet-pretrained models, the published mean and standard deviation. Getting this wrong costs several points of accuracy with no visible symptom.

Tabular data. Standardise numeric columns; leave one-hot columns alone (they are already 0/1, and scaling them distorts the regularisation penalty).

Text. Not applicable — tokens become embedding lookups, and the embeddings are learned at a sensible scale.

Audio. Normalise per clip, or use log-mel spectrograms, which compress the dynamic range in the same spirit as a log transform.

Time series. Scale using statistics from the training period only — using the whole series leaks future information into the past, which is the same mistake as fitting on the test set.

Questions people ask

Is scaling needed if I use batch normalisation? Yes. Batch normalisation normalises activations between layers; the first layer still receives the raw input.

Standardisation or min-max? Standardisation for most network inputs; min-max for naturally bounded values such as pixels.

Should I scale one-hot columns? No. They are already on a 0–1 scale, and scaling them changes how regularisation treats them.

What about the target variable? Scale it for neural network regression, and invert before reporting. Not needed for tree models.

What happens if a feature has zero variance? Standardisation divides by zero. Drop the column — a constant feature carries no information anyway.

Can I scale after splitting into batches? The statistics must come from the whole training set, not from a batch. Fit once, apply everywhere.

Recap in one screen

  • Networks need inputs on a comparable scale, or the loss surface becomes a ravine and activations saturate.
  • Standardise by default; min-max for bounded inputs; robust scaling when outliers are present.
  • Log-transform heavily skewed features before scaling.
  • Fit the scaler on training data only, and ship it with the model.
  • Trees do not need it; every distance-based, regularised or gradient-trained model does.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. Without scrolling back — what is the one-line takeaway from this module?

  2. What does this module say about “Why scale breaks gradient descent”?

  3. What does this module say about “Work the numbers”?

Cheat sheet

Feature Scaling & Weight Bias

The gradient of the loss with respect to a weight is proportional to that weight’s input. So a feature measured in hundreds of thousands produces gradients roughly four orders of magnitude larger than a feature measured in tens.

DEEP LEARNING · vizlearn.in/deep_learning/feature_scaling_in_neural_networks.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.