Deep Feedforward Neural Network (Multilayer Perceptron with 2 Hidden Layers O.o)
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import RMSprop
from keras.datasets import mnist
from keras.utils import np_utils
Using TensorFlow backend.
batch_size = 128
nb_classes = 10
nb_epoch = 100
(X_train, y_train), (X_test, y_test) = mnist.load_data()
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
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Dense(output_dim=625, input_dim=784, init='normal'))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(output_dim=625, input_dim=625, init='normal'))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(output_dim=10, input_dim=625, init='normal'))
model.add(Activation('softmax'))
model.compile(optimizer=RMSprop(lr=0.001, rho=0.9), loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
dense_1 (Dense) (None, 625) 490625 dense_input_1[0][0]
____________________________________________________________________________________________________
activation_1 (Activation) (None, 625) 0 dense_1[0][0]
____________________________________________________________________________________________________
dropout_1 (Dropout) (None, 625) 0 activation_1[0][0]
____________________________________________________________________________________________________
dense_2 (Dense) (None, 625) 391250 dropout_1[0][0]
____________________________________________________________________________________________________
activation_2 (Activation) (None, 625) 0 dense_2[0][0]
____________________________________________________________________________________________________
dropout_2 (Dropout) (None, 625) 0 activation_2[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 10) 6260 dropout_2[0][0]
____________________________________________________________________________________________________
activation_3 (Activation) (None, 10) 0 dense_3[0][0]
====================================================================================================
Total params: 888135
____________________________________________________________________________________________________
history = model.fit(X_train, Y_train, nb_epoch=nb_epoch, batch_size=batch_size, verbose=1)
Epoch 1/100
60000/60000 [==============================] - 3s - loss: 0.2623 - acc: 0.9192
Epoch 2/100
60000/60000 [==============================] - 2s - loss: 0.1104 - acc: 0.9667
Epoch 3/100
60000/60000 [==============================] - 2s - loss: 0.0830 - acc: 0.9753
Epoch 4/100
60000/60000 [==============================] - 2s - loss: 0.0663 - acc: 0.9801
Epoch 5/100
60000/60000 [==============================] - 2s - loss: 0.0566 - acc: 0.9838
Epoch 6/100
60000/60000 [==============================] - 2s - loss: 0.0501 - acc: 0.9856
Epoch 7/100
60000/60000 [==============================] - 2s - loss: 0.0430 - acc: 0.9879
Epoch 8/100
60000/60000 [==============================] - 2s - loss: 0.0419 - acc: 0.9882
Epoch 9/100
60000/60000 [==============================] - 2s - loss: 0.0361 - acc: 0.9903
Epoch 10/100
60000/60000 [==============================] - 2s - loss: 0.0327 - acc: 0.9912
Epoch 11/100
60000/60000 [==============================] - 2s - loss: 0.0323 - acc: 0.9917
Epoch 12/100
60000/60000 [==============================] - 2s - loss: 0.0332 - acc: 0.9920
Epoch 13/100
60000/60000 [==============================] - 2s - loss: 0.0307 - acc: 0.9919
Epoch 14/100
60000/60000 [==============================] - 2s - loss: 0.0280 - acc: 0.9929
Epoch 15/100
60000/60000 [==============================] - 2s - loss: 0.0286 - acc: 0.9928
Epoch 16/100
60000/60000 [==============================] - 2s - loss: 0.0272 - acc: 0.9934
Epoch 17/100
60000/60000 [==============================] - 2s - loss: 0.0265 - acc: 0.9938
Epoch 18/100
60000/60000 [==============================] - 2s - loss: 0.0263 - acc: 0.9939
Epoch 19/100
60000/60000 [==============================] - 2s - loss: 0.0280 - acc: 0.9934
Epoch 20/100
60000/60000 [==============================] - 2s - loss: 0.0220 - acc: 0.9950
Epoch 21/100
60000/60000 [==============================] - 2s - loss: 0.0238 - acc: 0.9944
Epoch 22/100
60000/60000 [==============================] - 2s - loss: 0.0221 - acc: 0.9950
Epoch 23/100
60000/60000 [==============================] - 2s - loss: 0.0221 - acc: 0.9951
Epoch 24/100
60000/60000 [==============================] - 2s - loss: 0.0249 - acc: 0.9947
Epoch 25/100
60000/60000 [==============================] - 2s - loss: 0.0210 - acc: 0.9954
Epoch 26/100
60000/60000 [==============================] - 2s - loss: 0.0252 - acc: 0.9950
Epoch 27/100
60000/60000 [==============================] - 2s - loss: 0.0216 - acc: 0.9954
Epoch 28/100
60000/60000 [==============================] - 2s - loss: 0.0215 - acc: 0.9954
Epoch 29/100
60000/60000 [==============================] - 2s - loss: 0.0184 - acc: 0.9962
Epoch 30/100
60000/60000 [==============================] - 2s - loss: 0.0208 - acc: 0.9957
Epoch 31/100
60000/60000 [==============================] - 2s - loss: 0.0227 - acc: 0.9954
Epoch 32/100
60000/60000 [==============================] - 2s - loss: 0.0198 - acc: 0.9960
Epoch 33/100
60000/60000 [==============================] - 2s - loss: 0.0227 - acc: 0.9954
Epoch 34/100
60000/60000 [==============================] - 2s - loss: 0.0216 - acc: 0.9959
Epoch 35/100
60000/60000 [==============================] - 2s - loss: 0.0211 - acc: 0.9959
Epoch 36/100
60000/60000 [==============================] - 2s - loss: 0.0224 - acc: 0.9957
Epoch 37/100
60000/60000 [==============================] - 2s - loss: 0.0216 - acc: 0.9962
Epoch 38/100
60000/60000 [==============================] - 2s - loss: 0.0210 - acc: 0.9961
Epoch 39/100
60000/60000 [==============================] - 2s - loss: 0.0209 - acc: 0.9961
Epoch 40/100
60000/60000 [==============================] - 2s - loss: 0.0192 - acc: 0.9964
Epoch 41/100
60000/60000 [==============================] - 2s - loss: 0.0211 - acc: 0.9960
Epoch 42/100
60000/60000 [==============================] - 2s - loss: 0.0211 - acc: 0.9963
Epoch 43/100
60000/60000 [==============================] - 2s - loss: 0.0208 - acc: 0.9958
Epoch 44/100
60000/60000 [==============================] - 2s - loss: 0.0223 - acc: 0.9962
Epoch 45/100
60000/60000 [==============================] - 2s - loss: 0.0220 - acc: 0.9965
Epoch 46/100
60000/60000 [==============================] - 2s - loss: 0.0219 - acc: 0.9965
Epoch 47/100
60000/60000 [==============================] - 2s - loss: 0.0212 - acc: 0.9966
Epoch 48/100
60000/60000 [==============================] - 2s - loss: 0.0203 - acc: 0.9965
Epoch 49/100
60000/60000 [==============================] - 2s - loss: 0.0248 - acc: 0.9963
Epoch 50/100
60000/60000 [==============================] - 2s - loss: 0.0225 - acc: 0.9962
Epoch 51/100
60000/60000 [==============================] - 2s - loss: 0.0244 - acc: 0.9963
Epoch 52/100
60000/60000 [==============================] - 2s - loss: 0.0196 - acc: 0.9966
Epoch 53/100
60000/60000 [==============================] - 2s - loss: 0.0219 - acc: 0.9965
Epoch 54/100
60000/60000 [==============================] - 2s - loss: 0.0200 - acc: 0.9968
Epoch 55/100
60000/60000 [==============================] - 2s - loss: 0.0206 - acc: 0.9967
Epoch 56/100
60000/60000 [==============================] - 2s - loss: 0.0196 - acc: 0.9968
Epoch 57/100
60000/60000 [==============================] - 2s - loss: 0.0242 - acc: 0.9966
Epoch 58/100
60000/60000 [==============================] - 2s - loss: 0.0207 - acc: 0.9968
Epoch 59/100
60000/60000 [==============================] - 2s - loss: 0.0184 - acc: 0.9970
Epoch 60/100
60000/60000 [==============================] - 2s - loss: 0.0222 - acc: 0.9965
Epoch 61/100
60000/60000 [==============================] - 2s - loss: 0.0195 - acc: 0.9969
Epoch 62/100
60000/60000 [==============================] - 2s - loss: 0.0214 - acc: 0.9967
Epoch 63/100
60000/60000 [==============================] - 2s - loss: 0.0195 - acc: 0.9971
Epoch 64/100
60000/60000 [==============================] - 2s - loss: 0.0220 - acc: 0.9965
Epoch 65/100
60000/60000 [==============================] - 2s - loss: 0.0222 - acc: 0.9966
Epoch 66/100
60000/60000 [==============================] - 2s - loss: 0.0218 - acc: 0.9968
Epoch 67/100
60000/60000 [==============================] - 2s - loss: 0.0219 - acc: 0.9968
Epoch 68/100
60000/60000 [==============================] - 2s - loss: 0.0207 - acc: 0.9969
Epoch 69/100
60000/60000 [==============================] - 2s - loss: 0.0199 - acc: 0.9971
Epoch 70/100
60000/60000 [==============================] - 2s - loss: 0.0164 - acc: 0.9975
Epoch 71/100
60000/60000 [==============================] - 2s - loss: 0.0233 - acc: 0.9968
Epoch 72/100
60000/60000 [==============================] - 2s - loss: 0.0202 - acc: 0.9969
Epoch 73/100
60000/60000 [==============================] - 2s - loss: 0.0209 - acc: 0.9969
Epoch 74/100
60000/60000 [==============================] - 2s - loss: 0.0219 - acc: 0.9969
Epoch 75/100
60000/60000 [==============================] - 2s - loss: 0.0206 - acc: 0.9971
Epoch 76/100
60000/60000 [==============================] - 2s - loss: 0.0224 - acc: 0.9969
Epoch 77/100
60000/60000 [==============================] - 2s - loss: 0.0203 - acc: 0.9973
Epoch 78/100
60000/60000 [==============================] - 2s - loss: 0.0168 - acc: 0.9974
Epoch 79/100
60000/60000 [==============================] - 2s - loss: 0.0201 - acc: 0.9971
Epoch 80/100
60000/60000 [==============================] - 2s - loss: 0.0184 - acc: 0.9974
Epoch 81/100
60000/60000 [==============================] - 2s - loss: 0.0203 - acc: 0.9972
Epoch 82/100
60000/60000 [==============================] - 2s - loss: 0.0205 - acc: 0.9973
Epoch 83/100
60000/60000 [==============================] - 2s - loss: 0.0200 - acc: 0.9977
Epoch 84/100
60000/60000 [==============================] - 2s - loss: 0.0261 - acc: 0.9966
Epoch 85/100
60000/60000 [==============================] - 2s - loss: 0.0176 - acc: 0.9974
Epoch 86/100
60000/60000 [==============================] - 2s - loss: 0.0198 - acc: 0.9974
Epoch 87/100
60000/60000 [==============================] - 2s - loss: 0.0194 - acc: 0.9974
Epoch 88/100
60000/60000 [==============================] - 2s - loss: 0.0188 - acc: 0.9974
Epoch 89/100
60000/60000 [==============================] - 2s - loss: 0.0198 - acc: 0.9972
Epoch 90/100
60000/60000 [==============================] - 2s - loss: 0.0207 - acc: 0.9972
Epoch 91/100
60000/60000 [==============================] - 2s - loss: 0.0224 - acc: 0.9971
Epoch 92/100
60000/60000 [==============================] - 2s - loss: 0.0192 - acc: 0.9975
Epoch 93/100
60000/60000 [==============================] - 2s - loss: 0.0223 - acc: 0.9971
Epoch 94/100
60000/60000 [==============================] - 2s - loss: 0.0197 - acc: 0.9974
Epoch 95/100
60000/60000 [==============================] - 2s - loss: 0.0193 - acc: 0.9973
Epoch 96/100
60000/60000 [==============================] - 2s - loss: 0.0190 - acc: 0.9973
Epoch 97/100
60000/60000 [==============================] - 2s - loss: 0.0229 - acc: 0.9970
Epoch 98/100
60000/60000 [==============================] - 2s - loss: 0.0212 - acc: 0.9973
Epoch 99/100
60000/60000 [==============================] - 2s - loss: 0.0222 - acc: 0.9969
Epoch 100/100
60000/60000 [==============================] - 2s - loss: 0.0208 - acc: 0.9972
evaluation = model.evaluate(X_test, Y_test, verbose=1)
print('Summary: Loss over the test dataset: %.2f, Accuracy: %.2f' % (evaluation[0], evaluation[1]))
9984/10000 [============================>.] - ETA: 0sSummary: Loss over the test dataset: 0.17, Accuracy: 0.98