3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

パーセプトロンの実装

Last updated at Posted at 2018-06-28

授業でパーセプトロンを習ったので、イメージを掴むために実装しました。

#<パーセプトロン実装>


#訓練データ
train_x0 = [1, 1, 1, 1 ,1 ,1]
train_x1 = [1.2, 0.2, -0.2, -0.5, -1.0, -1.5]

#訓練データのクラス
train_y = [1, 1, 1, 2, 2, 2]

#学習係数
p = 0.5

#重みベクトル
w0 = 0.5
w1 = 0.5
    
#誤りの修正回数
error_count = 0

# 識別関数の定義    
def learn():
    global w0, w1, error_count
    for i in range(5):
        g = w0 * train_x0[i] + w1 * train_x1[i] 
        if train_y[i] == 1 and g > 0:
            pass
        elif train_y[i] == 1 and g <= 0:
            w0 += p * train_x0[i]
            w1 += p * train_x1[i]
            error_count += 1   
        elif train_y[i] == 2 and g <= 0:
            pass
        else:
            w0 -= p * train_x0[i]
            w1 -= p * train_x1[i]
            error_count += 1

#学習回数
train_count = 0

#1回目の学習
learn()
train_count += 1

#2回目以降の学習
while error_count != 0:
    error_count = 0
    learn()
    train_count += 1

print("学習後のw0:" +str(w0))
print("学習後のw1:" +str(w1))
print("学習回数:" + str(train_count))

3
1
0

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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?