Created
June 11, 2019 08:03
Star
You must be signed in to star a gist
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
# from https://weblabo.oscasierra.net/python/keras-mnist-sample.html | |
import keras | |
from keras.datasets import mnist | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, InputLayer | |
from keras.optimizers import RMSprop | |
# read MNIST | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
# prepare data | |
x_train = x_train.reshape(60000, 784) | |
x_test = x_test.reshape(10000, 784) | |
x_train = x_train.astype('float32') / 255 | |
x_test = x_test.astype('float32') / 255 | |
y_train = keras.utils.to_categorical(y_train, 10) | |
y_test = keras.utils.to_categorical(y_test, 10) | |
# build models | |
model = Sequential() | |
model.add(InputLayer(input_shape=(784,))) | |
model.add(Dense(10, activation='softmax')) | |
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) | |
# train | |
epochs = 20 | |
batch_size = 120 | |
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) | |
# proof | |
score = model.evaluate(x_test, y_test, verbose=1) | |
print() | |
print(f"Test loss: {score[0]}") | |
print(f"Test accuracy: {score[1]}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment