stMind

about Tech, Computer vision and Machine learning

mlpyのBasic Perceptron

今日もmlpyを試す。

perceptron.py

単純パーセプトロンのクラス。learnメソッドの中で学習を行なっている。

アルゴリズムとコードの対応を見ていくと、入力ベクトルxarrと重みにバイアスノードを加えて、0より大きい場合は1を、0以下の場合は0を出力し、誤差の平均が閾値を超えたら学習のループを抜けるようになっている。

tmp = np.where((np.dot(xarr, self._w)+self._bias)>0, 1, 0)
err = np.sum(ynew != tmp) / float(n)
if err <= self._thr:
     i = i - 1
     break

その後に、ターゲット値とのdiffと入力ベクトルとの内積に学習係数を掛けた値を、現在の重みに累積していくことで重みを学習する。

diff = ynew - tmp
self._w += self._alpha * np.dot(xarr.T, diff)
self._bias += self._alpha * np.sum(diff)

example

mlpyのチュートリアルにあるPerceptronの例を実行確認。

import numpy as np
import matplotlib.pyplot as plt
import mlpy

np.random.seed(0)
mean1, cov1, n1 = [1, 5], [[1,1],[1,2]], 200  # 200 samples of class 1
x1 = np.random.multivariate_normal(mean1, cov1, n1)
y1 = np.ones(n1, dtype=np.int)
mean2, cov2, n2 = [2.5, 2.5], [[1,0],[0,1]], 300 # 300 samples of class -1
x2 = np.random.multivariate_normal(mean2, cov2, n2)
y2 = -np.ones(n2, dtype=np.int)
x = np.concatenate((x1, x2), axis=0) # concatenate the samples
y = np.concatenate((y1, y2))

## Learning
p = mlpy.Perceptron(alpha=0.1, thr=0.05, maxiters=100) # basic perceptron
p.learn(x, y)

w = p.w()
b = p.bias()
p.err()
p.iters()
xx = np.arange(np.min(x[:,0]), np.max(x[:,0]), 0.01)
yy = - (w[0] * xx + b) / w[1] # separator line
fig = plt.figure(1) # plot
plot1 = plt.plot(x1[:, 0], x1[:, 1], 'ob', x2[:, 0], x2[:, 1], 'or')
plot2 = plt.plot(xx, yy, '--k')
plt.show()

赤い点が青の側にいくつかありますが、大体分割出来ています。
f:id:satojkovic:20120420002245p:image