How to train a Model on TensorFlow
Hey Everyone,
In this article, we will understand, How to train a Machine Learning model using TensorFlow deep learning library 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 which 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 it’s 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 the following tutorial we are using a .csv file.
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 will be looking at the Behavior of the Data by visualizing it, Replace the missing values with desired central tendency, discard the data which are irrelevant or incomplete.
Shuffling/ Re indexing the Data:
This is and 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 build a Model using TensorFlow by Defining its Loss Function and Metrics. And also 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 by looking at its Accuracy, 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 2.0
In case if 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
Lets Hop into the Coding part
First of all, import the Required 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/mledudatasets/california_housing_train.csv") test_df = pd.read_csv("https://download.mlcc.google.com/mledudatasets/california_housing_test.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:
Normalized Value =
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 # Zscore = 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 train_df.head()
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:
Sum of Normalized value will be approximately 0.
Let us See how the Normalized Train Data looks like.
# lets look at the Normalised dataset: train_df_norm.head()
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 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) train_df_norm.head()
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))
Lets 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() model.add(feature_layer) model.add(tf.keras.layers.Dense(units=1, input_shape=(1,), 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:
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 lets 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 the performance.
So at the end, we have successfully train the model on TensorFlow in Python.
For TensorFlow Documentation: Click Here
Thank You for reaching till here!
Nice! Very useful for beginers, learners and especially sudents of Engineering Streams!
Actually useful coz I was not aware of this module in Python… Thanks for introducing this new topic.