深度学习之感知器的实现

感知器的一个公式:
当 wx+b>0 , f(x)=1,否则f(x)=0

  • x输入的向量
  • w:是权重
  • w*x是点积
  • b是偏置(与权重类似,偏置可以认为是激励函数的偏移量,或者给神经元一个基础活跃等级。)

对权重的调整公式,此处使用了梯度下降的方法: w=w+i*(t-count)*x

  • i是一个常数,i越小感知器学习越精确,当然耗费时间也最长
  • t:本应该得到的值
  • count:实际计算得到的值
  • x:同上

对偏置调整的公式: b=b+i(t-count)*

这次我们使用python来实现与和或

#因为与和或计算需要输入两个数,所以这里设置两个权重
global w1,w2
w1=0.4
w2=0.6
#偏置随便设一个
global b
b=0.8
#上面提到的常数
global i
i=0.01

对感知器公式实现的函数:

def count(x,y):
    if w1*x+w2*y+b>0:
        return 1
    else:
        return 0

对权重和偏置不断矫正的函数:

def update_all(r,x,y):
    global w1,w2,b
    temp = count(x,y)
    w1=w1+i*(r-temp)*x
    w2=w2+i*(r-temp)*y
    b=b+i*(r-temp)
    print(w1,w2,b)

最后就是输入学习的资料和确认计算结果:

def main():
    for _ in range(1,50):
        update_all(1.0,1.0,1.0)
        update_all(0.0,1.0,0.0)
        update_all(0.0,0.0,1.0)
        update_all(0.0,0.0,0.0)
    
    print('1 and 1 =',count(1,1))
    print('0 and 1 =',count(0,1))
    print('1 and 0 =',count(1,0))
    print('0 and 0 =',count(0,0))

这里我们先进行与的计算:
这里我前面代码写错了,所以说下面所有的结果会不一样…
进行25次学习的结果:

(0.4, 0.6, 0.8)
(0.39, 0.6, 0.79)
(0.39, 0.59, 0.78)
(0.39, 0.59, 0.77)
(0.39, 0.59, 0.77)
.......省略........
(0.1599999999999998, 0.35999999999999976, 0.08999999999999947)
(0.1599999999999998, 0.35999999999999976, 0.07999999999999947)
('1 and 1 =', 1)
('0 and 1 =', 1)
('1 and 0 =', 1)
('0 and 0 =', 1)

这里我们明显发现结果是错误的,提升到50次:

(0.4, 0.6, 0.8)
(0.39, 0.6, 0.79)
(0.39, 0.59, 0.78)
(0.39, 0.59, 0.77)
(0.39, 0.59, 0.77)
(0.38, 0.59, 0.76)
(0.38, 0.58, 0.75)
.......省略........
(0.07999999999999981, 0.1799999999999996, -0.18000000000000055)
(0.07999999999999981, 0.1799999999999996, -0.18000000000000055)
(0.07999999999999981, 0.1799999999999996, -0.18000000000000055)
('1 and 1 =', 1)
('0 and 1 =', 0)
('1 and 0 =', 0)
('0 and 0 =', 0)

结果正确了,并且权重和偏置都稳定到一个值,是不是感觉很神奇
或运算也是相同的,这里我们将偏置改为b=0.4:
25次:

(0.4, 0.6, 0.4)
(0.4, 0.6, 0.4)
(0.4, 0.6, 0.4)
(0.4, 0.6, 0.39)
(0.4, 0.6, 0.39)
(0.4, 0.6, 0.39)
(0.4, 0.6, 0.39)
(0.4, 0.6, 0.38)
(0.4, 0.6, 0.38)
(0.4, 0.6, 0.38)
.......省略........
(0.4, 0.6, 0.16999999999999982)
(0.4, 0.6, 0.16999999999999982)
(0.4, 0.6, 0.16999999999999982)
(0.4, 0.6, 0.16999999999999982)
(0.4, 0.6, 0.1599999999999998)
('1 and 1 =', 1)
('0 and 1 =', 1)
('1 and 0 =', 1)
('0 and 0 =', 1)

50次:

(0.4, 0.6, 0.4)
(0.4, 0.6, 0.4)
(0.4, 0.6, 0.4)
(0.4, 0.6, 0.39)
(0.4, 0.6, 0.39)
(0.4, 0.6, 0.39)
(0.4, 0.6, 0.39)
(0.4, 0.6, 0.38)
.......省略........
(0.4, 0.6, -1.97758476261356e-16)
(0.4, 0.6, -1.97758476261356e-16)
(0.4, 0.6, -1.97758476261356e-16)
(0.4, 0.6, -1.97758476261356e-16)
(0.4, 0.6, -1.97758476261356e-16)
('1 and 1 =', 1)
('0 and 1 =', 1)
('1 and 0 =', 1)
('0 and 0 =', 0)

参考:
感知器(维基百科)
零基础入门深度学习(1) - 感知器