How can Tensorflow be used with premade estimator to download the Iris dataset?


Tensorflow can be used with premade estimator to download the iris dataset using the ‘get_file’ method present in Keras package. A Google API holds the iris dataset, which can be passed as parameter to the ‘get_file’ 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.

Let us understand how Estimators can be used.

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

Based on this information, some constants can be defined that help in parsing data.

Example

import tensorflow as tf
import pandas as pd
print("Column names defined")
CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']
SPECIES = ['Setosa', 'Versicolor', 'Virginica']
print("Iris training data is being downloaded")
train_path = tf.keras.utils.get_file(
"iris_training.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv")
print("Iris test data is being downloaded")
test_path = tf.keras.utils.get_file(
"iris_test.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv")
train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0)
test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0)
print("Sample data is being displayed")
train.head()

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

Output

Column names defined
Iris training data is being downloaded
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv
8192/2194 [================================================================================================================] - 0s 0us/step
Iris test data is being downloaded
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv
8192/573 [============================================================================================================================================================================================================================================================================================================================================================================================================================================] - 0s 0us/step
Sample data is being displayed
SepalLength SepalWidth PetalLength PetalWidth Species
0 6.4 2.8 5.6 2.2 2
1 5.0 2.3 3.3 1.0 1
2 4.9 2.5 4.5 1.7 2
3 4.9 3.1 1.5 0.1 0
4 5.7 3.8 1.7 0.3 0

Explanation

  • Column names are defined.
  • The data is downloaded.
  • Some sample data is displayed on the console.

Updated on: 22-Feb-2021

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements