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)
In [6]:
print(X_train.shape, y_train.shape)
print(X_test.shape,y_test.shape)
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)
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()
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))
In [20]:
## we can check the param of the model after training
history.params
Out[20]:
In [21]:
history.history
Out[21]:
EVALUATION AND PREDICTION
In [22]:
score = model.evaluate(X_test,Y_test,verbose=0)
print('Test score:', score[0])
print('Test accuracy:',score[1])
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))
Out[26]:
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]))
In [ ]: