python实现感知器(与或运算)

感知器属于一种简单的神经元,在神经网络中通常感知机与神经元的区别就是激活函数的不同,一般常用的激活函数是sigmoid函数,感知器中为阶跃函数。

感知器的输出为:y=f(n)=f(wx+b)    //其中,w和b为感知器模型参数,w表示全权值,b表示偏置,wx表示w和x的内积。

在感知层进行学习时,每一个样本都将作为一个刺激输入神经元。输入信号是每一个样本的特征,期望的输出是该样本的类别。当输出与类别不同时,可以通过调整突触权值和偏置值,直到每个样本的输出与类别相同。

原理:输入向量X=(x1,x2,xr)T为样本的特征维,对应权值向量W=(w1,w2,wr)为一组参数。因此线性方程WX+b=0对应于空间中的一条直线。Wi是该直线法向分量,也是权值矩阵的某一行向量;b为直线的截距。该直线确定的判定边界将空间内的不同元素划分为正负两类,通过学习得到感知器模型,对于新的新输入向量可预测其输出类别。

给出一组矩阵,利用神经网络的反向传播功能(BP)来训练一种能够实现逻辑与、或的感知器

python实现感知器(与或运算)_第1张图片

 

import numpy as np

and_samples = [
    [0, 0, 0],
    [1, 0, 0],
    [0, 1, 0],
    [1, 1, 1],
]
or_samples = [
    [0, 0, 0],
    [1, 0, 1],
    [0, 1, 1],
    [1, 1, 1],
]


def perception(samples):
    # 权重
    w = np.array([1, 2])
    # 偏置
    b = 0
    a = 1
    # 训练10遍
    for i in range(10):
        for j in range(4):
            # 矩阵的第j行的前两个数值
            x = np.array(samples[j][:2])
            # 将未激活的值输入sigmoid函数 dot:向量的点乘运算
            if np.dot(w, x) + b > 0:
                y = 1
            else:
                y = 0
            # 真实值
            d = np.array(samples[j][2])
            delta_b = a * (d - y)
            delta_w = a * (d - y) * x
            print('No. {} sample [{} {} {} {} {} {} {}]'.format(
                i, j, w[0], w[1], b, y, delta_w[0], delta_w[1], delta_b
            ))
            # 更新权重
            w = w + delta_w
            b = b + delta_b


if __name__ == '__main__':
    print('logical and:')
    perception(and_samples)
    print('logical or:')
    perception(or_samples)

python实现感知器(与或运算)_第2张图片

python实现感知器(与或运算)_第3张图片

Result and:
No. 0 sample [0 1 2 0 0 0 0]
No. 0 sample [1 1 2 0 1 -1 0]
No. 0 sample [2 0 2 -1 1 0 -1]
No. 0 sample [3 0 1 -2 0 1 1]
No. 1 sample [0 1 2 -1 0 0 0]
No. 1 sample [1 1 2 -1 0 0 0]
No. 1 sample [2 1 2 -1 1 0 -1]
No. 1 sample [3 1 1 -2 0 1 1]
No. 2 sample [0 2 2 -1 0 0 0]
No. 2 sample [1 2 2 -1 1 -1 0]
No. 2 sample [2 1 2 -2 0 0 0]
No. 2 sample [3 1 2 -2 1 0 0]
No. 3 sample [0 1 2 -2 0 0 0]
No. 3 sample [1 1 2 -2 0 0 0]
No. 3 sample [2 1 2 -2 0 0 0]
No. 3 sample [3 1 2 -2 1 0 0]
No. 4 sample [0 1 2 -2 0 0 0]
No. 4 sample [1 1 2 -2 0 0 0]
No. 4 sample [2 1 2 -2 0 0 0]
No. 4 sample [3 1 2 -2 1 0 0]
No. 5 sample [0 1 2 -2 0 0 0]
No. 5 sample [1 1 2 -2 0 0 0]
No. 5 sample [2 1 2 -2 0 0 0]
No. 5 sample [3 1 2 -2 1 0 0]
No. 6 sample [0 1 2 -2 0 0 0]
No. 6 sample [1 1 2 -2 0 0 0]
No. 6 sample [2 1 2 -2 0 0 0]
No. 6 sample [3 1 2 -2 1 0 0]
No. 7 sample [0 1 2 -2 0 0 0]
No. 7 sample [1 1 2 -2 0 0 0]
No. 7 sample [2 1 2 -2 0 0 0]
No. 7 sample [3 1 2 -2 1 0 0]
No. 8 sample [0 1 2 -2 0 0 0]
No. 8 sample [1 1 2 -2 0 0 0]
No. 8 sample [2 1 2 -2 0 0 0]
No. 8 sample [3 1 2 -2 1 0 0]
No. 9 sample [0 1 2 -2 0 0 0]
No. 9 sample [1 1 2 -2 0 0 0]
No. 9 sample [2 1 2 -2 0 0 0]
No. 9 sample [3 1 2 -2 1 0 0]

观察发现从第三次训练后,通过神经网络的BP,权重w0,w1和b已经趋于稳定:w0=1,w1=2,b=-2

python实现感知器(与或运算)_第4张图片

python实现感知器(与或运算)_第5张图片 

Result or:
No. 0 sample [0 1 2 0 0 0 0]
No. 0 sample [1 1 2 0 1 0 0]
No. 0 sample [2 1 2 0 1 0 0]
No. 0 sample [3 1 2 0 1 0 0]
No. 1 sample [0 1 2 0 0 0 0]
No. 1 sample [1 1 2 0 1 0 0]
No. 1 sample [2 1 2 0 1 0 0]
No. 1 sample [3 1 2 0 1 0 0]
No. 2 sample [0 1 2 0 0 0 0]
No. 2 sample [1 1 2 0 1 0 0]
No. 2 sample [2 1 2 0 1 0 0]
No. 2 sample [3 1 2 0 1 0 0]
No. 3 sample [0 1 2 0 0 0 0]
No. 3 sample [1 1 2 0 1 0 0]
No. 3 sample [2 1 2 0 1 0 0]
No. 3 sample [3 1 2 0 1 0 0]
No. 4 sample [0 1 2 0 0 0 0]
No. 4 sample [1 1 2 0 1 0 0]
No. 4 sample [2 1 2 0 1 0 0]
No. 4 sample [3 1 2 0 1 0 0]
No. 5 sample [0 1 2 0 0 0 0]
No. 5 sample [1 1 2 0 1 0 0]
No. 5 sample [2 1 2 0 1 0 0]
No. 5 sample [3 1 2 0 1 0 0]
No. 6 sample [0 1 2 0 0 0 0]
No. 6 sample [1 1 2 0 1 0 0]
No. 6 sample [2 1 2 0 1 0 0]
No. 6 sample [3 1 2 0 1 0 0]
No. 7 sample [0 1 2 0 0 0 0]
No. 7 sample [1 1 2 0 1 0 0]
No. 7 sample [2 1 2 0 1 0 0]
No. 7 sample [3 1 2 0 1 0 0]
No. 8 sample [0 1 2 0 0 0 0]
No. 8 sample [1 1 2 0 1 0 0]
No. 8 sample [2 1 2 0 1 0 0]
No. 8 sample [3 1 2 0 1 0 0]
No. 9 sample [0 1 2 0 0 0 0]
No. 9 sample [1 1 2 0 1 0 0]
No. 9 sample [2 1 2 0 1 0 0]
No. 9 sample [3 1 2 0 1 0 0]

对于逻辑或,从输出样本的第一次训练之后,权重和偏置的BP趋于稳定:w0=1,w1=2,b=0

你可能感兴趣的:(人工智能,机器学习,深度学习)