深度学习(1)单层感知器

1、单层感知器模型

2、线性神经网络

感知器激活函数为 y = sign(x)  x>0 时y=1,x<0时 y=-1
线性神经网络在结构上和感知器类似,只是激活函数不同,激活函数为 y = x ,即purelin函数

3、代码实现单层感知器

import numpy as np
import matplotlib.pyplot as plt

#训练数据
X = np.array([
    [1,3,3]
    ,[1,4,3]
    ,[1,1,1]
    ,[1,2,1]
    ])
# 定义标签
T = np.array([
     [1]
     ,[1]
     ,[-1]
     ,[-1]
     ])
#初始化权值
W = np.random.random([3,1])
#设置学习率
lr = 0.1
#神经网络输出
Y = 0
#更新权值函数
def train():
    global X,W,T,lr
    Y = np.sign(np.dot(X,W))
    E = T - Y
    delta_W = lr * np.dot(X.T,E)/X.shape[0]
    W += delta_W

for i in range(100):
    train()
    print('epoch:',i+1)
    print('weights:',W)
    Y = np.sign(np.dot(X,W))
    if (Y == T).all():
        print('Finished')
        break

图像展示训练结果

#正样本的xy坐标
x1 = [3,4]
y1 = [3,3]
#负样本的xy坐标
x2 = [1,2]
y2 = [1,1]

plt.scatter(x1,y1,c='b')
plt.scatter(x2,y2,c='r')
#画出训练模型的样本分界线
#本例中分界线是条直线
#y = -(w1/w2)*x - w0/w2
#设定两个点的x坐标
x_ = [0,6]
y_ = (-W[1]/W[2])*x_ - W[0]/W[2]
plt.plot(x_,y_,'y')

发布了23 篇原创文章 · 获赞 1 · 访问量 3360

猜你喜欢

转载自blog.csdn.net/ABCDABCD321123/article/details/104235845