Skip to content

Keras Model.predict_classes Deprecation and Alternatives

Version Compatibility

This article addresses changes introduced in TensorFlow 2.6+ where model.predict_classes() was removed. The solutions apply to TensorFlow versions 2.6 and above.

Problem: 'Sequential' object has no attribute 'predict_classes'

Many TensorFlow/Keras users encountered an AttributeError: 'Sequential' object has no attribute 'predict_classes' when updating their code or environments. This error occurs because the predict_classes() method was deprecated in earlier TensorFlow versions and completely removed starting with TensorFlow 2.6.

The method was commonly used for classification tasks to obtain class predictions directly from trained models:

python
# This will now raise an AttributeError in TF 2.6+
yhat_classes = model.predict_classes(X_test)

Solutions for Class Predictions

The appropriate replacement depends on whether you're working with binary or multi-class classification.

Multi-class Classification (Softmax Activation)

For models with softmax activation in the output layer (typical for multi-class problems):

python
# Get probability predictions
predictions = model.predict(X_test)

# Convert probabilities to class indices
classes = np.argmax(predictions, axis=-1)

Binary Classification (Sigmoid Activation)

For binary classification models with sigmoid activation:

python
# Method 1: Using threshold comparison
predictions = (model.predict(X_test) > 0.5).astype("int32")

# Method 2: Alternative syntax
predictions = np.round(model.predict(X_test)).astype(int)

Complete Working Example

python
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import numpy as np

# Create and compile model
model = Sequential()
model.add(Dense(24, input_dim=13, activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(6, activation='softmax'))  # Multi-class classification
model.compile(loss='categorical_crossentropy', 
              optimizer='adam', 
              metrics=['accuracy'])

# Train model
history = model.fit(X_train, y_train, 
                   batch_size=256, 
                   epochs=10, 
                   verbose=2, 
                   validation_split=0.2)

# Evaluate model
score, acc = model.evaluate(X_test, y_test, verbose=2, batch_size=256)
print('Test accuracy:', acc)

# Get class predictions (replaces predict_classes)
y_pred_probs = model.predict(X_test)
y_pred_classes = np.argmax(y_pred_probs, axis=-1)

Migration Strategy

When updating code that used predict_classes():

  1. Determine your model type (binary or multi-class)
  2. Replace with the appropriate alternative
  3. Test thoroughly to ensure identical behavior

Why Was predict_classes Removed?

The predict_classes() method was deprecated to:

  • Simplify the API - Reduce redundant methods
  • Improve consistency - Standardize on predict() as the primary method
  • Enhance flexibility - Allow users to implement custom post-processing logic

Historical Context

  • TensorFlow 2.5: Warning issued about upcoming deprecation
  • TensorFlow 2.6: Method completely removed
  • The warning message clearly suggested the appropriate replacements

Additional Considerations

For Batch Processing

When working with large datasets, you might want to process predictions in batches:

python
def predict_classes_batch(model, X_data, batch_size=256):
    """
    Replacement for predict_classes with batch processing
    """
    predictions = model.predict(X_data, batch_size=batch_size)
    
    if model.output_shape[-1] == 1:  # Binary classification
        return (predictions > 0.5).astype("int32")
    else:  # Multi-class classification
        return np.argmax(predictions, axis=-1)

# Usage
y_pred = predict_classes_batch(model, X_test)

Performance Metrics Calculation

To calculate F1 score, precision, and recall using the new approach:

python
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score

# Get predictions
y_pred_probs = model.predict(X_test)
y_pred = np.argmax(y_pred_probs, axis=-1)

# Convert one-hot encoded y_test to class indices if necessary
if len(y_test.shape) > 1 and y_test.shape[1] > 1:
    y_true = np.argmax(y_test, axis=-1)
else:
    y_true = y_test

# Calculate metrics
accuracy = accuracy_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred, average='weighted')
precision = precision_score(y_true, y_pred, average='weighted')
recall = recall_score(y_true, y_pred, average='weighted')

print(f"Accuracy: {accuracy:.4f}")
print(f"F1 Score: {f1:.4f}")
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")

By adopting these updated approaches, you can continue to build and evaluate your Keras models effectively while maintaining compatibility with current and future TensorFlow versions.