当前位置:   article > 正文

深度学习笔记三:线性神经网络,delta学习规则,梯度下降法_delta学习规则例题

delta学习规则例题

这里我们提出一个神经网络解决异或问题

异或问题出现四个点,此时一条直线无法正确地区分出正负样本

  1. X = np.array([[1,0,0],[1,0,1],[1,1,0],[1,1,1]])
  2. Y = np.array([-1,1,1,-1])
  1. x1 = [0,1]
  2. y1 = [1,0]
  3. x2 = [0,1]
  4. y2 = [0,1]



于是我们引入线性神经网络






线性神经网络解决线性不可分问题


下面给出实现代码:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. X = np.array([[1,0,0,0,0,0],
  4. [1,0,1,0,0,1],
  5. [1,1,0,1,0,0],
  6. [1,1,1,1,1,1]])
  7. Y = np.array([-1,1,1,-1])
  8. W = (np.random.random(6)-0.5)*2
  9. print(W)
  10. #学习率
  11. lr = 0.11
  12. n = 0
  13. o = 0
  14. def update():
  15. global X,Y,W,lr,n
  16. n += 1
  17. o = np.dot(X,W.T)
  18. W_C = lr*((Y-o.T).dot(X))/X.shape[0]
  19. W = W + W_C
  20. def predict(X):
  21. global W
  22. y = np.sign(np.dot(X,W.T))
  23. return y
  24. def calculate(x,root):
  25. a = W[5]
  26. b = W[2]+x*W[4]
  27. c = W[0]+x*W[1]+x*x*W[3]
  28. if root==1:
  29. return (-b+np.sqrt(b*b-4*a*c))/(2*a)
  30. if root==2:
  31. return (-b-np.sqrt(b*b-4*a*c))/(2*a)
  32. for i in range(10000):
  33. update()
  34. #print(W)
  35. #print(n)
  36. #o = np.sign(np.dot(X,W.T))
  37. if(o == Y.T).all():
  38. print('finish')
  39. break
  40. for e in X:
  41. print(predict(e))
  42. x1 = [0,1]
  43. y1 = [1,0]
  44. x2 = [0,1]
  45. y2 = [0,1]
  46. xdata = np.linspace(0,5)
  47. k = -W[1]/W[2]
  48. d = -W[0]/W[2]
  49. plt.figure()
  50. plt.plot(xdata,calculate(xdata,1),'r')
  51. plt.plot(xdata,calculate(xdata,2),'r')
  52. plt.plot(x1,y1,'bo')
  53. plt.plot(x2,y2,'yo')
  54. plt.show()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/129642
推荐阅读
相关标签
  

闽ICP备14008679号