sklearn速度复习-神经网络

#单层感知器/线性神经网络-异或问题
'''
异或
0^0 = 0
0^1 = 1
1^0 = 1
1^1 = 0
'''
import numpy as np
import matplotlib.pyplot as plt
X = np.array([[1,0,0],#输入数据
              [1,0,1],
              [1,1,0],  
              [1,1,1]])
Y = np.array([[-1],#标签
              [1],
              [1],
              [-1]])
W = (np.random.random([3,1])-0.5)*2#权值初始化,3行1列,取值范围-1到1
print(W)
lr = 0.11#学习率设置
O = 0#神经网络输出
def update():
    global X,Y,W,lr
    O = np.sign(np.dot(X,W)) # shape:(3,1)
    W_C = lr*(X.T.dot(Y-O))/int(X.shape[0])
    W = W + W_C
for i in range(100):
    update()#更新权值
    print(W)#打印当前权值
    print(i)#打印迭代次数
    O = np.sign(np.dot(X,W))#计算当前输出  
    if(O == Y).all(): #如果实际输出等于期望输出,模型收敛,循环结束
        print('Finished')
        print('epoch:',i)
        break
x1 = [0,1]#正样本
y1 = [1,0]
x2 = [0,1]#负样本
y2 = [0,1]
k = -W[1]/W[2]#计算分界线的斜率以及截距
d = -W[0]/W[2]
print('k=',k)
print('d=',d)
xdata = (-2,3)
plt.figure()
plt.plot(xdata,xdata*k+d,'r')
plt.scatter(x1,y1,c='b')
plt.scatter(x2,y2,c='y')
plt.show()
#BP神经网络
import numpy as np
X = np.array([[1,0,0],#输入数据
              [1,0,1],
              [1,1,0],
              [1,1,1]])
Y = np.array([[0,1,1,0]])#标签
V = np.random.random((3,4))*2-1 #权值初始化,取值范围-1到1
W = np.random.random((4,1))*2-1
print(V)
print(W)
lr = 0.11#学习率设置
def sigmoid(x):
    return 1/(1+np.exp(-x))
def dsigmoid(x):
    return x*(1-x)
def update():
    global X,Y,W,V,lr
    L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)
    L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)
    L2_delta = (Y.T - L2)*dsigmoid(L2)
    L1_delta = L2_delta.dot(W.T)*dsigmoid(L1)
    W_C = lr*L1.T.dot(L2_delta)
    V_C = lr*X.T.dot(L1_delta)
    W = W + W_C
    V = V + V_C
for i in range(20000):
    update()#更新权值
    if i%500==0:
        L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)
        L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)
        print('Error:',np.mean(np.abs(Y.T-L2)))
L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)
L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)
print(L2)
def judge(x):
    if x>=0.5:
        return 1
    else:
        return 0
for i in map(judge,L2):
    print(i)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值