paint-brush
Classify Handwritten Digits using Deep learning with Tensorflow by@itsvinayak
236 reads

Classify Handwritten Digits using Deep learning with Tensorflow

by vinayakApril 24th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Deep learning is a subpart of machine learning and artificial intelligence which is also known as deep neural network this networks are capable of learning unsupervised from provided data which is unorganized or unlabeled. today, we will implement a neural network in 6 easy steps using TensorFlow to classify handwritten digits. We will use NumPy, Matplotlib, NumPy and Tensorflow to build a deep learning neural network. We'll use these modules to train our neural network and make a prediction.
featured image - Classify Handwritten Digits using Deep learning with Tensorflow
vinayak HackerNoon profile picture

Deep learning is a subpart of machine learning and artificial intelligence which is also known as deep neural network this networks capable of learning unsupervised from provided data which is unorganized or unlabeled. today, we will implement a neural network in 6 easy steps using TensorFlow to classify handwritten digits.

Modules required :

NumPy:

$ pip install numpy 

Matplotlib:

$ pip install matplotlib 

Tensorflow:

$ pip install tensorflow 

Steps to follow:

Step 1 : Importing all dependence

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

Step 2 : Import data and normalize it

mnist = tf.keras.datasets.mnist
(x_train,y_train) , (x_test,y_test) = mnist.load_data()
 
x_train = tf.keras.utils.normalize(x_train,axis=1)
x_test = tf.keras.utils.normalize(x_test,axis=1)

Step 3 : view data

def draw(n):
    plt.imshow(n,cmap=plt.cm.binary)
    plt.show() 
     
draw(x_train[0])

Step 4 : make a neural network and train it

#there are two types of models
#sequential is most common
 
model = tf.keras.models.Sequential()
 
model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
#reshape
 
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))
 
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy']
              )
model.fit(x_train,y_train,epochs=3)

Step 5 : check model accuracy and loss

val_loss,val_acc = model.evaluate(x_test,y_test)
print("loss-> ",val_loss,"\nacc-> ",val_acc)

Step 6 : prediction using model

predictions=model.predict([x_test])
print('lable -> ',y_test[2])
print('prediction -> ',np.argmax(predictions[2]))
 
draw(x_test[2])