Help us understand the problem. What is going on with this article?

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]
m__k
数学専攻(確率解析)→SE→ITコンサル 自然言語処理をメインに勉強しています。
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした