LoginSignup
0

More than 5 years have passed since last update.

Kerasで1桁の足し算

Last updated at Posted at 2018-11-13

「無から始めるKeras 第1回」
https://qiita.com/Ishotihadus/items/c2f864c0cde3d17b7efb

を参考に、0-5の乱数同士を足し合わせる、1桁の足し算を作ってみた。

hoge.py
import numpy as np
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation

# 足し算の問題と答えの配列作成
x_train = np.random.randint(0, 5, (1000, 2))
y_train = np_utils.to_categorical(np.sum(x_train, axis=1))

# model作って学習
model = Sequential()
model.add(Dense(20, input_dim=2))
model.add(Dense(9, activation='softmax'))
model.compile('rmsprop', 'categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, nb_epoch=300, validation_split=0.2)

# テストデータを作成して検証
x_test = np.random.randint(0, 5, (100, 2))
predict = np.argmax(model.predict(x_test), axis=1)
y_test = np.sum(x_test, axis=1)
print(sum(predict == y_test) / len(x_test))

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0