Keras
CNN
model training
loss stagnation
machine learning troubleshooting

Training and `Loss` not changing in Keras CNN model

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

When both training loss and validation loss stay flat in a Keras CNN, the model is usually not learning anything meaningful. The cause is rarely "Keras is broken" and much more often a mismatch in data, labels, model configuration, or optimization settings.

Start with the Simplest Diagnostic Question

Ask whether the model can overfit a tiny sample. If a CNN cannot drive the loss down on a very small subset of the training set, the training pipeline itself is probably wrong.

That makes debugging easier because you stop thinking about generalization and focus on the mechanics of learning.

python
1small_x = x_train[:32]
2small_y = y_train[:32]
3
4history = model.fit(small_x, small_y, epochs=50, verbose=0)
5print(history.history["loss"][-1])

If the loss is still flat here, investigate the model, labels, preprocessing, and optimizer before tuning architecture depth or regularization.

Check Label and Loss Compatibility

A very common mistake is pairing the wrong output layer with the wrong loss function. For example:

  • binary classification usually needs one output unit with sigmoid plus binary_crossentropy
  • multi-class single-label classification usually needs softmax plus categorical_crossentropy or sparse_categorical_crossentropy

Here is a correct binary classifier skeleton:

python
1from tensorflow import keras
2from tensorflow.keras import layers
3
4model = keras.Sequential([
5    layers.Conv2D(16, (3, 3), activation="relu", input_shape=(64, 64, 3)),
6    layers.MaxPooling2D(),
7    layers.Conv2D(32, (3, 3), activation="relu"),
8    layers.MaxPooling2D(),
9    layers.Flatten(),
10    layers.Dense(64, activation="relu"),
11    layers.Dense(1, activation="sigmoid"),
12])
13
14model.compile(
15    optimizer="adam",
16    loss="binary_crossentropy",
17    metrics=["accuracy"],
18)

If the labels are one-hot encoded but you compile with sparse_categorical_crossentropy, or vice versa, training can appear stuck or behave very strangely.

Verify the Input Data Pipeline

Loss can stay flat because the network is effectively seeing useless input. Typical causes include:

  • images are all zeros or all the same value after preprocessing
  • labels are shuffled incorrectly relative to images
  • normalization is inconsistent between train and validation
  • generators are yielding the wrong shape or wrong target arrays

A quick inspection is worth more than guessing:

python
print(x_train.shape, y_train.shape)
print(x_train.min(), x_train.max())
print(y_train[:10])

For image data, it is also worth plotting a few examples with their labels. Many "training is broken" incidents turn out to be bad preprocessing or mislabeled batches.

Learning Rate and Frozen Parameters

If the learning rate is too small, the optimizer may barely update weights. If it is too large, training may bounce around without improving. Another issue is accidentally freezing layers so there are few or no trainable parameters.

python
model.summary()
print("Trainable weights:", len(model.trainable_weights))

If the number of trainable weights is unexpectedly low, you may have set layer.trainable = False somewhere and forgotten about it.

You can also try a different optimizer or a different learning rate explicitly:

python
1from tensorflow.keras.optimizers import Adam
2
3model.compile(
4    optimizer=Adam(learning_rate=1e-3),
5    loss="binary_crossentropy",
6    metrics=["accuracy"],
7)

Data Scale and Activation Problems

CNNs often learn better when image inputs are scaled into a reasonable range such as [0, 1].

python
x_train = x_train.astype("float32") / 255.0
x_val = x_val.astype("float32") / 255.0

Bad scaling alone may not make the loss perfectly flat, but it can slow training so much that it looks stagnant.

Architecture choices can also cause trouble. If activations die early or the network is too heavily regularized with dropout, batch normalization, or weight decay in a tiny model, the optimizer may have little signal to work with.

A Practical Debugging Checklist

A useful order of operations is:

  1. overfit a tiny subset
  2. verify shapes and label format
  3. inspect a few training examples visually
  4. confirm the loss matches the output layer
  5. check trainable weights and optimizer settings
  6. simplify the model until training starts moving

When debugging, simpler is better. A small CNN that learns is more informative than a large one that fails for six possible reasons at once.

Common Pitfalls

  • Using the wrong loss function for the output layer and label encoding.
  • Feeding incorrectly normalized or corrupted input data.
  • Accidentally misaligning labels and images in generators or preprocessing code.
  • Freezing layers unintentionally and leaving almost nothing trainable.
  • Jumping straight to architecture changes before testing whether the model can overfit a tiny sample.

Summary

  • Flat loss usually means a training-pipeline or configuration problem, not just a hard dataset.
  • First test whether the model can overfit a very small batch.
  • Make sure the output layer, loss function, and label encoding agree.
  • Inspect the real input data and confirm the model still has trainable weights.
  • Simplify the problem until learning starts, then add complexity back carefully.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.