# How to train a Model on TensorFlow

Hey Everyone,

In this tutorial, we will understand, how to train a Machine Learning model using the TensorFlow deep learning module in Python. Training a model is one of the most crucial elements for building a reliable Machine learning model.

### What is a Model?

An ML Model is a complex structure that performs mathematical calculations to predict an Outcome. Ever wondered what will your future be like based on your current Situation? Even Models does the Same.
Depending upon its previous data the Model predicts the outcome.

### This is the Chronology on how we will be training the Model on TensorFlow

Step 1. Understanding the Problem Statement.
Step 2. Data Collection.
Step 3. Data Cleaning.
Step 4. Shuffle/Re index the Data (Optional).
Step 5: Build, Train the Model and Evaluate the Model.

### Understanding the Problem Statement

Every Business has different requirements. Understanding the Business and defining its problem will make us understand and develop strategies to tackle a particular business problem.
In this Particular tutorial we will be providing Solution to a Real Estate.

### Data Collection

Collection of the data which are relevant to the Business problem statement. It can be a Text file, Image file, Video file, .csv file etc. In this tutorial we are using a .csv file. The CSV file contains the required data.

### Data Cleaning

A sad news for all the Data Scientist, Machine Learning and AI Aspirant. Unfortunately the Data doesn’t come to us in a desired form. We are in the Activity where you might be spending the most time. It’s “Swach Data Abhiyaan”.
In this process, we have to look at the Behavior of the Data by visualizing it, Replacing the missing values with desired central tendency, discard the data which are irrelevant or incomplete.

### Shuffling/ Re indexing the Data

This is optional yet very much recommended. We don’t want to train our model on Summer Data and expect to get actionable predictions on winter data.

### Build, Train the Model and Evaluate the Model

Now we are ready to build a Model using TensorFlow by Defining its Loss Function and Metrics. Wi are also going to define its Hyper parameters (i.e. for High Performance of ML Model).

It might seem easy just to train the model on the training data set and Evaluate it by looking at its Accuracy. Looks quiet easy, right? Let us Explore and Understand.

Problem Statement:
To predict the median house value in California,USA and classify them.

We need of have the Following module installed into our IDE:

• NumPy.
• SciPy.
• Pandas.
• Matplotlib.
• TensorFlow

In case the modules are not installed into your IDE you may type the following into you Virtual Environment:

• `pip install numpy`
• `pip install scipy`
• `pip install matplotlib`
• `pip install tensorflow`

## Let’s Hop into the Coding part

Now we are going to see the Python code step by step.

First of all, import the required Python modules:

```import numpy as np
import pandas as pd
from scipy.stats import  norm
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow import feature_column
import matplotlib.pyplot as plt

print("All the Modules have been Imported")```

Output:

`All the Modules have been Imported.`

We will now import the Train and the Test Data set and reshuffle the Train Data set:

```train_df = pd.read_csv("https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv")
train_df = train_df.reindex(np.random.permutation(train_df.index))
print("Import the train and test Dataset and shuffle the dataset to prevent model from Overfitting")```

Output:

```Import the train and test Dataset and shuffle the dataset to prevent model from Overfitting

```

For Binary Classification we will have to normalize the data for that we have to use the following formula:

## True Value – Mean/ Standard Deviation(S.D.)

```train_df_mean = train_df.mean()
train_df_std = train_df.std()
train_df_norm = (train_df - train_df_mean)/train_df_std
# Z-score = raw value - mean/ Standard Deviation.
print("The Raw value with its label has been of train Dataset has been Normalised")

test_df_mean = test_df.mean()
test_df_std = test_df.std()
test_df_norm = (test_df - test_df_mean)/test_df_std
print("The Raw value with its label has been of test Dataset has been Normalised")```

Output:

```The Raw value with its label has been of train Dataset has been Normalised
The Raw value with its label has been of test Dataset has been Normalised```

Wanna have look at the train data:

