Build a ANN for hand digit recognition task in python

Machine Learning Using Python Artificial Neural Network
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€59.73
List Price:  €85.34
You save:  €25.60
£51.80
List Price:  £74.01
You save:  £22.20
CA$96.84
List Price:  CA$138.36
You save:  CA$41.51
A$106.75
List Price:  A$152.51
You save:  A$45.75
S$89.95
List Price:  S$128.50
You save:  S$38.55
HK$545.64
List Price:  HK$779.53
You save:  HK$233.88
CHF 55.85
List Price:  CHF 79.79
You save:  CHF 23.94
NOK kr703.17
List Price:  NOK kr1,004.57
You save:  NOK kr301.40
DKK kr445.91
List Price:  DKK kr637.04
You save:  DKK kr191.13
NZ$118.73
List Price:  NZ$169.63
You save:  NZ$50.89
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,495.52
List Price:  ৳12,136.98
You save:  ৳3,641.45
₹6,172.17
List Price:  ₹8,817.76
You save:  ₹2,645.59
RM295.67
List Price:  RM422.40
You save:  RM126.73
₦107,084.70
List Price:  ₦152,984.70
You save:  ₦45,900
₨19,808.40
List Price:  ₨28,298.93
You save:  ₨8,490.52
฿2,243.26
List Price:  ฿3,204.79
You save:  ฿961.53
₺2,883.76
List Price:  ₺4,119.83
You save:  ₺1,236.07
B$378.92
List Price:  B$541.35
You save:  B$162.42
R1,231.03
List Price:  R1,758.69
You save:  R527.66
Лв116.90
List Price:  Лв167.02
You save:  Лв50.11
₩97,041.13
List Price:  ₩138,636.13
You save:  ₩41,595
₪234.19
List Price:  ₪334.57
You save:  ₪100.38
₱3,968.43
List Price:  ₱5,669.43
You save:  ₱1,701
¥10,316.87
List Price:  ¥14,739.02
You save:  ¥4,422.15
MX$1,309.87
List Price:  MX$1,871.33
You save:  MX$561.45
QR255.14
List Price:  QR364.50
You save:  QR109.36
P938.51
List Price:  P1,340.79
You save:  P402.27
KSh9,032.87
List Price:  KSh12,904.65
You save:  KSh3,871.78
E£3,400.11
List Price:  E£4,857.51
You save:  E£1,457.40
ብር9,985.48
List Price:  ብር14,265.58
You save:  ብር4,280.10
Kz64,180.83
List Price:  Kz91,690.83
You save:  Kz27,510
CLP$67,522.15
List Price:  CLP$96,464.35
You save:  CLP$28,942.20
CN¥499.22
List Price:  CN¥713.21
You save:  CN¥213.98
RD$4,414.63
List Price:  RD$6,306.89
You save:  RD$1,892.25
DA9,089.11
List Price:  DA12,985
You save:  DA3,895.89
FJ$157.67
List Price:  FJ$225.25
You save:  FJ$67.58
Q535.38
List Price:  Q764.86
You save:  Q229.48
GY$14,604.61
List Price:  GY$20,864.62
You save:  GY$6,260.01
ISK kr8,549.27
List Price:  ISK kr12,213.77
You save:  ISK kr3,664.50
DH634.40
List Price:  DH906.33
You save:  DH271.92
L1,171.66
List Price:  L1,673.87
You save:  L502.21
ден3,674.28
List Price:  ден5,249.20
You save:  ден1,574.92
MOP$561.02
List Price:  MOP$801.49
You save:  MOP$240.47
N$1,234.02
List Price:  N$1,762.97
You save:  N$528.94
C$2,569.13
List Price:  C$3,670.35
You save:  C$1,101.21
रु9,858.12
List Price:  रु14,083.64
You save:  रु4,225.51
S/245.88
List Price:  S/351.28
You save:  S/105.39
K291.36
List Price:  K416.25
You save:  K124.88
SAR262.47
List Price:  SAR374.98
You save:  SAR112.50
ZK1,666.62
List Price:  ZK2,380.99
You save:  ZK714.36
L303.21
List Price:  L433.17
You save:  L129.96
Kč1,456.45
List Price:  Kč2,080.74
You save:  Kč624.28
Ft23,442.21
List Price:  Ft33,490.30
You save:  Ft10,048.09
SEK kr657.57
List Price:  SEK kr939.43
You save:  SEK kr281.85
ARS$95,110.21
List Price:  ARS$135,877.55
You save:  ARS$40,767.34
Bs482.36
List Price:  Bs689.11
You save:  Bs206.75
COP$278,383.76
List Price:  COP$397,708.14
You save:  COP$119,324.37
₡35,369.65
List Price:  ₡50,530.25
You save:  ₡15,160.59
L1,828.90
List Price:  L2,612.83
You save:  L783.92
₲504,728.30
List Price:  ₲721,071.34
You save:  ₲216,343.03
$U2,810.15
List Price:  $U4,014.67
You save:  $U1,204.52
zł253.83
List Price:  zł362.63
You save:  zł108.80
Already have an account? Log In

