Skip to content

Instantly share code, notes, and snippets.

@Kcrong
Last active January 31, 2018 15:50
Show Gist options
  • Save Kcrong/c60bae5fefe9fcb8d52d8439cefe2659 to your computer and use it in GitHub Desktop.
Save Kcrong/c60bae5fefe9fcb8d52d8439cefe2659 to your computer and use it in GitHub Desktop.
from keras.utils import np_utils
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Activation
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 784).astype('float32') / 255.0
x_test = x_test.reshape(x_test.shape[0], 784).astype('float32') / 255.0
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
model = Sequential()
model.add(Dense(units=64, input_dim=28*28, activation='relu'))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
hist = model.fit(x_train, y_train, epochs=5, batch_size=32, callbacks=[EarlyStopping(patience = 20)])
loss, acc = model.evaluate(x_test, y_test, batch_size=32)
print('loss: ', loss)
print('acc: ', acc*100, '%')
x_test_set = x_test[0:1]
result = model.predict(x_test_set)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment