Predicting MNIST Dataset using Keras

Aarthijayakumar
2 min readDec 31, 2020

What is keras?

Keras is an API designed for human beings, not machines. Keras follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear & actionable error messages. It also has extensive documentation and developer guides.

Let us know about MNIST dataset:

The MNIST dataset is an acronym that stands for the Modified National Institute of Standards and Technology dataset. It is a dataset of 60,000 small square 28×28 pixel grayscale images of handwritten single digits between 0 and 9.

To import MNIST dataset:

  1. from keras. datasets import mnist.
  2. from keras. models import Sequential. from keras. …
  3. from keras. utils import np_utils.
  4. # load data. (X_train, y_train), (X_test, y_test) = mnist. load_data()
  5. # flatten 28*28 images to a 784 vector for each image. num_pixels = X_train. shape[1] * X_train. shape[2] X_train = X_train.

What is Optimizer?

Optimizers are used to change the parameters of our model to minimize the loss function and make our prediction as accurate as possible. Optimizers updates the parameters of the model based on the output of loss function. Optimizers helps in molding our model to its most accurate possible form. Any algorithm can be used to optimize the model.

What is Dense ?

Dense is the number of layers we are using in our nural networks. In nutshell words dense layer is simply a layer where each unit or neuron is connected to each neuron in the next layer.. in our case 512 neuron layers are connected to each other. They receive input from the input we give and pass it to all the 512 layers.

Predicting MNIST dataset through keras following steps take place:

  1. Importing necessary libraries:

from __future__ import print_function

import numpy
numpy.random.seed(1337)

import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.datasets import mnist

2.Splitting the datasets into training and testing and reshaping:

num_classes = 10

batchSize = 128
epochs = 20

x_train, y_train), (x_test, y_test) = mnist.load_data()

print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)

x_train = x_train.reshape(60000, 784) # 28x28 = 784
x_test = x_test.reshape(10000, 784)

x_train = x_train.astype(‘float32’)
x_test = x_test.astype(‘float32’)

x_train /= 255 # 0–255 … 0–1
x_test /= 255 # normalizing your data

print(x_train.shape[0], ‘train samples’)
print(x_test.shape[0], ‘test samples’)

print(x_train.shape)
print(y_train.shape)

y_train[0]

3.3.Compiling the model:

model = Sequential()

model.add( Dense(512, activation=’sigmoid’, input_shape=(784,) ) )
model.add( Dense(512, activation=’sigmoid’ ) )
model.add( Dense(num_classes, activation=’softmax’))

model.summary()

model.compile(loss=’categorical_crossentropy’, optimizer=SGD(),
metrics=[‘accuracy’])

history = model.fit( x_train, y_train,
batch_size=batchSize,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))

4.Evaluating the model:

score = model.evaluate(x_test, y_test)

score[0]

--

--