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

TensorflowPythonServer Side ProgrammingProgramming

Tensorflow can be used with the estimator to predict output on new data using the ‘predict’ method which is present in the ‘classifier’ method.

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

We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.

A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. 

TensorFlow Text contains collection of text related classes and ops that can be used with TensorFlow 2.0. The TensorFlow Text can be used to preprocess sequence modelling.

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

An Estimator is TensorFlow's high-level representation of a complete model. It is designed for easy scaling and asynchronous training.

The model is trained using iris data set. There are 4 features, and one label.

  • sepal length
  • sepal width
  • petal length
  • petal width


print(“Generating predictions from model”)
expected = ['Setosa', 'Versicolor', 'Virginica']
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):
predictions = classifier.predict(
   input_fn=lambda: input_fn(predict_x))

Code credit −


Generating predictions from model
Defining input function for prediction
It converts inputs to dataset without labels


  • The model that is trained would produce good results.
  • This can be usd to predict species of an Iris flower, based on certain unlabelled measurements.
  • The predictions are made using a single function call.
Published on 12-Feb-2021 16:19:13