TensorFlow
Keras
predict_classes alternative
machine learning
deep learning

model.predict_classes is deprecated - What to use instead?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

model.predict_classes() was removed because it bundled prediction and label-decoding into one opinionated helper. Modern Keras expects you to call model.predict() and then decide how to turn raw outputs into labels based on the model's output layer and your task.

Why predict_classes() Went Away

The old helper looked convenient, but it quietly assumed too much:

  • binary classification should use a threshold of 0.5
  • multiclass classification should use argmax
  • the caller always wants class labels rather than scores

Those assumptions are not always correct. Some applications need:

  • custom thresholds
  • top-k predictions
  • calibrated probabilities
  • multilabel outputs instead of single-class outputs

So Keras removed the convenience method and left the interpretation step in user code, where it can be reviewed and customized.

The Modern Replacement Pattern

The replacement is always some version of:

  1. run model.predict(...)
  2. inspect the output shape and activation
  3. convert outputs into labels explicitly

For binary classification with a sigmoid output:

python
1import numpy as np
2
3probs = model.predict(x_test, verbose=0).reshape(-1)
4preds = (probs >= 0.5).astype("int32")
5
6print(preds[:10])

For multiclass classification with a softmax output:

python
1import numpy as np
2
3probs = model.predict(x_test, verbose=0)
4preds = np.argmax(probs, axis=1)
5
6print(preds[:10])

Those two patterns cover most migrations from predict_classes().

Binary, Multiclass, and Multilabel Are Different

The correct replacement depends on model semantics, not just on Keras version.

Binary classifier

Typical shape:

  • '(batch, 1) or (batch,)'

Typical conversion:

python
preds = (probs >= 0.5).astype("int32")

Multiclass classifier

Typical shape:

  • '(batch, num_classes)'

Typical conversion:

python
preds = np.argmax(probs, axis=1)

Multilabel classifier

Here argmax is wrong, because more than one label can be true.

python
preds = (probs >= 0.5).astype("int32")

but applied per output unit, not as a single winner-takes-all decision.

That is exactly why the old helper became too limited.

Keep the Probability Output

A major advantage of the new style is that you still have the scores after prediction.

python
probs = model.predict(x_test, verbose=0)

From there you can:

  • compute confidence
  • inspect uncertainty
  • tune thresholds on validation data
  • produce top-k candidates

Example top-3 classes:

python
top3 = np.argsort(probs, axis=1)[:, -3:][:, ::-1]
print(top3[:5])

That was awkward with predict_classes() because the probabilities were already discarded.

Migrating Legacy Evaluation Code

A lot of older code looked like this:

python
# old code
# y_pred = model.predict_classes(x_test)

Modern equivalent for a softmax classifier:

python
1import numpy as np
2from sklearn.metrics import accuracy_score
3
4probs = model.predict(x_test, verbose=0)
5y_pred = np.argmax(probs, axis=1)
6
7print(accuracy_score(y_true, y_pred))

If y_true is one-hot encoded, decode it first:

python
y_true_labels = np.argmax(y_true, axis=1)

Always make sure your label format and prediction format match before computing metrics.

Write a Small Helper if You Miss the Old Convenience

If you want the older convenience back, create your own utility rather than relying on a removed framework API.

python
1import numpy as np
2
3def predict_labels(model, x, threshold=0.5):
4    outputs = model.predict(x, verbose=0)
5    if outputs.ndim == 2 and outputs.shape[1] > 1:
6        return np.argmax(outputs, axis=1)
7    return (outputs.reshape(-1) >= threshold).astype("int32")

This keeps your project code short while preserving explicit rules.

The important part is that the decoding behavior now belongs to your codebase, not to a hidden default inside Keras.

Common Pitfalls

  • Replacing predict_classes() blindly without checking whether the model is binary, multiclass, or multilabel.
  • Applying argmax to a sigmoid output.
  • Hardcoding a 0.5 threshold when the application needs a tuned decision threshold.
  • Comparing one-hot targets against integer predictions without decoding first.
  • Throwing away probabilities too early when later evaluation still needs them.

Summary

  • 'predict_classes() was removed because it hid task-specific label-decoding logic.'
  • Use model.predict() and decode outputs explicitly.
  • Threshold sigmoid outputs for binary or multilabel tasks.
  • Use argmax for softmax multiclass outputs.
  • Keep probability outputs available when you need confidence, calibration, or top-k behavior.

Course illustration
Course illustration

All Rights Reserved.