Simple Neural Network by using RMSprop optimzer with only 5 layers.
Here's the coding.
from __future__ import print_function
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
from keras.utils import np_utils
import os
from six.moves import cPickle as pickle
batch_size = 128
num_classes = 10
epochs = 20
def load_data():
test_filename = "notMNIST.pickle"
if os.path.exists(test_filename):
with open(test_filename, 'rb') as f:
letter_dataset = pickle.load(f)
return (letter_dataset)
lt_dt = load_data()
train_dataset = lt_dt['train_dataset']
train_labels = lt_dt['train_labels']
valid_dataset = lt_dt['valid_dataset']
valid_labels = lt_dt['valid_labels']
test_dataset = lt_dt['test_dataset']
test_labels = lt_dt['test_labels']
x_train = train_dataset.reshape(train_dataset.shape[0], 28*28)
y_train = np_utils.to_categorical(train_labels, 10)
x_valid = valid_dataset.reshape(valid_dataset.shape[0], 28*28)
y_valid = np_utils.to_categorical(valid_labels, 10)
x_test = test_dataset.reshape(test_dataset.shape[0],28*28)
y_test = np_utils.to_categorical(test_labels, 10)
x_train = x_train.astype('float32') / 255
x_valid = x_valid.astype('float32') /255
x_test = x_test.astype('float32') / 255
print(x_train.shape[0], 'train samples')
print(x_valid.shape[0], 'valid samples')
print(x_test.shape[0], 'test samples')
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy'])
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_valid, y_valid))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Outputs:
200000 train samples
10000 valid samples
10000 test samples
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 512) 401920
_________________________________________________________________
dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 512) 262656
_________________________________________________________________
dropout_2 (Dropout) (None, 512) 0
_________________________________________________________________
dense_3 (Dense) (None, 10) 5130
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________
Train on 200000 samples, validate on 10000 samples
Epoch 1/20
200000/200000 [==============================] - 25s 127us/step - loss: 0.6390 - acc: 0.8158 - val_loss: 0.4971 - val_acc: 0.8476
Epoch 2/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.4582 - acc: 0.8614 - val_loss: 0.4366 - val_acc: 0.8634
Epoch 3/20
200000/200000 [==============================] - 26s 128us/step - loss: 0.4092 - acc: 0.8757 - val_loss: 0.3952 - val_acc: 0.8774
Epoch 4/20
200000/200000 [==============================] - 26s 129us/step - loss: 0.3776 - acc: 0.8854 - val_loss: 0.3747 - val_acc: 0.8840
Epoch 5/20
200000/200000 [==============================] - 26s 128us/step - loss: 0.3560 - acc: 0.8918 - val_loss: 0.3678 - val_acc: 0.8871
Epoch 6/20
200000/200000 [==============================] - 26s 128us/step - loss: 0.3397 - acc: 0.8956 - val_loss: 0.3568 - val_acc: 0.8915
Epoch 7/20
200000/200000 [==============================] - 26s 131us/step - loss: 0.3261 - acc: 0.9003 - val_loss: 0.3476 - val_acc: 0.8954
Epoch 8/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.3163 - acc: 0.9028 - val_loss: 0.3446 - val_acc: 0.8966
Epoch 9/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.3056 - acc: 0.9062 - val_loss: 0.3339 - val_acc: 0.8999
Epoch 10/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2966 - acc: 0.9086 - val_loss: 0.3363 - val_acc: 0.9009
Epoch 11/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2899 - acc: 0.9108 - val_loss: 0.3439 - val_acc: 0.8981
Epoch 12/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2845 - acc: 0.9123 - val_loss: 0.3378 - val_acc: 0.9040
Epoch 13/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2769 - acc: 0.9150 - val_loss: 0.3379 - val_acc: 0.9018
Epoch 14/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2724 - acc: 0.9158 - val_loss: 0.3412 - val_acc: 0.8999
Epoch 15/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2661 - acc: 0.9176 - val_loss: 0.3547 - val_acc: 0.9020
Epoch 16/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2610 - acc: 0.9191 - val_loss: 0.3435 - val_acc: 0.9036
Epoch 17/20
200000/200000 [==============================] - 26s 132us/step - loss: 0.2560 - acc: 0.9197 - val_loss: 0.3500 - val_acc: 0.9034
Epoch 18/20
200000/200000 [==============================] - 26s 132us/step - loss: 0.2524 - acc: 0.9219 - val_loss: 0.3387 - val_acc: 0.9038
Epoch 19/20
200000/200000 [==============================] - 26s 132us/step - loss: 0.2477 - acc: 0.9232 - val_loss: 0.3428 - val_acc: 0.9056
Epoch 20/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2430 - acc: 0.9247 - val_loss: 0.3492 - val_acc: 0.9065
Test loss: 0.144602749768
Test accuracy: 0.9583