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.

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

Overview

Adjust the sliders below to pass different values.

In this guide
  1. Why scale breaks gradient descent
  2. Standardisation and normalisation
  3. Work the counts
  4. Why unscaled inputs break training
  5. The three ways
  6. Fit on training data just
  7. Why an unscaled input slows all down
  8. Guided experiments
  9. Where this goes wrong
  10. In one line
  11. Which nets care
  12. Scaling other kinds of input
  13. Questions people ask
  14. Recap in one screen

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.

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 gives gradients roughly four orders of magnitude more 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 each weight. And convergence takes a fraction of the steps.

This page checks standardisation (z-score), min-max normalisation. And IQR-based scaling side by side, shows when each is right. And covers the most common mistake — fitting the scaler on the full dataset before splitting. It leaks test statistics into training. The code blocks train the same network with and without scaling so the convergence gap is measured. And the visualisation above lets you switch scaling ways and watch the loss surface reshape in real time. It also covers which net families need scaling (each gradient-trained or distance-based net) and which do not (trees, forests, boosting), and explains why batch normalisation does not remove the need to scale the input layer.

Standardisation and normalisation

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

x′ = (x − μ) / σ

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

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

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

Useful when a bounded input is needed. And fragile: a single extreme outlier sets the max and squashes each ordinary value into a narrow band near zero.

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

Work the counts

Take age 30 and salary 60,000 with weights of 0.5 each. The salary term adds 30,000 to the weighted sum and the age term adds 15 — the age feature is invisible. And it would take a weight around 1000× more 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 add comparably. And both weights get gradients of a close 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 fully. And the gradient with respect to its weight is hundreds of thousands of times more than the gradient for the first.

Three things go wrong at once.

The loss surface turns a ravine. Steep in one way, nearly flat in another. A learning rate small enough to be stable in the steep way 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. There the derivative is in effect 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 nets, which check features individually and are indifferent to units, each neural network needs its inputs on a comparable scale.

The three ways

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

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

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

Reliable scaling uses the median and interquartile range. So a handful of extreme vals cannot distort the transformation.

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

Fit on training data just

This is the rule that makes the gap between an honest test 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. It means information from those sets has influenced the training pipeline. The reported score is then optimistic, sometimes much.

Two more real points. The fitted scaler must be saved and shipped with the net — a net served with unscaled inputs. Or scaled by recomputed statistics, gives 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 all down

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

example_01.pyNumPy
Output

Now run gradient descent on each variant, each at its own best learning rate. And check convergence.

example_02.pyNumPy
Output

Centring alone came out worse than raw since it changes where the surface sits without changing how elongated it is. The gradient magnitudes show why one learning rate cannot serve both features.

example_03.pyNumPy
Output

These are the real rules for choosing a scaler and avoiding the common mistakes.

example_04.pyNumPy
Output

Guided experiments

  1. Train unscaled. Set Data Normalization Way to none, put Salary Input near 200000 and Age Input near 30. And press Start Training. Training is unstable or crawls, since one input dominates each gradient.
  2. Standardise and repeat. Switch Data Normalization Way to standardisation, press Reset Weights and train again with the same inputs. Convergence is faster and far smoother.
  3. Check the scalers. Run min-max against standardisation on the same vals. Min-max compresses all 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 gives silently wrong inputs.
  • Scaling one-hot columns. They are now on a comparable scale. And standardising them destroys sparsity by making each zero non-zero.
  • Min-max with outliers. One extreme value compresses all else into a sliver of the range. Use standardisation or reliable 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 just, keep those statistics for validation, test and production. And leave one-hot columns alone.

Which nets 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 right — it makes optimisation work. A net that will not train at all often starts training the moment the inputs are scaled. It is why it is the first thing to check.

Scaling other kinds of input

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

Tabular data. Standardise numeric columns. Leave one-hot columns alone (they are now 0/1, and scaling them distorts the regularisation cost).

Text. Not applicable — tokens turn embedding lookups. And the embeddings are learned at a sensible scale.

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

Time series. Scale with statistics from the training period just — with the whole series leaks future information into the past. It 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 gets the raw input.

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

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

What about the target var? Scale it for neural network regression. And invert before reporting. Not needed for tree nets.

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 all over.

Recap in one screen

  • Networks need inputs on a comparable scale. Or the loss surface turns a ravine and activations saturate.
  • Standardise by default. Min-max for bounded inputs. Reliable scaling when outliers are present.
  • Log-transform heavily skewed features before scaling.
  • Fit the scaler on training data just. And ship it with the net.
  • Trees do not need it. Each distance-based, regularised or gradient-trained net 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 counts”?

Cheat sheet

Feature Scaling & Weight Bias

See what happens when one feature is measured in thousands and another in decimals, and how scaling rebalances the gradients.

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.