keras/examples/mnist_mlp.py

58 lines
1.6 KiB
Python
Raw Normal View History

2016-03-19 16:07:15 +00:00
'''Trains a simple deep NN on the MNIST dataset.
2015-12-09 02:49:14 +00:00
2016-03-19 16:07:15 +00:00
Gets to 98.40% test accuracy after 20 epochs
2015-12-09 02:49:14 +00:00
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
'''
2015-04-30 00:17:22 +00:00
from __future__ import print_function
import keras
2015-04-30 00:17:22 +00:00
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
2015-04-30 00:17:22 +00:00
2015-06-19 19:52:43 +00:00
batch_size = 128
2017-02-11 02:16:11 +00:00
num_classes = 10
2017-02-15 00:08:30 +00:00
epochs = 20
2015-04-30 00:17:22 +00:00
2016-04-04 18:30:24 +00:00
# the data, shuffled and split between train and test sets
2017-02-11 02:16:11 +00:00
(x_train, y_train), (x_test, y_test) = mnist.load_data()
2015-05-04 17:31:03 +00:00
2017-02-11 02:16:11 +00:00
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')
2015-04-30 00:17:22 +00:00
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
2015-04-30 00:17:22 +00:00
model = Sequential()
2017-02-11 02:16:11 +00:00
model.add(Dense(512, activation='relu', input_shape=(784,)))
2015-05-04 17:31:03 +00:00
model.add(Dropout(0.2))
2017-02-11 02:16:11 +00:00
model.add(Dense(512, activation='relu'))
2015-05-04 17:31:03 +00:00
model.add(Dropout(0.2))
2017-02-11 02:16:11 +00:00
model.add(Dense(10, activation='softmax'))
2015-04-30 00:17:22 +00:00
2016-03-19 16:07:15 +00:00
model.summary()
2015-04-30 00:17:22 +00:00
2016-03-19 16:07:15 +00:00
model.compile(loss='categorical_crossentropy',
2016-04-10 12:38:00 +00:00
optimizer=RMSprop(),
2016-03-19 16:07:15 +00:00
metrics=['accuracy'])
2017-02-11 02:16:11 +00:00
history = model.fit(x_train, y_train,
2017-03-26 14:27:49 +00:00
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
2017-02-11 02:16:11 +00:00
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
2015-05-04 17:31:03 +00:00
print('Test accuracy:', score[1])