Using Tensorflow estimator API to not write Boilerplate codes

What we are doing here : predicting price of taxifare dataset. We are using tensorflow and the high level estimator API . As we know machine learning is a very buzz words now a days and we strongly believe that everyone should needs to know the nuts and and bolts of machine learning so without further ado i am writing the code that will give you the freedom of doing in memory machine learning with tensorflow with as less as code possible.

What is the Tensorflow Estimator API? - Databricks

This is the overview of tensorflow API we are using estimator API to get you started and running .

you need numpy , pandas , tensorflow and shutil package use one colab to import the packages.

import tensorflow as tf
import pandas as pd
import numpy as np
import shutil

print(tf.__version__)
1.13.1

Next importing the data as pandas dataframe

# In CSV, label is the first column, after the features, followed by the key
CSV_COLUMNS = ['fare_amount', 'pickuplon','pickuplat','dropofflon','dropofflat','passengers', 'key']
FEATURES = CSV_COLUMNS[1:len(CSV_COLUMNS) - 1]
LABEL = CSV_COLUMNS[0]

df_train = pd.read_csv('./taxi-train.csv', header = None, names = CSV_COLUMNS)
df_valid = pd.read_csv('./taxi-valid.csv', header = None, names = CSV_COLUMNS)
df_test = pd.read_csv('./taxi-test.csv', header = None, names = CSV_COLUMNS)

now define the training input and validation input function unlike pandas we provide function into estimator api that converts our raw file into tensors.

def make_train_input_fn(df, num_epochs):
  return tf.estimator.inputs.pandas_input_fn(
    x = df,
    y = df[LABEL],
    batch_size = 128,
    num_epochs = num_epochs,
    shuffle = True,
    queue_capacity = 1000
  )

we are using tf.estimator.inputs.pandas_input_fn() function here which handles our input query.

As we only have numeric columns in the example we need only numeric_column() methods here.

tf.feature_column.numeric_column() will take care of your input_columns next we came to provide the model tf.estimator.LinearRegressor() is the estimator version of tensorflow we are using to use DNNRegressor just change it

define a model as

model = tf.estimator.LinearRegressor(
      feature_columns = make_feature_cols(), model_dir = OUTDIR)

provides features and output

model.train() fn do the training

model.evaluate() will evaluate your data

model.predict() will predict your data

the github code has dependency issues.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s