BP神经网络
Posted 光何
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BP神经网络相关的知识,希望对你有一定的参考价值。
BP(Back Propagation)神经网络是1986年由Rumelhart和McCelland为首的科学家小组提出,是一种按误差逆传播算法训练的多层前馈网络,是目前应用最广泛的神经网络模型之一。BP网络能学习和存贮大量的输入-输出模式映射关系,而无需事前揭示描述这种映射关系的数学方程。它的学习规则是使用梯度下降法,通过反向传播来不断调整网络的权值和阈值,使网络的误差平方和最小。BP神经网络模型拓扑结构包括输入层(input)、隐层(hidden layer)和输出层(output layer)。
关键字:人工智能,神经网络,BP
The BP neural network
Abstract: the structure of the neural network is to simulate the human brain tissue, in an attempt to use the knowledge of bionics to solve problems. Undeniably artificial intelligence and virtual reality will become a hot technology in the recent years, virtual reality entry threshold is low, and artificial intelligence research needs certain levels of knowledge, but there is no doubt that every new breakthrough in the field of artificial intelligence will deeply affect the development of science and technology in the future.
BP (Back Propagation) neural network is a group of scientists headed by Rumelhart and McCelland 1986, is a kind of according to the error Back Propagation algorithm training of the multilayer feedforward network, is currently one of the most widely used neural network model. BP network can learn and store a lot of input - output model mapping, without prior reveal describe the mathematical equations of the mapping relationship. Its learning rule is to use gradient descent method, by back propagation to constantly adjust the network weights and threshold, minimize the error sum of squares of the network. BP neural network model of topological structures include input layer (input), the hidden layer, hidden layer) and output layer (output layer).
Keywords: artificial intelligence, neural network, BP
1,研究背景
“人脑是如何工作的?”
“人类能否制作模拟人脑的人工神经元?”
2,研究方向
a 1 = tansig (IW 1,1 p 1 +b 1 )
tansig(x)=tanh(x)=(ex-e-x)/(ex+e-x)
a 2 = purelin (LW 2,1 a 1 +b 2 )
3,程序设计
#include<iostream> #include<cstdlib> #include<ctime> #include<cmath> #include<iomanip> using namespace std; const int max_learn_length = 2000; //最大学习次数 const double study_rate = 0.01; //学习率(数据调整步长值) const double anticipation_error = 0.0001; //期望误差 const int sample = 21; //样本数量 const int input = 1; //每组样本输入层数据量 const int hidden = 5; //每组样本隐含层数据量 const int output = 1; //每组样本输出层数据量 double P[sample] = //输入矢量 { -1,-0.9,-0.8,-0.7,-0.6, -0.5,-0.4,-0.3,-0.2,-0.1, 0,0.1,0.2,0.3,0.4, 0.5,0.6,0.7,0.8,0.9, 1 }; double T[sample] = //输出矢量 { -0.9602,-0.5770,-0.0729,0.3771,0.6405, 0.6600,0.4609,0.1336,-0.2013,-0.4344, -0.5000,-0.3930,-0.1647,0.0988,0.3072, 0.3960,0.3449,0.1816,-0.0312,-0.2189, -0.3201 }; int main(int argc, char **argv) { double precision; //误差精度变量 double W_input_hidden[hidden][input]; //输入层到隐含层的网络权值变量 double W_hidden_output[output][hidden]; //隐含层到输出层的网络权值变量 double B_input_hidden[hidden]; //输入层到隐含层阈值变量 double B_hidden_output[output]; //隐含层到输出层阈值变量 double E_input_hidden[hidden]; //输入层到隐含层误差 double E_hidden_output[output]; //隐含层到输出层误差 double A_input_hidden[hidden]; //隐含层实际输出值 double A_hidden_output[output]; //输出层实际输出值 int ii, ij, ik, ic; srand(time(0)); //初始化随机函数 for (ii = 0; ii<hidden; ii++) { B_input_hidden[ii] = 2 * (double)rand() / RAND_MAX - 1; //阈值变量赋随机值(-1,1) for (ij = 0; ij<input; ij++) //网络权值变量赋随机值 { W_input_hidden[ii][ij] = 2 * (double)rand() / RAND_MAX - 1; } } for (ii = 0; ii<output; ii++) { B_hidden_output[ii] = 2 * (double)rand() / RAND_MAX - 1; //阈值变量赋随机值(-1,1) for (ij = 0; ij<hidden; ij++) //网络权值变量赋随机值 { W_hidden_output[ii][ij] = 2 * (double)rand() / RAND_MAX - 1; } } precision = INT_MAX ; //初始化精度值 for (ic = 0; ic < max_learn_length; ic++) //最大学习次数内循环 { if (precision<anticipation_error) //循环剪枝函数 { break; } precision = 0; for (ii = 0; ii<sample; ii++) //21组样本循环叠加误差精度 { for (ij = 0; ij<hidden; ij++) //输入层到隐含层的输出计算 { A_input_hidden[ij] = 0.0; for (ik = 0; ik<input; ik++) { A_input_hidden[ij] += P[ik] * W_input_hidden[ij][ik]; } A_input_hidden[ij] += B_input_hidden[ij]; A_input_hidden[ij]=(double)2/(1+exp(-2*A_input_hidden[ij]))-1; } for (ij = 0; ij<output; ij++) //中间层到输出层的输出计算 { A_hidden_output[ij] = 0.0; for (ik = 0; ik<hidden; ik++) { A_hidden_output[ij] += A_input_hidden[ik] * W_hidden_output[ij][ik]; } A_hidden_output[ij] += B_hidden_output[ij]; } for(ij=0;ij<output;ij++) //隐含层到输出层的误差效能计算 { E_hidden_output[ij]=T[ij]-A_hidden_output[ij]; } for(ij=0;ij<hidden;ij++) //输入层到隐含层的误差效能计算 { E_input_hidden[ij]=0.0; for(ik=0;ik<output;ik++) { E_input_hidden[ij]+=E_hidden_output[ik]*W_hidden_output[ik][ij]; } E_input_hidden[ij]=E_input_hidden[ij]*(1-A_input_hidden[ij]); } for (ij = 0; ij<output; ij++) //通过学习率调整隐含层到输出层的网络权值和阈值 { for (ik = 0; ik<hidden; ik++) { W_hidden_output[ij][ik] += study_rate*E_hidden_output[ij]*A_input_hidden[ik]; } B_hidden_output[ij] += study_rate*E_hidden_output[ij]; } for (ij = 0; ij<hidden; ij++) //通过学习率调整输入层到隐含层的网络权值和阈值 { for (ik = 0; ik<input; ik++) { W_input_hidden[ij][ik] += study_rate*E_input_hidden[ij]*P[ik]; } B_input_hidden[ij] += study_rate*E_input_hidden[ij]; } for (ij = 0; ij<output; ij++) //计算误差精度 { precision += pow((T[ij] - A_hidden_output[ij]),2); } } } cout<<"学习后输入层到隐含层的网络权值为:"<<endl; for(ii=0;ii<hidden;ii++) { for(ij=0;ij<input;ij++) { cout<<W_input_hidden[ii][ij]; } cout<<endl; } cout<<"阈值为:"<<endl; for(ii=0;ii<hidden;ii++) { cout<<B_input_hidden[ii]; } cout<<endl<<"学习后隐含层到输出层的网络权值为:"<<endl; for(ii=0;ii<output;ii++) { for(ij=0;ij<hidden;ij++) { cout<<W_hidden_output[ii][ij]; } cout<<endl; } cout<<"阈值为:"<<endl; for(ii=0;ii<output;ii++) { cout<<B_hidden_output[ii]; } cout<<endl<<endl; cout <<"最大学习次数为:" << max_learn_length << endl; cout << "完成目标的学习次数为:" << ic << endl; cout << endl << "期望误差为:" <<setiosflags(ios::fixed) <<setprecision(10)<< anticipation_error << endl; cout << "达成目标学习后的精度为:" << precision <<endl<< endl; system("pause"); return 0; }
以上是关于BP神经网络的主要内容,如果未能解决你的问题,请参考以下文章