感知机原理与代码

2020-11-07  本文已影响0人  纵春水东流

mark一下,有空在打
1、原理
(1)感知机模型
超平面
学习策略
损失函数
梯度下降
(2)
(3)
(4)
2、代码

x=[[2,3],[3,4],[4,3],[5,3]]
y=[-1,-1,1,1]



def perception(x,y,lr=0.1):
    x=np.array(x)
    y=np.array(y)
    w=np.zeros_like(x[0])
    b=0
    
    def sign(x):
        if x < 0:
            return -1
        else:
            return 1
    
    run=True
    count=0
    while(run):
        run=False                
        for i in range(len(x)):
            y_ = sign(np.dot(w,x[i])+b)
            
            if y[i]*y_ <=0:
                w = w + y[i]*x[i]
                b = b + y[i]
                print(w,b)
        
        for i in range(len(x)):
            y_i = sign(np.dot(w,x[i])+b)
            if y[i]*y_i<=0:
                run=True
                count+=1        
    return(w,b)

import numpy as np

w,b = perception(x,y)
h_x = np.array(range(8))
h_y = np.empty(0)
for i in range(8):
    delta=(-b-w[1]*i)/w[0]
    h_y = np.append(h_y,delta)
import matplotlib.pyplot as plt
x=np.array(x)
y=np.array(y)
plt.scatter(x[:2,0],x[:2,1],color='red')
plt.scatter(x[2:,0],x[2:,1])
plt.plot(h_x,h_y)
plt.plot()
plt.show



[-2 -3] -1
[2 0] 0
[ 0 -3] -1
[4 0] 0
[ 2 -3] -1
[6 0] 0
[ 4 -3] -1
上一篇 下一篇

猜你喜欢

热点阅读