Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@m__k

KerasでMNISTのメモ

More than 1 year has passed since last update.

以下の写経以外何もしてません。
- http://aidiary.hatenablog.com/entry/20161109/1478696865
- https://qiita.com/PinappleHunter/items/e4ddf4a0062726af08c8#%E3%83%87%E3%83%BC%E3%82%BF%E3%82%92%E4%BD%BF%E3%81%88%E3%82%8B%E5%BD%A2%E3%81%AB%E5%A4%89%E6%8F%9B%E3%81%99%E3%82%8B

KerasのDocumentation
- https://keras.io/ja/

準備

裏でTensorflowを使っているので、Tensorflowを入れる

pip install tensorflow

Kerasのインストール

pip install keras

上記サイトでモデルの構造を画像で保存する機能を使っているので、その準備

brew install graphviz
pip install pydot

実装の写経

ただ上記を真似しただけ。

# データセット準備
from keras.datasets import mnist
from keras.utils import np_utils

(x_train, y_train), (x_test, y_test) = mnist.load_data()

 # インプットの形式変換
x_train = x_train.reshape(len(x_train),784)
x_test = x_test.reshape(len(x_test), 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")

nb_classes = 10

y_train = np_utils.to_categorical(y_train,nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)


# モデル定義
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import Adam

def build_multilayer_perceptron():
    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"))
    return model

model = build_multilayer_perceptron()

# modelのsummaryを表示&画像でも表示
from keras.utils.vis_utils import plot_model

model.summary()
plot_model(model, show_shapes=True, show_layer_names=True, to_file="model.png")

# model構築
model.compile(loss="categorical_crossentropy", optimizer=Adam(), metrics=["accuracy"])

# 学習
from keras.callbacks import EarlyStopping

early_stopping = EarlyStopping(patience=0, verbose=1)
history = model.fit(x_train, y_train, batch_size=32, epochs=100, verbose=1, validation_split=0.1, callbacks=[early_stopping])

# 予測
model.evaluate(x_test, y_test)
# [0.07958280239470332, 0.979]
2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
m__k
数学専攻(確率解析)→SE→ITコンサル→事業会社でデータサイエンティスト 実装重視で初学者にもわかりやすくをモットーにしています。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?