Correct way of normalizing and scaling the MNIST dataset
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
MNIST images are small grayscale digits with pixel values from 0 to 255. Preprocessing them is simple, but there is still confusion about whether you should only scale, fully standardize, or apply both. The correct answer depends on the model, but for most modern workflows the safe baseline is to convert to floating point and scale to 0.0 through 1.0.
Start with Simple Scaling
Each MNIST image is 28 x 28, stored as unsigned bytes. Neural networks train more smoothly when the inputs are on a smaller numeric range, so the usual first step is:
That alone is enough for many Keras and PyTorch examples. It keeps the relative brightness structure intact and avoids large input magnitudes.
Scaling Versus Standardization
These terms are often mixed together.
Scaling means mapping pixel values into a smaller fixed range, usually 0 through 1.
Standardization means subtracting a mean and dividing by a standard deviation so the resulting distribution is centered near zero with unit variance.
For MNIST, simple scaling is often sufficient, especially for convolutional models and small multilayer perceptrons. Standardization can still help, but it is not mandatory in the same way it often is for tabular features with very different units.
A Good Keras Pipeline
Here is a minimal TensorFlow example that uses the common baseline preprocessing.
This is a valid and standard way to preprocess MNIST.
When Mean and Standard Deviation Normalization Helps
Some workflows standardize MNIST using dataset statistics. In PyTorch, a common pair is mean 0.1307 and standard deviation 0.3081.
ToTensor() already scales from 0 through 255 into 0.0 through 1.0. Normalize then standardizes using the supplied statistics.
This is useful if you want inputs centered around zero or you are following a reference training setup that assumes those values.
What You Usually Should Not Do
Do not apply random scaling formulas without understanding them. For example, dividing by 256 instead of 255 is slightly wrong and has no advantage.
Do not compute separate normalization statistics for each image unless you explicitly want per-image normalization. That changes the task because each digit image gets rescaled independently, which can distort intensity information.
Also avoid fitting normalization statistics on the test set separately if your goal is a realistic evaluation pipeline. In general, compute any learned preprocessing on the training set and reuse it for validation and test data.
CNN Input Shape and Data Type
Many bugs come from shape handling rather than normalization itself. A dense model may accept 28 x 28 and flatten internally, but a convolutional model usually expects a channel dimension.
The data should also be floating point before division. If a framework uses integer division rules or the dtype remains integer too long, you can silently get incorrect results.
Common Pitfalls
A common mistake is saying you are normalizing when you are only scaling. The code may still be fine, but the terminology becomes misleading.
Another mistake is stacking redundant preprocessing steps. If a library transform already scales to 0 through 1, do not divide by 255 again.
Finally, keep training and inference consistent. If you train on scaled inputs and later feed raw byte images into the model, performance will collapse even though the model itself is unchanged.
Summary
- For most MNIST models, converting to float and dividing by
255.0is the correct baseline. - Standardization with dataset mean and standard deviation is optional, not mandatory.
- Use framework helpers carefully so you do not scale the data twice.
- Keep preprocessing consistent between training, validation, and inference.
- Watch tensor shape and dtype, because many MNIST issues are shape bugs disguised as preprocessing problems.
Related reading
- Correlated features and classification accuracy
- Correlation among Hyperparameters of Classifiers
- Cost and activation functions for multiple independent labels
- Cost function in logistic regression gives NaN as a result
- Cost Function, Linear Regression, trying to avoid hard coding theta. Octave.
- Couch DB cluster immediate read not available
- Could Keras prefetch data like tensorflow Dataset?
- Could not convert string to float error from the Titanic competition
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.