Feedforward Neural Network (Multilayer Perceptron)

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.datasets import mnist
from keras.utils import np_utils
Using TensorFlow backend.
batch_size = 128
nb_classes = 10
nb_epoch = 100
# Load MNIST dataset
(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)
# Multilayer Perceptron model
model = Sequential()
model.add(Dense(output_dim=625, input_dim=784, init='normal', activation='sigmoid'))
model.add(Dense(output_dim=625, input_dim=625, init='normal', activation='sigmoid'))
model.add(Dense(output_dim=10, input_dim=625, init='normal', activation='softmax'))
model.compile(optimizer=SGD(lr=0.05), 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]              
____________________________________________________________________________________________________
dense_2 (Dense)                  (None, 625)           391250      dense_1[0][0]                    
____________________________________________________________________________________________________
dense_3 (Dense)                  (None, 10)            6260        dense_2[0][0]                    
====================================================================================================
Total params: 888135
____________________________________________________________________________________________________
# Train
history = model.fit(X_train, Y_Train, nb_epoch=nb_epoch, batch_size=batch_size, verbose=1)
Epoch 1/100
60000/60000 [==============================] - 3s - loss: 1.9330 - acc: 0.4130     
Epoch 2/100
60000/60000 [==============================] - 2s - loss: 0.9534 - acc: 0.7734     
Epoch 3/100
60000/60000 [==============================] - 2s - loss: 0.5957 - acc: 0.8447     
Epoch 4/100
60000/60000 [==============================] - 2s - loss: 0.4765 - acc: 0.8705     
Epoch 5/100
60000/60000 [==============================] - 2s - loss: 0.4196 - acc: 0.8835     
Epoch 6/100
60000/60000 [==============================] - 2s - loss: 0.3873 - acc: 0.8908     
Epoch 7/100
60000/60000 [==============================] - 2s - loss: 0.3666 - acc: 0.8950     
Epoch 8/100
60000/60000 [==============================] - 2s - loss: 0.3509 - acc: 0.8988     
Epoch 9/100
60000/60000 [==============================] - 2s - loss: 0.3393 - acc: 0.9023     
Epoch 10/100
60000/60000 [==============================] - 2s - loss: 0.3299 - acc: 0.9046     
Epoch 11/100
60000/60000 [==============================] - 2s - loss: 0.3222 - acc: 0.9063     
Epoch 12/100
60000/60000 [==============================] - 2s - loss: 0.3157 - acc: 0.9080     
Epoch 13/100
60000/60000 [==============================] - 2s - loss: 0.3101 - acc: 0.9099     
Epoch 14/100
60000/60000 [==============================] - 2s - loss: 0.3052 - acc: 0.9113     
Epoch 15/100
60000/60000 [==============================] - 2s - loss: 0.3006 - acc: 0.9130     
Epoch 16/100
60000/60000 [==============================] - 2s - loss: 0.2969 - acc: 0.9138     
Epoch 17/100
60000/60000 [==============================] - 2s - loss: 0.2927 - acc: 0.9141     
Epoch 18/100
60000/60000 [==============================] - 2s - loss: 0.2891 - acc: 0.9164     
Epoch 19/100
60000/60000 [==============================] - 2s - loss: 0.2860 - acc: 0.9171     
Epoch 20/100
60000/60000 [==============================] - 2s - loss: 0.2829 - acc: 0.9178     
Epoch 21/100
60000/60000 [==============================] - 2s - loss: 0.2796 - acc: 0.9183     
Epoch 22/100
60000/60000 [==============================] - 2s - loss: 0.2768 - acc: 0.9199     
Epoch 23/100
60000/60000 [==============================] - 2s - loss: 0.2739 - acc: 0.9205     
Epoch 24/100
60000/60000 [==============================] - 2s - loss: 0.2713 - acc: 0.9216     
Epoch 25/100
60000/60000 [==============================] - 2s - loss: 0.2686 - acc: 0.9222     
Epoch 26/100
60000/60000 [==============================] - 2s - loss: 0.2659 - acc: 0.9230     
Epoch 27/100
60000/60000 [==============================] - 2s - loss: 0.2633 - acc: 0.9235     
Epoch 28/100
60000/60000 [==============================] - 2s - loss: 0.2608 - acc: 0.9247     
Epoch 29/100
60000/60000 [==============================] - 2s - loss: 0.2579 - acc: 0.9259     
Epoch 30/100
60000/60000 [==============================] - 2s - loss: 0.2556 - acc: 0.9264     
Epoch 31/100
60000/60000 [==============================] - 2s - loss: 0.2527 - acc: 0.9275     
Epoch 32/100
60000/60000 [==============================] - 2s - loss: 0.2501 - acc: 0.9277     
Epoch 33/100
60000/60000 [==============================] - 2s - loss: 0.2474 - acc: 0.9288     
Epoch 34/100
60000/60000 [==============================] - 2s - loss: 0.2448 - acc: 0.9298     
Epoch 35/100
60000/60000 [==============================] - 2s - loss: 0.2423 - acc: 0.9295     
Epoch 36/100
60000/60000 [==============================] - 2s - loss: 0.2396 - acc: 0.9313     
Epoch 37/100
60000/60000 [==============================] - 2s - loss: 0.2372 - acc: 0.9316     
Epoch 38/100
60000/60000 [==============================] - 2s - loss: 0.2339 - acc: 0.9330     
Epoch 39/100
60000/60000 [==============================] - 2s - loss: 0.2312 - acc: 0.9338     
Epoch 40/100
60000/60000 [==============================] - 2s - loss: 0.2284 - acc: 0.9348     
Epoch 41/100
60000/60000 [==============================] - 2s - loss: 0.2257 - acc: 0.9355     
Epoch 42/100
60000/60000 [==============================] - 2s - loss: 0.2229 - acc: 0.9360     
Epoch 43/100
60000/60000 [==============================] - 2s - loss: 0.2202 - acc: 0.9372     
Epoch 44/100
60000/60000 [==============================] - 2s - loss: 0.2175 - acc: 0.9383     
Epoch 45/100
60000/60000 [==============================] - 2s - loss: 0.2152 - acc: 0.9384     
Epoch 46/100
60000/60000 [==============================] - 2s - loss: 0.2121 - acc: 0.9395     
Epoch 47/100
60000/60000 [==============================] - 2s - loss: 0.2097 - acc: 0.9403     
Epoch 48/100
60000/60000 [==============================] - 2s - loss: 0.2071 - acc: 0.9410     
Epoch 49/100
60000/60000 [==============================] - 2s - loss: 0.2046 - acc: 0.9418     
Epoch 50/100
60000/60000 [==============================] - 2s - loss: 0.2017 - acc: 0.9425     
Epoch 51/100
60000/60000 [==============================] - 2s - loss: 0.1992 - acc: 0.9436     
Epoch 52/100
60000/60000 [==============================] - 2s - loss: 0.1965 - acc: 0.9443     
Epoch 53/100
60000/60000 [==============================] - 2s - loss: 0.1937 - acc: 0.9446     
Epoch 54/100
60000/60000 [==============================] - 2s - loss: 0.1916 - acc: 0.9460     
Epoch 55/100
60000/60000 [==============================] - 2s - loss: 0.1892 - acc: 0.9458     
Epoch 56/100
60000/60000 [==============================] - 2s - loss: 0.1864 - acc: 0.9469     
Epoch 57/100
60000/60000 [==============================] - 2s - loss: 0.1842 - acc: 0.9478     
Epoch 58/100
60000/60000 [==============================] - 2s - loss: 0.1819 - acc: 0.9480     
Epoch 59/100
60000/60000 [==============================] - 2s - loss: 0.1795 - acc: 0.9487     
Epoch 60/100
60000/60000 [==============================] - 2s - loss: 0.1768 - acc: 0.9497     
Epoch 61/100
60000/60000 [==============================] - 2s - loss: 0.1748 - acc: 0.9501     
Epoch 62/100
60000/60000 [==============================] - 2s - loss: 0.1725 - acc: 0.9509     
Epoch 63/100
60000/60000 [==============================] - 2s - loss: 0.1704 - acc: 0.9512     
Epoch 64/100
60000/60000 [==============================] - 2s - loss: 0.1684 - acc: 0.9519     
Epoch 65/100
60000/60000 [==============================] - 2s - loss: 0.1662 - acc: 0.9526     
Epoch 66/100
60000/60000 [==============================] - 2s - loss: 0.1637 - acc: 0.9530     
Epoch 67/100
60000/60000 [==============================] - 2s - loss: 0.1621 - acc: 0.9542     
Epoch 68/100
60000/60000 [==============================] - 2s - loss: 0.1599 - acc: 0.9542     
Epoch 69/100
60000/60000 [==============================] - 2s - loss: 0.1579 - acc: 0.9546     
Epoch 70/100
60000/60000 [==============================] - 2s - loss: 0.1563 - acc: 0.9550     
Epoch 71/100
60000/60000 [==============================] - 2s - loss: 0.1543 - acc: 0.9559     
Epoch 72/100
60000/60000 [==============================] - 2s - loss: 0.1521 - acc: 0.9564     
Epoch 73/100
60000/60000 [==============================] - 2s - loss: 0.1505 - acc: 0.9570     
Epoch 74/100
60000/60000 [==============================] - 2s - loss: 0.1486 - acc: 0.9574     
Epoch 75/100
60000/60000 [==============================] - 2s - loss: 0.1467 - acc: 0.9585     
Epoch 76/100
60000/60000 [==============================] - 2s - loss: 0.1451 - acc: 0.9583     
Epoch 77/100
60000/60000 [==============================] - 2s - loss: 0.1433 - acc: 0.9587     
Epoch 78/100
60000/60000 [==============================] - 2s - loss: 0.1415 - acc: 0.9596     
Epoch 79/100
60000/60000 [==============================] - 2s - loss: 0.1400 - acc: 0.9598     
Epoch 80/100
60000/60000 [==============================] - 2s - loss: 0.1383 - acc: 0.9603     
Epoch 81/100
60000/60000 [==============================] - 2s - loss: 0.1367 - acc: 0.9607     
Epoch 82/100
60000/60000 [==============================] - 2s - loss: 0.1354 - acc: 0.9609     
Epoch 83/100
60000/60000 [==============================] - 2s - loss: 0.1335 - acc: 0.9618     
Epoch 84/100
60000/60000 [==============================] - 2s - loss: 0.1320 - acc: 0.9621     
Epoch 85/100
60000/60000 [==============================] - 2s - loss: 0.1308 - acc: 0.9629     
Epoch 86/100
60000/60000 [==============================] - 2s - loss: 0.1290 - acc: 0.9634     
Epoch 87/100
60000/60000 [==============================] - 2s - loss: 0.1275 - acc: 0.9636     
Epoch 88/100
60000/60000 [==============================] - 2s - loss: 0.1259 - acc: 0.9637     
Epoch 89/100
60000/60000 [==============================] - 2s - loss: 0.1252 - acc: 0.9642     
Epoch 90/100
60000/60000 [==============================] - 2s - loss: 0.1234 - acc: 0.9649     
Epoch 91/100
60000/60000 [==============================] - 2s - loss: 0.1222 - acc: 0.9646     
Epoch 92/100
60000/60000 [==============================] - 2s - loss: 0.1205 - acc: 0.9648     
Epoch 93/100
60000/60000 [==============================] - 2s - loss: 0.1193 - acc: 0.9657     
Epoch 94/100
60000/60000 [==============================] - 2s - loss: 0.1180 - acc: 0.9663     
Epoch 95/100
60000/60000 [==============================] - 2s - loss: 0.1170 - acc: 0.9666     
Epoch 96/100
60000/60000 [==============================] - 2s - loss: 0.1154 - acc: 0.9669     
Epoch 97/100
60000/60000 [==============================] - 2s - loss: 0.1140 - acc: 0.9675     
Epoch 98/100
60000/60000 [==============================] - 2s - loss: 0.1131 - acc: 0.9672     
Epoch 99/100
60000/60000 [==============================] - 2s - loss: 0.1120 - acc: 0.9683     
Epoch 100/100
60000/60000 [==============================] - 2s - loss: 0.1107 - acc: 0.9678     
# Evaluate
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.13, Accuracy: 0.96

results matching ""

    No results matching ""