1'''Trains a simple deep NN on the MNIST dataset. 2 3Gets to 98.40% test accuracy after 20 epochs 4(there is *a lot* of margin for parameter tuning). 52 seconds per epoch on a K520 GPU. 6''' 7 8from __future__ import print_function 9 10import keras 11from keras.datasets import mnist 12from keras.models import Sequential 13from keras.layers import Dense, Dropout 14from keras.optimizers import RMSprop 15 16batch_size = 128 17num_classes = 10 18epochs = 20 19 20# the data, split between train and test sets 21(x_train, y_train), (x_test, y_test) = mnist.load_data() 22 23x_train = x_train.reshape(60000, 784) 24x_test = x_test.reshape(10000, 784) 25x_train = x_train.astype('float32') 26x_test = x_test.astype('float32') 27x_train /= 255 28x_test /= 255 29print(x_train.shape[0], 'train samples') 30print(x_test.shape[0], 'test samples') 31 32# convert class vectors to binary class matrices 33y_train = keras.utils.to_categorical(y_train, num_classes) 34y_test = keras.utils.to_categorical(y_test, num_classes) 35 36model = Sequential() 37model.add(Dense(512, activation='relu', input_shape=(784,))) 38model.add(Dropout(0.2)) 39model.add(Dense(512, activation='relu')) 40model.add(Dropout(0.2)) 41model.add(Dense(num_classes, activation='softmax')) 42 43model.summary() 44 45model.compile(loss='categorical_crossentropy', 46 optimizer=RMSprop(), 47 metrics=['accuracy']) 48 49history = model.fit(x_train, y_train, 50 batch_size=batch_size, 51 epochs=epochs, 52 verbose=1, 53 validation_data=(x_test, y_test)) 54score = model.evaluate(x_test, y_test, verbose=0) 55print('Test loss:', score[0]) 56print('Test accuracy:', score[1]) 57