神经网络(线性神经网络)

线性神经网络

线性神经网络在结构上与感知器非常相似,只是激活函数不同。在模型训练时把原来的sign函数改成了purelin函数y = x

线性神经网络结构

在这里插入图片描述

代码示例
import numpy as np
import matplotlib.pyplot as plt

#输入数据
X = np.array([[1,3,3],
              [1,4,3],
              [1,1,1],
              [1,0,2]])
#标签
Y = np.array([[1],
              [1],
              [-1],
              [-1]])

#权值初始化,3行1列,取值范围-1到1
W = (np.random.random([3,1])-0.5)*2

print(W)
#学习率设置
lr = 0.11
#神经网络输出
O = 0

def update():
    global X,Y,W,lr
    O = np.dot(X,W)
    W_C = lr*(X.T.dot(Y-O))/int(X.shape[0])
    W = W + W_C
  
for _ in range(100):
    update()#更新权值

    #正样本
    x1 = [3,4]
    y1 = [3,3]
    #负样本
    x2 = [1,0]
    y2 = [1,2]

    #计算分界线的斜率以及截距
    k = -W[1]/W[2]
    d = -W[0]/W[2]
    print('k=',k)
    print('d=',d)

    xdata = (0,5)

    plt.figure()
    plt.plot(xdata,xdata*k+d,'r')
    plt.scatter(x1,y1,c='b')
    plt.scatter(x2,y2,c='y')
    plt.show()

在这里插入图片描述

解决异或问题
import numpy as np
import matplotlib.pyplot as plt

#输入数据
X = np.array([[1,0,0,0,0,0],
              [1,0,1,0,0,1],
              [1,1,0,1,0,0],
              [1,1,1,1,1,1]])
#标签
Y = np.array([[-1],
              [1],
              [1],
              [-1]])

#权值初始化,3行1列,取值范围-1到1
W = (np.random.random([6,1])-0.5)*2

print(W)
#学习率设置
lr = 0.11
#神经网络输出
O = 0

def update():
    global X,Y,W,lr
    O = np.dot(X,W)
    W_C = lr*(X.T.dot(Y-O))/int(X.shape[0])
    W = W + W_C

for _ in range(1000):
    update()#更新权值

#正样本
x1 = [0,1]
y1 = [1,0]
#负样本
x2 = [0,1]
y2 = [0,1]

def show(x,root):
    a = W[5]
    b = W[2]+x*W[4]
    c = W[0]+x*W[1]+x*x*W[3]
    if root == 1:
        return (-b+np.sqrt(b*b-4*a*c)) / (2*a)
    if root == 2:
        return (-b-np.sqrt(b*b-4*a*c)) / (2*a)

xdata = np.linspace(-1, 2)

plt.figure()
plt.plot(xdata,show(xdata, 1),'r')
plt.plot(xdata,show(xdata, 2),'r')
    
plt.scatter(x1,y1,c='b')
plt.scatter(x2,y2,c='y')
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

!一直往南方开.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值