Last active
July 22, 2017 16:42
-
-
Save adash333/d18e78840cd96156bb672723133f1434 to your computer and use it in GitHub Desktop.
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
# 3.6 perceptron, Keras, graph2 | |
import numpy as np | |
from keras.models import Sequential | |
from keras.layers import Dense, Activation | |
from keras.optimizers import SGD | |
np.random.seed(123) | |
''' | |
1.データの生成 | |
''' | |
X = np.array([[0,0], [0,1], [1,0], [1,1]]) | |
Y = np.array([[0], [1], [1], [0]]) | |
''' | |
2.モデル設定 | |
''' | |
model = Sequential() | |
# 入力層 - 隠れ層 | |
model.add(Dense(input_dim=2, units=2)) | |
model.add(Activation('sigmoid')) | |
# 隠れ層 - 出力層 | |
model.add(Dense(units=1)) | |
model.add(Activation('sigmoid')) | |
model.compile(loss='binary_crossentropy', optimizer=SGD(lr=0.1)) | |
''' | |
3.モデル学習 | |
''' | |
model.fit(X, Y, epochs=500, batch_size=4) | |
''' | |
4.学習結果の確認 | |
''' | |
classes = model.predict_classes(X, batch_size=4) | |
prob = model.predict_proba(X, batch_size=4) | |
print('classified:') | |
print(Y == classes) | |
print() | |
print('output probability:') | |
print(prob) | |
''' | |
5. モデルのグラフ化(pngファイルで保存する場合)(Windowsでは設定に要注意) | |
参考:http://ni4muraano.hatenablog.com/entry/2017/02/09/063000 | |
''' | |
from keras.utils import plot_model | |
plot_model(model, to_file='model36.png') | |
''' | |
(jupyter notebook内に表示する場合は以下のように記載) | |
from IPython.display import SVG | |
from keras.utils.vis_utils import model_to_dot | |
SVG(model_to_dot(model).create(prog='dot', format='svg')) | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment