简单练习神经网络单层结构练习:
神经网络练习实现代码如下:
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]])

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))
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]
b = -W[0]/W[2]

print(‘k=’,k)
print(‘b=’,b)
x_data = (0,5)
plt.figure()
plt.plot(x_data,x_data*k+b,‘r’)
plt.scatter(x1,y1,c=‘b’)
plt.scatter(x2,y2,c=‘g’)
plt.show()

相关文章:

  • 2021-11-30
  • 2021-06-21
  • 2021-09-08
  • 2021-11-19
  • 2021-04-18
  • 2021-06-07
猜你喜欢
  • 2021-05-18
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案