Classification using Tensorflow Estimator

Shravan C
3 min readFeb 10, 2020

https://www.kdnuggets.com/2019/08/tensorflow-20.html

This article is about the classification problem on the tic-tac-toe dataset available at UCI. In reference to my previous article on classification using feature column and dataset pipeline, this is article is about using the TensorFlow estimator API. For this article, I have followed the article from the TensorFlow website. This is a easy to follow as the dataset is a little simple compared to the previous article.

The code is available in Github, also on Colab. Even in this program, compared to the previous example, four steps are involved:

Load CSV Data to Pandas Dataframe → Write a helper function to return an input function that returns a Dataset Pipeline → Feature Columns initialization for each column in the Dataframe →Define the estimator with respect to the problem that we are solving(in this case LinearClassifier)

tf.data

Recommend the previous article for the further understanding of the tf.data API.

URL = "./tic-tac-toe.data.csv"
df = pd.read_csv(URL)
print(df.head())
train, test = train_test_split(df , test_size=0.2)
train, val = train_test_split(train, test_size=0.2)
def make_input_fn(data_df, label_df, num_epochs=10, shuffle=True, batch_size=32):
def input_function():
ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))
if shuffle:
ds = ds.shuffle(1000)
ds = ds.batch(batch_size).repeat(num_epochs)
return ds
return input_function
y_train = train.pop('class')
train_input_fn = make_input_fn(train, y_train)
y_eval = val.pop('class')
eval_input_fn = make_input_fn(val, y_eval, num_epochs=1, shuffle=False)

tf.feature_column

As this dataset contains only categorical features, it is very simple in this program.

feature_columns = []
for header in ['t_l_s', 't_m_s', 't_r_s', 'm_l_s', 'm_m_s', 'm_r_s', 'b_l_s', 'b_m_s', 'b_r_S']:
fc = feature_column.categorical_column_with_vocabulary_list(header, df[header].unique())
feature_columns.append(feature_column.indicator_column(fc))

tf.estimator

Using estimator API is a bypass method for you to build, train and evaluate your model. What estimator does is, it just needs to know what type of classification or the machine learning algorithm needs to be performed on the dataset. And then provide a function that will fetch the input data in the fashion you want. Here in our program, we are specifying LineraClassifer as the estimator and then passing feature_columns to it and then it is trained with the data pipeline which input_fn will return.

The estimator is responsible to perform all these four functions:

training →evaluation →prediction → export for serving.

It provides an abstraction for the model. Also, it supports for us to design a model with Keras and then encapsulate it with the estimator API. The one advantage that we get out of this is the distributed training support.

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)

As its evident from the above code so much of the code is reduced. It is a great way to start any machine learning model with an estimator and set this as a baseline for further improvement.

Yeah and that's it for this article. So far enjoying working in the Tensorflow framework. Further need to find more ways to do EDA and implement the same in the Tensorflow and many things to explore like TensorBoard and GCP. Cheers, enjoy coding!!!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Shravan C
Shravan C

Written by Shravan C

Software Engineer | Machine Learning Enthusiast | Super interested in Deep Learning with Tensorflow | GCP

No responses yet

Write a response