Simple Keras neural network isn't learning
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
When a simple Keras model is not learning, the cause is usually basic rather than exotic. Most failures come from mismatched output layers and loss functions, unscaled inputs, label problems, or a training setup that has never been validated on a tiny overfit test.
Start With a Known-Good Baseline
Before changing the architecture, make sure the overall training loop is sane. A minimal binary classification model should look something like this:
If this kind of setup works on synthetic data but your real problem does not, the issue is likely in the data or problem formulation rather than in Keras itself.
Check Output Layer and Loss Compatibility
A very common mistake is mixing the wrong output activation with the wrong loss.
Examples:
- Binary classification: one output unit plus
sigmoidplusbinary_crossentropy. - Multi-class single-label classification:
softmaxplussparse_categorical_crossentropyorcategorical_crossentropy. - Regression: usually no output activation or a task-specific one, plus a regression loss such as
mse.
A mismatch here can make learning look broken even though the optimizer is running exactly as asked.
Scale Inputs Before Training
Simple dense networks are sensitive to input scale. If one feature ranges from 0 to 1 and another ranges from 0 to 100000, optimization may become unstable or slow.
A quick normalization step is often enough:
If the network only starts learning after scaling, that is not a surprise. It is normal.
Verify the Labels, Not Just the Features
If labels are wrong, inconsistent, or misaligned with the feature rows, the network cannot learn the intended mapping. Check:
- Are labels shuffled independently from features?
- Are class IDs in the expected range?
- Are there unexpected
NaNvalues? - Does the target actually depend on the provided features?
A model that stays near chance accuracy is often reflecting a data-label problem instead of an optimizer problem.
Run the Tiny Overfit Test
One of the best diagnostics is to train on a very small subset, such as 20 samples, and see whether the network can nearly memorize it.
If the model cannot overfit a tiny clean subset, something fundamental is wrong:
- Loss and output mismatch.
- Broken labels.
- Learning rate issue.
- Data preprocessing bug.
This test is often more informative than adding layers or training longer.
Learning Rate Still Matters
A learning rate that is too high can make the loss bounce or diverge. Too low can make the network appear frozen.
The default Adam settings are a good starting point, but if training is flat, try a modest sweep instead of guessing forever:
- '
1e-2' - '
1e-3' - '
1e-4'
Do not change ten things at once. Change one variable and observe the loss curve.
Common Pitfalls
- Using an output layer and loss function that do not match the task.
- Feeding unscaled numeric inputs into a dense network.
- Training on misaligned or low-quality labels.
- Assuming the model is the problem before running a tiny overfit test.
- Changing architecture repeatedly without checking the basics first.
Summary
- Most simple Keras learning failures come from setup errors, not from the framework.
- Check output activation and loss compatibility first.
- Scale inputs and verify label integrity.
- Use a tiny overfit test to prove the training loop can learn at all.
- Tune learning rate only after the data and task formulation make sense.
Related reading
- Simple multi layer neural network implementation
- Simple Multilayer Perceptron model does not converge in TensorFlow
- Sinusoidal embedding - Attention is all you need
- Sinusoidal embedding - Attention is all you need
- Simple way to visualize a TensorFlow graph in Jupyter?
- Simpler way to avoid the UserWarning Converting sparse IndexedSlices
- Simple Linear Regression in Python
- Simple Machine learning model training returning Nan
.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.