Transcript

Hello everyone, welcome to the course of machine learning with Python. In this video, we shall learn how to implement handwritten digit recognition using artificial neural networks in Python. So first, we shall be importing necessary libraries. So import NumPy as NP and import matplotlib.pi plot as PLT now from Kara's dot data sets, we have to import m NIST. So let's go ahead and run this particular cell. So here it has mentioned using TensorFlow back end, now, we shall be loading the data set.

So extern comma y train, this is one tupple. And if this common White is this is another tupple. So both these tables are returned by a nice dot load data method. So let's go ahead and run this particular cell. So now external dot shape within third bracket zero will be 60,000. That means there are total 60,000 printing samples.

Similarly, there are 10,000 test samples now Extreme dogs shape will give us 60,000 comma 28 comma 20. So, that means it is comprises of 60,000 images each image is of 28 pixels by 20 pixels okay now number of training samples is nothing but x underscore train dot shape within the brackets zero and number of samples is x underscore test dot shape with the bracket zero number of row will be equals to x underscore screen dot shape within fat bracket one and number of columns is equals to x underscore train dot shape within bracket two. So row and column are nothing but the number of rows and number of column in each images. So let's go ahead and add this particular cell. Now we shall be flattening the data in order to flatten the data. So first we have to convert it to float 32 format and then we have to reshape it to number of training samples multiplied with row called multiply width column okay.

So we have converted it to float afterwards. So now if we look at the shape of the printing as test data set it is 60,000 comma 74 and 10,000 comma 74. So now 28 cross 28 dimensional image has been converted to a flattened array of dimension 74. Now, we should normalize the data see that here we have already converted it to float, so, we'll just divide it by 255. And the maximum value of external expressed would be one and the minimum value is zero that means all the pixels will now contain the value between zero to one. Now, we shall want to encode the target variables so from Kara's dot utils import to underscore categorical So, why create one word is to underscore categorical by Korean and why taste one out is equals to two categorical within bracket by test so, we just take one example first one hot encoded cleaning level.

So it should look something like this. Okay? see only one element is one and other says zero now within the neural network model. So from Cara's dot layers we'll be putting days and from Kara's dot models will be inputting sequential, so, it will be a sequential model. So, input that he didn't then output. So, there is many we have imported sequential model.

Now, number of input feature will be nothing but romantica with column which is 74 and number of classes we goes to take. So, we have created a model called my nn which is nothing but an instance or object of the sequential class. Now, we shall be adding a hidden layer in my model. So, my underscore alien dot add, then within bracket dense, who has specified output dimension to be number of input features and input dimension should be the number of input features that means, the hidden layer should contain the same number of nodes as the input layer. Okay and have specified activation as read now, we shall be adding the output layer. Now the output layer accepts a dense Connect share from the previous hidden layer.

So, here we did not to specify the input dimension, but we should specify the output dimension should be close to number of classes and the activation should be softmax. So, let's go ahead and run this particular cell. So, after that we can print the summary of my model it shows that it has two dense layers okay. And there are total 620 3000 total parameters out of which all the parameters are trainable. Now, we shall be compiling the model note that always compile the model before training. So, this is our model my underscore training we shall be compiling it with loss categorical cross entropy optimizer Adam and metrics is accuracy.

So matrix is accuracy means it should measure the error To receive values Okay, now creating the model. So, we have specified number of epochs tane and batch size 128. So, we will be using dot fit method to fit the training data set into my model. So, it will take some time depending upon the system configuration. So, each epoch is taking approximately 23 seconds, so, 10 epochs will take around 230 seconds, okay. So, now our training is completed note that after each report the accuracy is increasing while the loss is decreasing.

Now, we shall be making the predictions. So, why underscore trading is equals to the model dot predict classes within bracket we are supplying the test data set So, it should have total 10,000 predictive values. Now we shall be measuring the performance of the model. So again from SK learn dot matrix we'll be importing confusion matrix and here the confusion matrix con underscore mat is equals to the confusion matrix within bracket it is taking the argument whitest and why credit so, we can print the confusion matrix and see that the diagram increase or more or less taking the larger values and eliminates our smaller values. So, the accuracy would be NP dot trace of this confusion matrix that is sum of all the diagonal values divided by total number of test samples which is nothing but total value of this confusion matrix okay. So the accuracy of the model is 98.31% on the test data set Okay.

Now we shall be plotting the loss versus epoch curve. So while creating the model, we have stored the training instance Inside a variable called history, so, now within history there is a dictionary which actually stores the value of all the losses Part A POC, and all the addresses part of So, these we are actually plotting, so let's see, okay. So loss is gradually decreasing for each block and the date is also discernible, now plotting accuracy versus Invoker. So that can be done using this function. So, we can see that the accuracy is gradually increasing with the number of epoch, but here it is more or less saturated. Okay, so so far, this is our discussion.

And this concludes the video lecture of this entire course. So I hope that you have enjoyed the course all the best for your future. Thank you.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.