In [29]: import keras
from [Link] import mnist
from [Link] import to_categorical
import numpy as np
from PIL import Image
import [Link] as plt
In [30]: (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
In [31]: train_images.shape
(60000, 28, 28)
Out[31]:
In [32]: len(train_labels)
60000
Out[32]:
In [33]: train_labels
array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)
Out[33]:
In [34]: test_images.shape
(10000, 28, 28)
Out[34]:
In [35]: len(test_labels)
10000
Out[35]:
In [36]: test_labels
array([7, 2, 1, ..., 4, 5, 6], dtype=uint8)
Out[36]:
In [37]: # Step 1: Data Preprocessing (Machine Readable Form)
# Scaling - One Unit Standard Deviation for Numeric Columns
# Fundamental requirement of Deep Learning Networks
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
In [38]: # Step 1: Data Preprocessing (Machine Readable Form)
# OneHot Coding for Categorical Data
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
In [39]: # Step 2: Build your Network
from keras import models
from keras import layers
network = [Link]()
[Link]([Link](512, activation='relu', input_shape=(28 * 28,)))
[Link]([Link](10, activation='softmax'))
In [40]: # Step 3: Compile your Network
[Link](optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
In [41]: # Step 4: Training / Learning your Network
[Link](train_images,
train_labels,
epochs=5,
batch_size=128)
Epoch 1/5
469/469 [==============================] - 4s 9ms/step - loss: 0.2550 - accuracy: 0.9257
Epoch 2/5
469/469 [==============================] - 5s 10ms/step - loss: 0.1027 - accuracy: 0.9693
Epoch 3/5
469/469 [==============================] - 4s 9ms/step - loss: 0.0678 - accuracy: 0.9793
Epoch 4/5
469/469 [==============================] - 5s 10ms/step - loss: 0.0484 - accuracy: 0.9851
Epoch 5/5
469/469 [==============================] - 5s 11ms/step - loss: 0.0370 - accuracy: 0.9884
<[Link] at 0x1d90ba331c8>
Out[41]:
In [42]: # Step 5: Performance Evaluation - Test Dataset
# Unseen (Not included in Training Dataset)
test_loss, test_acc = [Link](test_images, test_labels)
print('test_acc:', test_acc)
313/313 [==============================] - 1s 2ms/step - loss: 0.0641 - accuracy: 0.9811
test_acc: 0.9811000227928162
In [51]: # Save Model use in FUTURE Use
[Link]('HandDigitRecorgnitionModel.h5')