Train and evaluate TensorFlow models in Python

In this tutorial, we will focus on how to train and evaluate a TensorFlow model using Python.

We need to train the model after performing all the preprocessing steps on the datasets(including splitting data into training and testing set).

Learn the basics of TensorFlow.

Training a model in TensorFlow:

Create a placeholder for input and output:

X = tf.placeholder(datatype,shape) ....(for input)

y = tf.placeholder(datatype,shape) .....(for output)

Placeholders are empty when we initiate. They get values only when the session runs.

Create a hidden layer :

w=tf.Variable(tf.zeros(shape)) ......(weight)

b=tf.Variable(tf.zeros(shape)) ......(bias)

Variables can modify the values during the computation. 

Perform the linear operation:

y_in=tf.matmul(X,w)+b .........(input*weight+bias)


Now, Apply the activation function on the neural network model.


To execute code in TensorFlow, we have to create a session.


Create a session and run it to get a proper output:

with tf.Session() as sess:


Whenever a variable is created, include an initializer and pass it as an argument for

Initialize the variables:


Finally, Specify the loss function and optimizer to evaluate the model.


Example code: Train and evaluate TensorFlow models in Python

#import the necessary modules
import tensorflow as tf
import pandas as pd
import numpy as np

#load the data
df = pd.read_csv("mnist_dataset.csv")

#have a look at the first ten rows of the data.

Train and evaluate TensorFlow models in Python

#grabbing the first column and assign it to labels.
labels = df.iloc[:,0].values 

#grabbing all the column except the first and assigning it to the image.
image = df.iloc[:,1:].values  

from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(sparse=False)

#reshapping it to get a column array.
encoder_reshape= labels.reshape(len(df),1)

#transforming and fitting the labels.
encoded_labels = encoder.fit_transform(encoder_reshape)
encoded_labels = encoded_labels.astype(np.uint8)

#printing a number from the array to see how encoder works.

[0 0 1 0 0 0 0 0 0 0]

One hot encoding represents 1 for presence and 0 for the absence of each class.

#splitting the data into train and test set
from sklearn.model_selection import train_test_split


Training a model:

#create a placeholder for input and output layer.
X = tf.placeholder(tf.float32,shape=[None,784])     #input layer
y = tf.placeholder(tf.float32,shape=[None,10])      #output layer

#create a hidden layer
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

#perform the linear operation
y_in = tf.matmul(X,w)+b

#apply activation function
output = tf.nn.softmax(y_in)

#Finding the loss and optimizer using tensorflow's in-built function
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(y_in,y))
optimizer = tf.train.GradientDescentOptimizer(0.01)

#minimizing the loss function
train_data = optimizer.minimize(loss)

prediction = tf.equal(tf.argmax(y_in,1),tf.argmax(y,1))

#converting into float and finding the average accuracy
accuracy = tf.reduce_mean(tf.cast(prediction,tf.float32))
#creating a session and passing the values in batches of size 100 to placeholders.
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    for i in range(1000):
            start = (i*100) % (X_train.shape[0]-100)
            end = start+100
            batch_X = X_train[start:end]
            batch_y = y_train[start:end]

I hope this post helps!

Also, read Real-time object detection using TensorFlow in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *