Created
November 26, 2016 22:57
-
-
Save tomthetrainer/f76ee825f0b128a5250552720753bf0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Trains a simple deep NN on the MNIST dataset. | |
Gets to 98.40% test accuracy after 20 epochs | |
(there is *a lot* of margin for parameter tuning). | |
2 seconds per epoch on a K520 GPU. | |
''' | |
from __future__ import print_function | |
import numpy as np | |
import tensorflow as tf | |
tf.python.control_flow_ops = tf | |
np.random.seed(1337) # for reproducibility | |
from keras.datasets import mnist | |
from keras.models import Sequential | |
from keras.layers.core import Dense, Dropout, Activation | |
from keras.optimizers import SGD, Adam, RMSprop | |
from keras.utils import np_utils | |
batch_size = 128 | |
nb_classes = 10 | |
nb_epoch = 20 | |
# the data, shuffled and split between train and test sets | |
(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 | |
print(X_train.shape[0], 'train samples') | |
print(X_test.shape[0], 'test samples') | |
# convert class vectors to binary class matrices | |
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(512, input_shape=(784,))) | |
model.add(Activation('relu')) | |
model.add(Dropout(0.2)) | |
model.add(Dense(512)) | |
model.add(Activation('relu')) | |
model.add(Dropout(0.2)) | |
model.add(Dense(10)) | |
model.add(Activation('softmax')) | |
model.summary() | |
model.compile(loss='categorical_crossentropy', | |
optimizer=RMSprop(), | |
metrics=['accuracy']) | |
history = model.fit(X_train, Y_train, | |
batch_size=batch_size, nb_epoch=nb_epoch, | |
verbose=1, validation_data=(X_test, Y_test)) | |
score = model.evaluate(X_test, Y_test, verbose=0) | |
print('Test score:', score[0]) | |
print('Test accuracy:', score[1]) | |
model.save_weights('model_save') | |
#model.save_model('sm2') ## does not work | |
json_string = model.to_json() | |
text_file = open("keras_model", "w") | |
text_file.write(json_string) | |
text_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment