Created
October 9, 2017 14:30
-
-
Save Synashida/7da8a28bf58b374d6b1e3d9b48acafe1 to your computer and use it in GitHub Desktop.
何回も挫折した人の初めてのKeras ref: http://qiita.com/Syn256/items/3dff2a7fe40b351d70ee
This file contains 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
python load.py | |
Using TensorFlow backend. | |
32/280 [==>...........................] - ETA: 0s予測:1,実際1,True | |
予測:1,実際1,True | |
予測:1,実際1,True | |
: | |
: | |
: | |
予測:2,実際2,True | |
予測:2,実際0,False | |
正解率78.92857142857143% (検証日数 280日) | |
雨の正解率 = 0.0% | |
晴れの正解率 = 84.21052631578947% | |
曇の正解率 = 91.55844155844156% | |
雪の正解率 = 0% |
This file contains 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 data | |
import numpy as np | |
from keras.models import Sequential | |
from keras.layers import Activation, Dense | |
from keras.optimizers import SGD | |
# 教師データの読み込み | |
X = data.x_train | |
Y = data.y_train | |
model = Sequential() | |
# GPUがない環境でやっているので、多層だと時間が足りないため、1層で処理 | |
# Denseの第一引数は出力の次元数 (数を4096や2048、データ総数だった7670などを指定してみた結果、5120が比較的安定した結果になったので、5120にした。チューニングできるポイント) | |
# input_dimは1日の気象データの列数。ここは自作データの列数と同じ値を指定すること | |
model.add(Dense(5120, input_dim=7)) | |
model.add(Activation('relu')) | |
model.add(Activation('softmax')) | |
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy']) | |
# あんまりちゃんと理解してないので細かい解説ができない | |
from keras.callbacks import ModelCheckpoint | |
check = ModelCheckpoint("model.hdf5") | |
# epochsを20 / 10 / 5 で試してみたところ、10にした時の8回目あたりがピークだったので | |
# epochesを8にした。 | |
# 結果こんな : 7550/7670 [============================>.] - ETA: 0s[2.2502278308875128, 0.77874837114416173] | |
model.fit(X, Y, epochs=8, batch_size=1, verbose=1) | |
# モデルを保存 | |
model.save("model.h5") | |
# ここは蛇足。一応学習結果の確認をしています。 | |
model.metrics_names | |
print(model.evaluate(X, Y, batch_size=10)) |
This file contains 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
from keras.models import load_model | |
import evaluate_data | |
import numpy as np | |
model = load_model('model.h5') | |
eX = evaluate_data.eX | |
eY = evaluate_data.eY | |
#学習結果の確認 | |
ret = model.predict_classes(eX) | |
collectCount = 0 | |
# ラベル毎の正解の保存 | |
collects = [[0, 0],[0, 0],[0, 0], [0, 0]] | |
for idx, val in enumerate(ret): | |
print("予測:{0},実際{1},{2}".format(val, eY[idx], val == eY[idx])) | |
collectCount = collectCount + (1 if val == eY[idx] else 0) | |
collects[eY[idx]][0] = collects[eY[idx]][0] + 1 | |
if val == eY[idx]: | |
collects[eY[idx]][1] = collects[eY[idx]][1] + 1 | |
print("正解率{0}% (検証日数 {1}日)".format(collectCount/len(ret)*100, len(ret))) | |
print("雨の正解率 = {0}%, 晴れの正解率 = {1}%, 曇の正解率 = {2}%, 雪の正解率 = {3}%".format( | |
collects[0][1] / collects[0][0] * 100 if collects[0][0] > 0 else 0, | |
collects[1][1] / collects[1][0] * 100 if collects[1][0] > 0 else 0, | |
collects[2][1] / collects[2][0] * 100 if collects[2][0] > 0 else 0, | |
collects[3][1] / collects[3][0] * 100 if collects[3][0] > 0 else 0 | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment