Kerasã«ãã2ã¯ã©ã¹ãã¸ã¹ãã£ãã¯å帰
ã¾ãã¯ãã£ã¨ãç°¡åãª2ã¯ã©ã¹ãã¸ã¹ãã£ãã¯å帰ã¢ãã«ãKerasã§æ¸ãã¦ã¿ããåã«Theanoã§ãã£ãï¼2015/5/19ï¼ã®ãKerasã§æ¸ãæããã ãããã¸ã¹ãã£ãã¯å帰ã¯ãå帰ã¨ã¤ããã©åé¡ã®ã¢ã«ã´ãªãºã ã§ãé ã層ããªããæ´»æ§åé¢æ°ã«ã·ã°ã¢ã¤ãé¢æ°ã使ã£ããã¥ã¼ã©ã«ãããã¨ãã¦ã¢ãã«åã§ããã
ãã¼ã¿ã¯ãPRMLã®4ç« ã®ex2data1 ã使ãã1åç®ã¨2åç®ããã¼ã¿ã§3åç®ãã¯ã©ã¹ã©ãã«ï¼0ã¾ãã¯1ã®2ã¯ã©ã¹ï¼ã
ã½ã¼ã¹ã³ã¼ãï¼ex2data1.py
ãã¼ã¿ã®ãã¼ãã¨æ£è¦å
ãã¼ã¿ãèªã¿è¾¼ãã©ã¤ãã©ãªã«ã¯pandasãªã©ãããããããã§ã¯numpy.genfromtxt()
ã使ããX
ããã¼ã¿ã§äºæ¬¡å
ãã¼ã¿ã®ãªã¹ããt
ãã©ãã«ã§0ã¾ãã¯1ã®ãªã¹ãã
# load training data data = np.genfromtxt(os.path.join('data', 'ex2data1.txt'), delimiter=',') X = data[:, (0, 1)] t = data[:, 2]
ãã¼ã¿ã®ååã®å¹³åã0ãæ¨æºåå·®ã1ã«ãªãããã«ãã¼ã¿ãæ£è¦åããããã®æ£è¦åãããªãã¨å¦ç¿ãã¾ã£ããé²ã¾ãªãï¼=lossãå°ãããªããªãï¼ãã¨ãå¤ãã£ãã®ã§ãã£ãæ¹ããããããsklearn.preprocessing
ã¢ã¸ã¥ã¼ã«ã«æ£è¦åã®ã¡ã½ãããããã®ã§ä½¿ãã
from sklearn import preprocessing # normalize data X = preprocessing.scale(X)
以ä¸ã®ã³ã¼ãã§ãã¼ã¿ã®ååãå¹³å0ãæ¨æºåå·®1ã«æ£è¦åããããã¨ã確èªã§ããããã¼ã¿ã®axis=0
ï¼è¡æ¹åï¼ã«å¹³åã¨æ¨æºåå·®ãæ±ãã¦ãããå¹³åã¯0ã£ã½ããªãããe-17ãªã®ã§éããªã0ã«è¿ãã
print(np.mean(X, axis=0)) print(np.std(X, axis=0))
[ -7.66053887e-17 1.11022302e-15] [ 1. 1.]
ãã¼ã¿ã®å¯è¦å
ã©ã®ãããªãã¼ã¿ãmatplotlibã§å¯è¦åããã
import matplotlib.pyplot as plt def plot_data(X, t): positive = [i for i in range(len(t)) if t[i] == 1] negative = [i for i in range(len(t)) if t[i] == 0] plt.scatter(X[positive, 0], X[positive, 1], c='red', marker='o', label='positive') plt.scatter(X[negative, 0], X[negative, 1], c='blue', marker='o', label='negative') # plot training data plt.figure(1) plot_data(X, t)
赤ãæ£ä¾ï¼positiveï¼ã§éãè² ä¾ï¼negativeï¼ããã®2ã¯ã©ã¹ãç´ç·ã§åé¡ããã®ãç®æ¨ã
ãã¸ã¹ãã£ãã¯å帰ã¢ãã«ã®æ§ç¯
ãã¸ã¹ãã£ãã¯å帰ã¢ãã«ãçµã¿ç«ã¦ãã空ã®Sequentialã¢ãã«ãä½æããããã«ã¬ã¤ã¤ï¼Denseï¼ãæ´»æ§åé¢æ°ï¼Activationï¼ãé çªã«è¿½å ããæå¾ã«ã³ã³ãã¤ã«ãããSequentialã¢ãã«ã¨ããã®ã¯é常ã®ãã¥ã¼ã©ã«ãããã®ããã«å±¤ãç©ã¿éããã¢ãã«ãæããã³ã³ãã¤ã«æã«æé©åææ³ï¼optimizerï¼ãæ失é¢æ°ï¼lossï¼ãè©ä¾¡ææ¨ï¼metricsï¼ãæå®ããã
# create the logistic regression model model = Sequential() model.add(Dense(1, input_shape=(2, ))) model.add(Activation('sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- å ¥åãã¼ã¿ã¯2次å ãåºåã¯1次å
- ãã®ã¿ã¹ã¯ã¯0ã¾ãã¯1ãäºæ¸¬ããäºå¤åé¡ãªã®ã§æ失é¢æ°ã«ã¯
binary_crossentropy
- ãã¨ã§å®é¨ãããMNISTã®ãããªå¤å¤åé¡ã§ã¯
categorical_crossentropy
- æé©åã¢ã«ã´ãªãºã ã«ã¯Adamãè©ä¾¡ææ¨ã«ã¯ç²¾åº¦ãç¨ãããããã¯è¨ç·´ãã¼ã¿ã§ç²¾åº¦æ±ãã¦ããã®ããªï¼è¦æ¤è¨¼
Kerasã®å±¤ï¼layerï¼ã¯ãä¸è¬çãªéã¿ããã層ã ãã§ãªããæ´»æ§åé¢æ°ã層ã¨ã¿ãªãã¦ãããä¸ã®å®è¡çµæãããããããã«Activation
ãkeras.layers
ã«å«ã¾ãã¦ããã
print(model.layers[0]) print(model.layers[1])
<keras.layers.core.Dense object at 0x7f556811b0b8> <keras.layers.core.Activation object at 0x7f556816a5c0>
modelã®çµã¿ç«ã¦æ¹ã¯ä¸ã®æ¹æ³ãã¹ã¿ã³ãã¼ãã ããã©ãFunctional APIã使ãã¨ãã£ã¨æè»ã«ã¢ãã«ãä½ããããã ããã¨ã§ãã®APIã使ã£ãæ¸ãæ¹ããã¦ã¿ããã
è¨ç·´
ã¢ãã«ã®è¨ç·´ã¯scikit-learnã¨åããfit()
ã¨ããé¢æ°ã«ãã¼ã¿ã¨ã©ãã«ã渡ãã°ããã
# fit the model model.fit(X, t, nb_epoch=1000, batch_size=5, verbose=1)
- fit()ã«ã¯ãåºå®ã®ã¨ããã¯æ°ï¼nb_epochï¼ãããããµã¤ãºï¼batch_sizeï¼ãçµéåºåæ¹æ³ï¼verboseï¼ãæå®ããã
Epoch 1/1000 100/100 [==============================] - 0s - loss: 0.6531 - acc: 0.6600 Epoch 2/1000 100/100 [==============================] - 0s - loss: 0.6455 - acc: 0.6700 Epoch 3/1000 100/100 [==============================] - 0s - loss: 0.6385 - acc: 0.6700 Epoch 4/1000 100/100 [==============================] - 0s - loss: 0.6310 - acc: 0.6800 Epoch 5/1000 100/100 [==============================] - 0s - loss: 0.6243 - acc: 0.6800
verbose=1
ã«ãã¦ããã¨å¦ç¿çµéãæ£ã°ã©ãã§è¡¨ç¤ºãã¦ãããã®ã§é常ã«ä¾¿å©ï¼- ãã¨ã§ç´¹ä»ããEarly-stoppingã使ãã¨åºå®ã¨ããã¯ã ãã«ã¼ããåãã®ã§ã¯ãªããåæå¤å®ãã¦æ¢ãã¦ãããããã«ãªãã
å¦ç¿ããéã¿ã®åå¾
model.layers
ãªã¹ãã«å層ã®æ
å ±ãæ ¼ç´ããã¦ããã好ããªå±¤ãæå®ãã¦get_weights()
ã§éã¿ãåå¾ã§ãããéã¿ãå¤æ¬¡å
ãªã¹ããªã®ã§ãããããããæåã®æ¬¡å
ã¯ã[0]ãéã¿ã[1]ããã¤ã¢ã¹é
ã表ãããã ãã¢ãã«ããã¯éã¿ã ãã§ãªããåºåãåãåºããã®ã§ãã¨ã§ã¾ã¨ãã¦ãããã
# get the learned weight weights = model.layers[0].get_weights() w1 = weights[0][0, 0] w2 = weights[0][1, 0] b = weights[1][0]
ããã§ã¯ã決å®å¢çãæç»ãããã®ã§å¦ç¿ããéã¿w1ãw2ã¨ãã¤ã¢ã¹bãåå¾ããã
決å®å¢çã®æç»
ãã¸ã¹ãã£ãã¯å帰ã®æ±ºå®å¢çã®æ¡ä»¶ã¯
ãªã®ã§ [x1, x2] å¹³é¢ä¸ã§ã®ç´ç·ã®æ¹ç¨å¼ã«ç´ãã¨
ã¨ãªãããã®ç´ç·ãmatplotlibã§å ã»ã©ã®ã°ã©ãã«è¿½è¨ããã
# draw decision boundary plt.figure(1) xmin, xmax = min(X[:, 0]), max(X[:, 0]) ymin, ymax = min(X[:, 1]), max(X[:, 1]) xs = np.linspace(xmin, xmax, 100) ys = [- (w1 / w2) * x - (b / w2) for x in xs] plt.plot(xs, ys, 'b-', label='decision boundary') plt.xlabel('x1') plt.ylabel('x2') plt.xlim(xmin, xmax) plt.ylim(ymin, ymax) plt.legend() plt.show()
çµæã¯ã
ã¨ãªããåé¡ããç´ç·ãå¦ç¿ã§ãã¦ãããã¨ããããã ä»åã¯ããã¹ããã¼ã¿ã使ã£ãè©ä¾¡ãªã©ã¯è¡ã£ã¦ããªãã