# 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)
```

**Output:**

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

output=tf.nn.activationFunction(y_in)

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

**Session:**

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

with tf.Session() as sess: sess.run()

*NOTE:*

*Whenever a variable is created, include an initializer and pass it as an argument for sess.run()*

**Initialize the variables:**

init=tf.global_variables_initializer()

**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. df.head(10)

#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. print(labels[25]) print(encoded_labels[25])

2 [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 X_train,X_test,y_train,y_test=train_test_split(image,encoded_labels,test_size=0.3,random_state=101)

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() sess.run(init) 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] sess.run(train_step,feed_dict={X:batch_X,y:batch_y}) print(sess.run(accuracy,feed_dict={X:X_test,y:y_test}))

I hope this post helps!

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

## Leave a Reply