News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

神经网络——Python实现BP神经网络算法(理论+例子+程序)

posted on 2023-06-06 11:24     read(838)     comment(0)     like(8)     collect(5)


1. Multi-layer perceptron model based on BP algorithm

The multi-layer perceptron using BP algorithm is the most widely used neural network so far. In the application of multi-layer perceptron, the application of single hidden layer network shown in Figure 3-15 is the most common. It is generally customary to refer to a single hidden layer feedforward network as a three-layer perceptron. The so-called three layers include the input layer, hidden layer and output layer.

Neural network - Python implements BP neural network algorithm

Neural network - Python implements BP neural network algorithm

The final result of the algorithm adopts the gradient descent method, and the specific detailed process is omitted here!

2. The program implementation process of BP algorithm

Neural network - Python implements BP neural network algorithm

3. Improvement of standard BP algorithm - adding momentum item

When the standard BP algorithm adjusts the weight, it only adjusts according to the gradient direction of the error at time t, and does not consider the gradient direction before time t, which often makes the training process oscillate and converge slowly. In order to improve the training speed of the network, a momentum term can be added to the weight adjustment formula. If W represents the weight matrix of a certain layer and X represents the input vector of a certain layer, the expression of the weight adjustment vector containing the momentum item is

Neural network - Python implements BP neural network algorithm

It can be seen that increasing the momentum item means taking a part from the previous weight adjustment and adding it to the current weight adjustment. α is called the momentum coefficient, and generally has a∈(0,1). The momentum term reflects the previous accumulated adjustment experience and plays a damping role for the adjustment at time t. When there are sudden fluctuations in the error surface, the oscillation tendency can be reduced and the training speed can be improved. At present, the momentum item is added to the BP algorithm, so that the BP algorithm with the momentum item becomes a new standard algorithm.

4. Python implements BP neural network and its learning algorithm

In order to use the algorithm, here is a brief example (an example that does not require normalization or standardization)

Input X=-1:0.1:1;
output D=..... (see the data in the code for details)

In order to facilitate the viewing of the results, we output and draw the results as graphics, as follows:

Neural network - Python implements BP neural network algorithm

The yellow and blue lines represent the output and input after training

5. The procedure is as follows:

  1. # -*- coding: utf-8 -*-
  2. import math
  3. import string
  4. import matplotlib as mpl
  5. ############################################调用库(根据自己编程情况修改)
  6. import numpy.matlib
  7. import numpy as np
  8. np.seterr(divide='ignore',invalid='ignore')
  9. import matplotlib.pyplot as plt
  10. from matplotlib import font_manager
  11. import pandas as pd
  12. import random
  13. #生成区间[a,b]内的随机数
  14. def random_number(a,b):
  15. return (b-a)*random.random()+a
  16. #生成一个矩阵,大小为m*n,并且设置默认零矩阵
  17. def makematrix(m, n, fill=0.0):
  18. a = []
  19. for i in range(m):
  20. a.append([fill]*n)
  21. return np.array(a)
  22. #函数sigmoid(),两个函数都可以作为激活函数
  23. def sigmoid(x):
  24. #return np.tanh(x)
  25. return (1-np.exp(-1*x))/(1+np.exp(-1*x))
  26. #函数sigmoid的派生函数
  27. def derived_sigmoid(x):
  28. return 1-(np.tanh(x))**2
  29. #return (2*np.exp((-1)*x)/((1+np.exp(-1*x)**2)))
  30. #构造三层BP网络架构
  31. class BPNN:
  32. def __init__(self, num_in, num_hidden, num_out):
  33. #输入层,隐藏层,输出层的节点数
  34. self.num_in = num_in + 1 #增加一个偏置结点
  35. self.num_hidden = num_hidden + 1 #增加一个偏置结点
  36. self.num_out = num_out
  37. #激活神经网络的所有节点(向量)
  38. self.active_in = np.array([-1.0]*self.num_in)
  39. self.active_hidden = np.array([-1.0]*self.num_hidden)
  40. self.active_out = np.array([1.0]*self.num_out)
  41. #创建权重矩阵
  42. self.wight_in = makematrix(self.num_in, self.num_hidden)
  43. self.wight_out = makematrix(self.num_hidden, self.num_out)
  44. #对权值矩阵赋初值
  45. for i in range(self.num_in):
  46. for j in range(self.num_hidden):
  47. self.wight_in[i][j] = random_number(0.1, 0.1)
  48. for i in range(self.num_hidden):
  49. for j in range(self.num_out):
  50. self.wight_out[i][j] = random_number(0.1, 0.1)
  51. #偏差
  52. for j in range(self.num_hidden):
  53. self.wight_in[0][j] = 0.1
  54. for j in range(self.num_out):
  55. self.wight_in[0][j] = 0.1
  56. #最后建立动量因子(矩阵)
  57. self.ci = makematrix(self.num_in, self.num_hidden)
  58. self.co = makematrix(self.num_hidden, self.num_out)
  59. #信号正向传播
  60. def update(self, inputs):
  61. if len(inputs) != self.num_in-1:
  62. raise ValueError('与输入层节点数不符')
  63. #数据输入输入层
  64. self.active_in[1:self.num_in]=inputs
  65. #数据在隐藏层的处理
  66. self.sum_hidden=np.dot(self.wight_in.T,self.active_in.reshape(-1,1)) #点乘
  67. self.active_hidden=sigmoid(self.sum_hidden) #active_hidden[]是处理完输入数据之后存储,作为输出层的输入数据
  68. self.active_hidden[0]=-1
  69. #数据在输出层的处理
  70. self.sum_out=np.dot(self.wight_out.T,self.active_hidden) #点乘
  71. self.active_out = sigmoid(self.sum_out) #与上同理
  72. return self.active_out
  73. #误差反向传播
  74. def errorbackpropagate(self, targets, lr,m): #lr是学习率
  75. if self.num_out==1:
  76. targets=[targets]
  77. if len(targets) != self.num_out:
  78. raise ValueError('与输出层节点数不符!')
  79. #误差
  80. error=(1/2)*np.dot((targets.reshape(-1,1)-self.active_out).T,(targets.reshape(-1,1)-self.active_out))
  81. #输出误差信号
  82. self.error_out=(targets.reshape(-1,1)-self.active_out)*derived_sigmoid(self.sum_out)
  83. #隐层误差信号
  84. #self.error_hidden=np.dot(self.wight_out.reshape(-1,1),self.error_out.reshape(-1,1))*self.active_hidden*(1-self.active_hidden)
  85. self.error_hidden=np.dot(self.wight_out,self.error_out)*derived_sigmoid(self.sum_hidden)
  86. #更新权值
  87. #隐藏
  88. self.wight_out=self.wight_out+lr*np.dot(self.error_out,self.active_hidden.reshape(1,-1)).T+m*self.co
  89. self.co=lr*np.dot(self.error_out,self.active_hidden.reshape(1,-1)).T
  90. #输入
  91. self.wight_in=self.wight_in+lr*np.dot(self.error_hidden,self.active_in.reshape(1,-1)).T+m*self.ci
  92. self.ci=lr*np.dot(self.error_hidden,self.active_in.reshape(1,-1)).T
  93. return error
  94. #测试
  95. def test(self, patterns):
  96. for i in patterns:
  97. print(i[0:self.num_in-1], '->', self.update(i[0:self.num_in-1]))
  98. return self.update(i[0:self.num_in-1])
  99. #权值
  100. def weights(self):
  101. print("输入层权重")
  102. print(self.wight_in)
  103. print("输出层权重")
  104. print(self.wight_out)
  105. def train(self, pattern, itera=100, lr = 0.2, m=0.1):
  106. for i in range(itera):
  107. error = 0.0
  108. for j in pattern:
  109. inputs = j[0:self.num_in-1]
  110. targets = j[self.num_in-1:]
  111. self.update(inputs)
  112. error = error+self.errorbackpropagate(targets, lr,m)
  113. if i % 10 == 0:
  114. print('########################误差 %-.5f######################第%d次迭代' %(error,i))
  115. #实例
  116. X=list(np.arange(-1,1.1,0.1))
  117. D=[-0.96, -0.577, -0.0729, 0.017, -0.641, -0.66, -0.11, 0.1336, -0.201, -0.434, -0.5, -0.393, -0.1647, 0.0988, 0.3072, 0.396, 0.3449, 0.1816, -0.0312, -0.2183, -0.3201]
  118. A=X+D
  119. patt=np.array([A]*2)
  120. #创建神经网络,21个输入节点,21个隐藏层节点,1个输出层节点
  121. n = BPNN(21, 21, 21)
  122. #训练神经网络
  123. n.train(patt)
  124. #测试神经网络
  125. d=n.test(patt)
  126. #查阅权重值
  127. n.weights()
  128. plt.plot(X,D)
  129. plt.plot(X,d)
  130. plt.show()

Source: Xiao Ling の Blog—Good Times|A bad blog

[1] Han Liqun, Artificial Neural Network Theory and Application [M]. Beijing: Mechanical Industry Press, 2016.



Category of website: technical article > Blog

Author:evilangel

link:http://www.pythonblackhole.com/blog/article/80182/9ce5360f9ab57f180da7/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

8 0
collect article
collected

Comment content: (supports up to 255 characters)