import numpy as np
import matplotlib.pyplot as plt
import keras
from keras import layers
It already comes as trainging and testing set, so you don't need to split it yourself
(Xt, yt), (Xs, ys) = keras.datasets.mnist.load_data()
Xt.shape, Xs.shape, yt.shape, ys.shape
((60000, 28, 28), (10000, 28, 28), (60000,), (10000,))
plt.figure(figsize=(15,7))
for k in range(40):
plt.subplot(4,10,k+1)
plt.imshow(Xt[k],cmap='gray')
plt.axis('off')
plt.show()
def modelCNN(input_shape=(28,28,1),output=10):
Input = keras.Input(shape=input_shape)
x = layers.Conv2D(32, kernel_size=(3, 3), activation="relu")(Input)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.Conv2D(64, kernel_size=(3, 3), activation="relu")(x)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.Flatten()(x)
x = layers.Dropout(0.5)(x)
output = layers.Dense(output, activation="softmax")(x)
model = keras.models.Model(Input, output)
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
return model
model = modelCNN()
model.summary()
Model: "model_2" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_6 (InputLayer) (None, 28, 28, 1) 0 _________________________________________________________________ conv2d_7 (Conv2D) (None, 26, 26, 32) 320 _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, 13, 13, 32) 0 _________________________________________________________________ conv2d_8 (Conv2D) (None, 11, 11, 64) 18496 _________________________________________________________________ max_pooling2d_4 (MaxPooling2 (None, 5, 5, 64) 0 _________________________________________________________________ flatten_2 (Flatten) (None, 1600) 0 _________________________________________________________________ dropout_2 (Dropout) (None, 1600) 0 _________________________________________________________________ dense_2 (Dense) (None, 10) 16010 ================================================================= Total params: 34,826 Trainable params: 34,826 Non-trainable params: 0 _________________________________________________________________
Xt = Xt[:,:,:,None]
Xs = Xs[:,:,:,None]
Xt.shape, Xs.shape
((60000, 28, 28, 1), (10000, 28, 28, 1))
history = model.fit(Xt, yt, batch_size=128, epochs=2, verbose=1,validation_data=(Xs, ys),)
Train on 60000 samples, validate on 10000 samples Epoch 1/2 60000/60000 [==============================] - 27s 453us/step - loss: 0.1420 - accuracy: 0.9574 - val_loss: 0.0641 - val_accuracy: 0.9807 Epoch 2/2 60000/60000 [==============================] - 27s 451us/step - loss: 0.1148 - accuracy: 0.9644 - val_loss: 0.0597 - val_accuracy: 0.9817
Keras model gives you probability scores
ytp = model.predict(Xt)
ysp = model.predict(Xs)
Converting probabilities to class number by computing the maximum probabilty
ytpI = np.argmax(ytp,1)
yspI = np.argmax(ysp,1)
Predicted classes for first 10 training examples
ytpI[:10]
array([5, 0, 4, 1, 9, 2, 1, 3, 1, 4], dtype=int64)
Actual classes of first 10 training examples
yt[:10]
array([5, 0, 4, 1, 9, 2, 1, 3, 1, 4], dtype=uint8)
print('Training Accuracy : ',np.mean(ytpI==yt))
print('Testing Accuracy : ',np.mean(yspI==ys))
Training Accuracy : 0.9851333333333333 Testing Accuracy : 0.9817