How can Tensorflow be used to instantiate an estimator using Python?

PythonServer Side ProgrammingProgramming

An estimator can be instantiated using Tensorflow by using the ‘DNNClassifier’ method that is present in ‘estimator’ class of Tensorflow library.

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.


print("Build a DNN that has 2 hidden layers with 30 and 10 hidden nodes each")
classifier = tf.estimator.DNNClassifier(
   hidden_units=[30, 10],

Code credit −


Build a DNN that has 2 hidden layers with 30 and 10 hidden nodes each
INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpdh8866zb
INFO:tensorflow:Using config: {'_model_dir': '/tmp/tmpdh8866zb', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
   rewrite_options {
      meta_optimizer_iterations: ONE
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_checkpoint_save_graph_def': True, '_service': None, '_cluster_spec': ClusterSpec({}), '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}


  • The Iris problem is considered as classification problem.
  • Tensorflow comes with many pre-made classifier Estimators, which includes −
    • tf.estimator.DNNClassifier for deep models which perform multi-class classification.
    • tf.estimator.DNNLinearCombinedClassifier for wide & deep models.
    • tf.estimator.LinearClassifier for classifiers based on linear models.
  • For Iris problem, we use tf.estimator.
Published on 12-Feb-2021 11:44:06