In [2]:
## A simple implementation of ANN for MNIST
import random
import numpy as np
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout,Activation
from keras.optimizers import RMSprop
from keras.utils import np_utils
from keras import backend as K
#Image Dimension:::  In Tensorflow:(64,64,3)     ..In THEANO:(3,64,64) i.e. channel goes first
K.set_image_dim_ordering('tf')  # always check u r using the correct image dimension
import matplotlib.pyplot as plt
%matplotlib inline

In [3]:
## Set the full path to mnist.pkl.gz
## Point this to the data folder inside the repository
path_to_dataset = "./mnist.pkl.gz"

In [5]:
## Load the dataset
(X_train,y_train),(X_test, y_test) = mnist.load_data(path_to_dataset)


Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11419648/11490434 [============================>.] - ETA: 0s

In [6]:
print(X_train.shape, y_train.shape)
print(X_test.shape,y_test.shape)


(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)

X_train = X_train.reshape(60000,784) X_test = X_test.reshape(10000,784) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /=255 X_test /=255 print(X_train.shape[0],'train_samples') print(X_test.shape[0],'test_samples')


In [10]:
batch_size = 128
nb_classes =10
nb_epochs = 10

# convert class vectors to binary class matrices for softmax layer
Y_train = keras.utils.np_utils.to_categorical(y_train,nb_classes)
Y_test = keras.utils.np_utils.to_categorical(y_test,nb_classes)
## for example 6's label is now [0,0,0,0,0,0,0,0]

print(Y_train.shape)


(60000, 10)

MODEL DEFINITION


In [15]:
model = Sequential()
model.add(Dense(512,input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation('softmax'))

In [16]:
model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 512)               401920    
_________________________________________________________________
activation_1 (Activation)    (None, 512)               0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 512)               262656    
_________________________________________________________________
activation_2 (Activation)    (None, 512)               0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 10)                5130      
_________________________________________________________________
activation_3 (Activation)    (None, 10)                0         
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________

MODEL COMPILATION


In [17]:
model.compile(loss='categorical_crossentropy',optimizer=RMSprop(),metrics=['accuracy'])

In [19]:
history = model.fit(X_train,Y_train,
                   batch_size=batch_size,nb_epoch=nb_epochs,
                   verbose=1,validation_data=(X_test,Y_test))


C:\Users\prassha\AppData\Local\Continuum\Anaconda3\lib\site-packages\keras\models.py:844: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
  warnings.warn('The `nb_epoch` argument in `fit` '
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 24s - loss: 0.2431 - acc: 0.9254 - val_loss: 0.1269 - val_acc: 0.9621
Epoch 2/10
60000/60000 [==============================] - 13s - loss: 0.1024 - acc: 0.9688 - val_loss: 0.0856 - val_acc: 0.9729
Epoch 3/10
60000/60000 [==============================] - 13s - loss: 0.0740 - acc: 0.9777 - val_loss: 0.0726 - val_acc: 0.9788
Epoch 4/10
60000/60000 [==============================] - 13s - loss: 0.0599 - acc: 0.9817 - val_loss: 0.0669 - val_acc: 0.9810
Epoch 5/10
60000/60000 [==============================] - 14s - loss: 0.0498 - acc: 0.9851 - val_loss: 0.0769 - val_acc: 0.9792
Epoch 6/10
60000/60000 [==============================] - 13s - loss: 0.0435 - acc: 0.9872 - val_loss: 0.0755 - val_acc: 0.9799
Epoch 7/10
60000/60000 [==============================] - 13s - loss: 0.0378 - acc: 0.9888 - val_loss: 0.0912 - val_acc: 0.9795
Epoch 8/10
60000/60000 [==============================] - 13s - loss: 0.0341 - acc: 0.9901 - val_loss: 0.0826 - val_acc: 0.9837
Epoch 9/10
60000/60000 [==============================] - 14s - loss: 0.0310 - acc: 0.9914 - val_loss: 0.0800 - val_acc: 0.9826
Epoch 10/10
60000/60000 [==============================] - 14s - loss: 0.0272 - acc: 0.9920 - val_loss: 0.0923 - val_acc: 0.9820

In [20]:
## we can check the param of the model after training
history.params


Out[20]:
{'batch_size': 128,
 'do_validation': True,
 'epochs': 10,
 'metrics': ['loss', 'acc', 'val_loss', 'val_acc'],
 'samples': 60000,
 'verbose': 1}

In [21]:
history.history


Out[21]:
{'acc': [0.92539999996821087,
  0.96875,
  0.97770000000000001,
  0.98173333330154422,
  0.98509999999999998,
  0.9871500000317891,
  0.98878333330154422,
  0.99009999999999998,
  0.99138333333333328,
  0.99203333333333332],
 'loss': [0.24306186430851617,
  0.10241646350622177,
  0.074045213498175139,
  0.05993780317977071,
  0.04978033597295483,
  0.043547396647930142,
  0.037821342162663737,
  0.034101896805347254,
  0.031037697566005711,
  0.02718515122255],
 'val_acc': [0.96209999999999996,
  0.97289999999999999,
  0.9788,
  0.98099999999999998,
  0.97919999999999996,
  0.97989999999999999,
  0.97950000000000004,
  0.98370000000000002,
  0.98260000000000003,
  0.98199999999999998],
 'val_loss': [0.12690679808259012,
  0.085576612854003906,
  0.072642266020923857,
  0.066926550019974815,
  0.076914661341672769,
  0.075545392327068833,
  0.091223234136444814,
  0.082562959535460681,
  0.079975112177579286,
  0.092299355368274702]}

EVALUATION AND PREDICTION


In [22]:
score = model.evaluate(X_test,Y_test,verbose=0)
print('Test score:', score[0])
print('Test accuracy:',score[1])


Test score: 0.0922993640328
Test accuracy: 0.982

Now lets predict one single sample


In [26]:
X_test_0 = X_test[0,:].reshape(1,784)
Y_test_0 = Y_test[0,:]
print(Y_test_0)
plt.imshow(X_test_0.reshape(28,28))


[ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]
Out[26]:
<matplotlib.image.AxesImage at 0x23550a76908>

In [29]:
pred = model.predict(X_test_0)
print('Label of testing sample:', np.argmax(Y_test_0))
print('\nOutput of the softmax layer:',pred[0])
print('\nNeural Network prediction:', np.argmax(pred[0]))


Label of testing sample: 7

Output of the softmax layer: [  2.98037133e-24   8.67462551e-19   2.12443354e-16   1.26270700e-14
   2.76469579e-28   1.28870976e-18   1.76797381e-33   1.00000000e+00
   3.10203522e-20   2.58616544e-15]

Neural Network prediction: 7

In [ ]: