Hello Every one, In recent days we are hearing a lot about Machine Learning, Deep learning. Many of us have rough idea what is machine learning and deep learning. In this article, I am showing the implementation of neural networks from scratch. Neural networks have great potential in extracting the features of the images in order to classify the images or detection of object in an image as well as audio data. This kind of data is said to be unstructured data. Here we are taking an digits image data and building an artificial neural network (ANN) to predict the given digit is exactly matching with the labeled data. Most of the Deep learning problems are supervised learning based which means for given data we know what is output exactly.
Image is always represented in matrix form m x n pixels. Here we are considering 64 pixel image which is 8 x 8 pixel image and the image is gray image which is black and white image.
Steps in implementing ANN for Digits Classification ;
1. Importing Libraries
2. Importing Data Set
3. Splitting the Data for train and test
4. Building Model
5. Evaluation
Install libraries if not installed earlier
For installing tensor flow and keras libraries run below commands in jupyter or google colabs cell
!pip install tensorflow
!pip install keras
Importing Libraries
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import keras
from tensorflow.keras import models,layers
from sklearn import datasets
from sklearn.model_selection import train_test_split
Importing Dataset and lets have look on shape of data
digits = datasets.load_digits()
X= digits.data
y= digits.target
print(X.shape,y.shape).
Output :
(1797, 64) (1797,)
Let's have a look how data looks like
plt.imshow(digits.images[3], cmap=plt.cm.gray_r, interpolation='nearest')
Output :
Splitting the data for train and test
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42,stratify=y)
print(X_train.shape,X_test.shape,y_train.shape,y_test.shape)
y_train = y_train.reshape(-1,)
y_train.shape
Output :
(1437, 64) (360, 64) (1437,) (360,)Building Basic ANN model for classificationann = models.Sequential([ layers.Flatten(input_shape=(64,1)), layers.Dense(3000,activation='relu'), layers.Dense(1000,activation='relu'), layers.Dense(10,activation='sigmoid') ])
ann.compile(optimizer='SGD',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
ann.fit(X_train,y_train,epochs=5)
Output :
Epoch 1/5 45/45 [==============================] - 2s 16ms/step - loss: 2.3419 - accuracy: 0.5718 Epoch 2/5 45/45 [==============================] - 1s 16ms/step - loss: 0.1600 - accuracy: 0.9650 Epoch 3/5 45/45 [==============================] - 1s 15ms/step - loss: 0.1017 - accuracy: 0.9796 Epoch 4/5 45/45 [==============================] - 1s 16ms/step - loss: 0.0752 - accuracy: 0.9947 0s - loss: 0.0804 - accu Epoch 5/5 45/45 [==============================] - 1s 14ms/step - loss: 0.0551 - accuracy: 0.9951Evaluate the model :ann.evaluate(X_test,y_test)
Output :
[0.08141227066516876, 0.980555534362793] --> loss, accuracyLet's Predict the test data with actual target dataWe have actaul labeled data which is y_test. Now we have developed our ANN model for predicting the X_test data which is said to be y_pred . Our aim is to make Y_pred = y_test , then we can say our model is doing very good.y_pred = ann.predict(X_test)
np.argmax(y_pred[3])
y_classes = [np.argmax(element) for element in y_pred]
print(y_test[:10])
print(y_classes[:10])
Output :
[5, 2, 8, 1, 7, 2, 6, 2, 6, 5] --> y_test first 10 values [5, 2, 8, 1, 7, 2, 6, 2, 6, 5] --> y_pred first 10 valuesConclusionWe have observed at the end y_test and Y_pred values are looking similar which means our ANN model which we build is doing very good. Actually we have confidence on our model at the stahe of evaluation stage, if you have observed at evaluation stage we have achieved loss as 0.08 which is near to zero and accuracy is 0.98 which is 98 % . This section shows us how good our model can predict on new data set.This is all from this article, How you got basic idea at the end of article on how to implement ANN on digits dataset.
Well explained.
ReplyDeleteThank you :-)
ReplyDeleteWell Explained...
ReplyDeleteThanks you :-)
Delete👍👍👍👍
ReplyDelete:-)
Delete