Skip to content

Instantly share code, notes, and snippets.

@rakuishi
Created February 7, 2018 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakuishi/26929121a753cf890b46e527ad4f5fdc to your computer and use it in GitHub Desktop.
Save rakuishi/26929121a753cf890b46e527ad4f5fdc to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# https://rakuishi.com/archives/getting-started-with-keras/
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Activation
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train.shape, y_train.shape, x_test.shape, y_test.shape
x_train = x_train.reshape(-1, 784) / 255
x_test = x_test.reshape(-1, 784) /255
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential() # モデルを作成
model.add(Dense(units=256, input_shape=(784,))) # 784 -> 256 に線形変換
model.add(Activation('relu')) # ReLU 関数で活性化
model.add(Dense(units=100))
model.add(Activation('relu'))
model.add(Dense(units=10)) # 最終的に 0 ~ 9 にする
model.add(Activation('softmax'))
model.compile(
loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy']
)
model.fit(
x_train, y_train,
batch_size=100, epochs=10,
validation_data=(x_test, y_test)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment