Professional Documents
Culture Documents
CNN
Building a simple Convolutional Neural Network
using mnist data set to recognize handwritten
digits.
Dataset:
Data Processing:
import tensorflow as tf
(x_train, y_train), (x_test, y_test) =
tf.keras.datasets.mnist.load_data()
The data set contains 60,000 traning images and 10000 testing
images. Here I split the data into training and testing datasets
respectively. The x_train & x_test contains grayscale codes while
y_test & y_train contains labels from 0–9 which represents the
numbers.
Here we get pretty high accuracy with just 10 epochs. Since the
dataset doesn’t need heavy computational power you can play
around with the number of epochs you can also play around with the
optimizer, loss function and metrics.
Model Evaluation:
model.evaluate(x_test, y_test)
When this model is evaluated we see that just 10 epochs gave use the
accuracy of 98.59% at a very low loss.
Now to check its prediction:
image_index = 2853
plt.imshow(x_test[image_index].reshape(28, 28),cmap='Greys')
predict = x_test[image_index].reshape(28,28)
pred = model.predict(x_test[image_index].reshape(1, 28, 28, 1))
print(pred.argmax())
Image by author
And that is how you can build and implement a simple convolutional
neural network. You can implement this concept to various different
types of classification and other such implementations.