```#Look at the Train Dataset

Output:

If you want to look at a Normalized data(not the Normalized data from the data set) visualization you may run the Following code:

```domain = np.linspace(-2,2,1000)
plt.plot(domain, norm.pdf(domain,0,1))
plt.title("Standard Normal")
plt.xlabel("value")
plt.ylabel("Density")
num = str(round(sum(domain)))
print("Sum of Normalization is equal to "+num)
plt.show()```

Output:

The Sum of the Normalized value will be approximately 0.

Let us see how the Normalized Train Data looks like with the code given below:

```# lets look at the Normalised dataset:

Output:

For Binary Classification we will convert all the `median_house_value` from the train and test data set(i.e. Not Normalized) greater than 265000 to 1 and `median_house_value` less than 265000 to 0 and append into the column `median_house_value_is_high`  and add the column to the Normalized Train data set.
We have defined 265000 as the threshold as it is the 75% of the `median_house_value`.
You may choose your desired Threshold.

```#This is the 75% percentile for the median_house_value. And can be considered for #Classification.
threshold = 265000
train_df_norm["median_house_value_is_high"] = (train_df["median_house_value"]>threshold).astype(float)
test_df_norm["median_house_value_is_high"] = (test_df["median_house_value"]>threshold).astype(float)

Output:

Now let us Define the Feature columns and convert it into Numeric and create feature layer for building a model:

```feature_columns = []

median_income = tf.feature_column.numeric_column("median_income")
feature_columns.append(median_income)

tr = tf.feature_column.numeric_column("total_rooms")
feature_columns.append(tr)

feature_layer = layers.DenseFeatures(feature_columns)

feature_layer(dict(train_df_norm))```

Let’s build the Model by defining its function:

```#Function to Create a Model
def create_model(my_learning_rate, feature_layer, my_metrics):

model = tf.keras.models.Sequential()

activation=tf.sigmoid),)

model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=my_learning_rate),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=my_metrics)

return model

print("Model has been created")```

Output:

`Model has been created`

Lets define a function to Train the Model:

```#Function to train the model
def train_model(model, dataset, epochs, label_name,
batch_size=None, shuffle=True):

features = {name:np.array(value) for name, value in dataset.items()}
label = np.array(features.pop(label_name))
history = model.fit(x=features, y=label, batch_size=batch_size,
epochs=epochs, shuffle=shuffle)

epochs = history.epoch

hist = pd.DataFrame(history.history)

return epochs, hist

print("Function to train the Model has been Defined")```

Output:

`Function to train the Model has been Defined`

To Understand the Behavior of Accuracy in different Epochs we will define a Function for it. Below is the given code for performing this task:

```def plot_curve(epochs, hist, list_of_metrics):

plt.figure()
plt.xlabel("Epoch")
plt.ylabel("Value")

for m in list_of_metrics:
x = hist[m]
plt.plot(epochs[1:], x[1:], label=m)

plt.legend()

print("plot_curve function has been created")```

Output:

`plot_curve function has been created`

Now let’s Define the Hyper Parameters to build the Model, train the Model and plot the Performance:

```#These are Hyperparameter to build and Train the model and plot it Metrics:
learning_rate = 0.001
epochs = 16
batch_size = 10
label_name = "median_house_value_is_high"
classification_threshold = 0.35

METRICS = [
tf.keras.metrics.BinaryAccuracy(name='accuracy',
threshold=classification_threshold),
]

my_model = create_model(learning_rate, feature_layer, METRICS)

epochs, hist = train_model(my_model, train_df_norm, epochs,
label_name, batch_size)

# Plot a graph of the metric(s) vs. epochs.
list_of_metrics_to_plot = ['accuracy']

plot_curve(epochs, hist, list_of_metrics_to_plot)```

Output:

In the following output we can say that the accuracy of the model to predict house values and classify as 1 and 0 is 80%.
The Goal is to have high Accuracy and low loss.
I strongly Encourage you to play with the `learning_rate` and epochs value to examine it changes on performance.

So at the end, we have successfully train the model on TensorFlow in Python.