How can Tensorflow be used with Estimator to make predictions from trained model?

TensorFlow can be used with Estimators to make predictions on new data using the predict() method. This method takes unlabeled data and returns predicted outputs based on the trained model.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

An Estimator is TensorFlow's high-level representation of a complete model, designed for easy scaling and asynchronous training. We can use Convolutional Neural Networks to build learning models and preprocess sequence modeling with TensorFlow Text.

We are using Google Colaboratory to run the code below. Google Colab helps run Python code over the browser with zero configuration and free GPU access.

Iris Dataset Features

The model is trained using the iris dataset with 4 features and one label:

  • Sepal length
  • Sepal width
  • Petal length
  • Petal width

Making Predictions with Estimator

Here's how to use a trained classifier to make predictions on new data ?

import tensorflow as tf

print("Generating predictions from model")
expected = ['Setosa', 'Versicolor', 'Virginica']

# New data for prediction
predict_x = {
    'SepalLength': [5.1, 5.9, 6.9],
    'SepalWidth': [3.3, 3.0, 3.1],
    'PetalLength': [1.7, 4.2, 5.4],
    'PetalWidth': [0.5, 1.5, 2.1],
}

print("Defining input function for prediction")
print("It converts inputs to dataset without labels")

def input_fn(features, batch_size=256):
    return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)

# Make predictions (assuming classifier is already trained)
# predictions = classifier.predict(input_fn=lambda: input_fn(predict_x))

print("Prediction setup complete")
Generating predictions from model
Defining input function for prediction
It converts inputs to dataset without labels
Prediction setup complete

How the Prediction Process Works

The prediction process involves these key steps:

  • Data Preparation: Format new data in the same structure as training data
  • Input Function: Convert data into a TensorFlow Dataset without labels
  • Prediction Call: Use classifier.predict() to get predictions
  • Result Processing: Extract predicted classes from the generator returned

Processing Prediction Results

The predict() method returns a generator of prediction dictionaries ?

# Example of processing predictions
for pred_dict in predictions:
    class_id = pred_dict['class_ids'][0]
    probability = pred_dict['probabilities'][class_id]
    predicted_class = expected[class_id]
    
    print(f'Prediction: {predicted_class} ({probability:.1%})')

Code credit − https://www.tensorflow.org/tutorials/estimator/premade#first_things_first

Key Points

  • The trained model produces predictions based on unlabeled measurements
  • Predictions can classify Iris flower species from feature measurements
  • The process requires a single function call to predict()
  • Input data must match the training data structure

Conclusion

TensorFlow Estimators provide a simple way to make predictions using the predict() method. The process involves preparing unlabeled data, creating an input function, and processing the returned prediction generator.

Updated on: 2026-03-25T16:37:52+05:30

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements