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.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 = [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()
输出
[[ 0.02315167]
[-0.32239024]
[ 0.31036948]]
0
[[0.02315167]
[0.00760976]
[0.47536948]]
1
[[-0.08684833]
[-0.04739024]
[ 0.31036948]]
2
[[-0.19684833]
[-0.10239024]
[ 0.14536948]]
3
[[-0.14184833]
[ 0.28260976]
[ 0.36536948]]
4
[[-0.25184833]
[ 0.22760976]
[ 0.20036948]]
5
[[-0.36184833]
[ 0.17260976]
[ 0.03536948]]
6
Finished
epoch: 6
k= [-4.88018935]
d= [10.23052451]
#输入数据
X = np.array([[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1]])
#标签
Y = np.array([[-1],
[1],
[1],
[-1]])