Last active
November 15, 2017 11:42
Keras mnist sample 1
This file contains hidden or 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
import keras | |
from keras.datasets import mnist | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, InputLayer | |
from keras.optimizers import RMSprop | |
# MNISTデータを読込む | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
# MNISTデータを加工する | |
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 | |
y_train = keras.utils.to_categorical(y_train, 10) | |
y_test = keras.utils.to_categorical(y_test, 10) | |
# モデルの構築 | |
model = Sequential() | |
model.add(InputLayer(input_shape=(784,))) | |
model.add(Dense(10, activation='softmax')) | |
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) | |
# 学習 | |
epochs = 20 | |
batch_size = 128 | |
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) | |
# 検証 | |
score = model.evaluate(x_test, y_test, verbose=1) | |
print() | |
print('Test loss:', score[0]) | |
print('Test accuracy:', score[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment