zoukankan      html  css  js  c++  java
  • 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,研究方向
       
    图片


    •I->H (输入层à隐含层)

      a 1 = tansig (IW 1,1 p 1 +b 1 )

      tansig(x)=tanh(x)=(ex-e-x)/(ex+e-x)

    •H->O (隐含层à输出层)

      a 2 = purelin (LW 2,1 a 1 +b 2 )

    •输入层神经元个数  n
    •隐含层神经元个数  s1
    •输出层神经元个数  s2
    •输入q组样本  p1,p2,...,pq    pi∈Rn
    •期望输出 T1,T2,...,Tq,     T ∈Rs1
    •网络输出  a21,a22,...,a2q  a2∈Rs2
    •解决方法  误差最小
    •实质为一个优化问题
    •思路1: 梯度法(gradient)
    •找出误差与加权系数的关系
    •得到加权系数改变的规律


    图片

    图片

    •输出层误差 ej(j=1,2,…,s2)
    •隐含层误差 ei(i=1,2,…,n1)
    •ei与ej的关系?
    •ei可以认为是由ej加权组合形成的。由于作用函数的存在,ej的等效作用为δj =ejf '()

    图片


    图片

     

    3,程序设计

    图片   

      1 #include<iostream>
      2 #include<cstdlib>
      3 #include<fstream>
      4 #include<ctime>
      5 #include<cmath>
      6 #include<iomanip>
      7 using namespace std;
      8 
      9 const int max_learn_length = 2000;      //最大学习次数
     10 const double study_rate = 0.01;          //学习率(数据调整步长值)
     11 const double anticipation_error = 0.0001;  //期望误差
     12 const int input = 1;                    //每组样本输入层数据量
     13 const int hidden = 5;                   //每组样本隐含层数据量
     14 const int output = 1;                   //每组样本输出层数据量
     15 
     16 int sample;                      //样本数量
     17 double *P;                //输入矢量
     18 double *T;                //输出矢量
     19 
     20 void readfile()
     21 {
     22     fstream p,t;
     23     p.open("p.txt",ios::in);
     24     t.open("t.txt",ios::in);
     25     if(!p||!t)
     26     {
     27         cout<<"file load failed!"<<endl;
     28         exit(0);
     29     }
     30     p>>sample;
     31     t>>sample;
     32     P=new double[sample];
     33     T=new double[sample];
     34     for(int ii=0;!p.eof()&&!t.eof();ii++)
     35     {
     36         p>>P[ii];
     37         t>>T[ii];
     38     }
     39     p.close();
     40     t.close();
     41 }
     42 
     43 void displaydata()
     44 {
     45     cout<<"sample="<<sample<<endl;
     46     cout<<setiosflags(ios::fixed)<<setprecision(6);
     47     for(int ii=0;ii<sample;ii++)
     48     {
     49         cout<<"P["<<ii<<"]="<<P[ii]<<" T["<<ii<<"]="<<T[ii]<<endl;
     50     }
     51 }
     52 
     53 int main(int argc, char **argv)
     54 {
     55     double precision;                           //误差精度变量
     56     double W_input_hidden[hidden][input];       //输入层到隐含层的网络权值变量
     57     double W_hidden_output[output][hidden];     //隐含层到输出层的网络权值变量
     58     double B_input_hidden[hidden];              //输入层到隐含层阈值变量
     59     double B_hidden_output[output];             //隐含层到输出层阈值变量
     60     double E_input_hidden[hidden];              //输入层到隐含层误差
     61     double E_hidden_output[output];             //隐含层到输出层误差
     62     double A_input_hidden[hidden];                    //隐含层实际输出值
     63     double A_hidden_output[output];                    //输出层实际输出值
     64     int ii, ij, ik, ic;
     65 
     66     readfile();
     67     displaydata();
     68 
     69     srand(time(0));                            //初始化随机函数
     70 
     71     for (ii = 0; ii<hidden; ii++)
     72     {
     73         B_input_hidden[ii] = 2 * (double)rand() / RAND_MAX - 1;  //阈值变量赋随机值(-1,1)
     74         for (ij = 0; ij<input; ij++)                             //网络权值变量赋随机值 
     75         {
     76             W_input_hidden[ii][ij] = 2 * (double)rand() / RAND_MAX - 1;
     77         }
     78     }
     79     for (ii = 0; ii<output; ii++)
     80     {
     81         B_hidden_output[ii] = 2 * (double)rand() / RAND_MAX - 1;  //阈值变量赋随机值(-1,1)
     82         for (ij = 0; ij<hidden; ij++)                             //网络权值变量赋随机值 
     83         {
     84             W_hidden_output[ii][ij] = 2 * (double)rand() / RAND_MAX - 1;
     85         }
     86     }
     87 
     88     precision = INT_MAX ;                      //初始化精度值
     89     for (ic = 0; ic < max_learn_length; ic++)  //最大学习次数内循环
     90     {
     91         if (precision<anticipation_error)      //循环剪枝函数
     92         {
     93             break;
     94         }
     95         precision = 0;
     96         for (ii = 0; ii<sample; ii++)          //21组样本循环叠加误差精度
     97         {
     98             for (ij = 0; ij<hidden; ij++)      //输入层到隐含层的输出计算
     99             {
    100                 A_input_hidden[ij] = 0.0;
    101                 for (ik = 0; ik<input; ik++)  
    102                 {
    103                     A_input_hidden[ij] += P[ik] * W_input_hidden[ij][ik];
    104                 }
    105                 A_input_hidden[ij] += B_input_hidden[ij];
    106                 A_input_hidden[ij]=(double)2/(1+exp(-2*A_input_hidden[ij]))-1;
    107             }
    108             for (ij = 0; ij<output; ij++)      //中间层到输出层的输出计算
    109             {
    110                 A_hidden_output[ij] = 0.0;
    111                 for (ik = 0; ik<hidden; ik++)  
    112                 {
    113                     A_hidden_output[ij] += A_input_hidden[ik] * W_hidden_output[ij][ik];
    114                 }
    115                 A_hidden_output[ij] += B_hidden_output[ij];
    116             }
    117 
    118             for(ij=0;ij<output;ij++)           //隐含层到输出层的误差效能计算
    119             {
    120                 E_hidden_output[ij]=T[ij]-A_hidden_output[ij];
    121             }
    122             for(ij=0;ij<hidden;ij++)           //输入层到隐含层的误差效能计算
    123             {
    124                 E_input_hidden[ij]=0.0;
    125                 for(ik=0;ik<output;ik++)
    126                 {
    127                     E_input_hidden[ij]+=E_hidden_output[ik]*W_hidden_output[ik][ij];
    128                 }
    129                 E_input_hidden[ij]=E_input_hidden[ij]*(1-A_input_hidden[ij]);
    130             }
    131 
    132             for (ij = 0; ij<output; ij++)      //通过学习率调整隐含层到输出层的网络权值和阈值
    133             {
    134                 for (ik = 0; ik<hidden; ik++)
    135                 {
    136                     W_hidden_output[ij][ik] += study_rate*E_hidden_output[ij]*A_input_hidden[ik];
    137                 }
    138                 B_hidden_output[ij] += study_rate*E_hidden_output[ij];
    139             }
    140             for (ij = 0; ij<hidden; ij++)      //通过学习率调整输入层到隐含层的网络权值和阈值
    141             {
    142                 for (ik = 0; ik<input; ik++)
    143                 {
    144                     W_input_hidden[ij][ik] += study_rate*E_input_hidden[ij]*P[ik];
    145                 }
    146                 B_input_hidden[ij] += study_rate*E_input_hidden[ij];
    147             }
    148 
    149             for (ij = 0; ij<output; ij++)   //计算误差精度
    150             {
    151                 precision += pow((T[ij] - A_hidden_output[ij]),2);
    152             }
    153         }
    154     }
    155     cout<<"学习后输入层到隐含层的网络权值为:"<<endl;
    156     for(ii=0;ii<hidden;ii++)
    157     {
    158         for(ij=0;ij<input;ij++)
    159         {
    160             cout<<W_input_hidden[ii][ij];
    161         }
    162         cout<<endl;
    163     }
    164     cout<<"阈值为:"<<endl;
    165     for(ii=0;ii<hidden;ii++)
    166     {
    167         cout<<B_input_hidden[ii];
    168     }
    169     cout<<endl<<"学习后隐含层到输出层的网络权值为:"<<endl;
    170     for(ii=0;ii<output;ii++)
    171     {
    172         for(ij=0;ij<hidden;ij++)
    173         {
    174             cout<<W_hidden_output[ii][ij];
    175         }
    176         cout<<endl;
    177     }
    178     cout<<"阈值为:"<<endl;
    179     for(ii=0;ii<output;ii++)
    180     {
    181         cout<<B_hidden_output[ii];
    182     }
    183     cout<<endl<<endl;
    184     cout <<"最大学习次数为:" << max_learn_length << endl;
    185     cout << "完成目标的学习次数为:" << ic << endl;
    186     cout << endl << "期望误差为:" <<setiosflags(ios::fixed)
    187         <<setprecision(10)<< anticipation_error << endl;
    188     cout << "达成目标学习后的精度为:" << precision <<endl<< endl;
    189     system("pause");
    190     return 0;
    191 }

    p.txt

     1 21
     2 -1
     3 -0.9
     4 -0.8
     5 -0.7
     6 -0.6
     7 -0.5
     8 -0.4
     9 -0.3
    10 -0.2
    11 -0.1
    12 0
    13 0.1
    14 0.2
    15 0.3
    16 0.4
    17 0.5
    18 0.6
    19 0.7
    20 0.8
    21 0.9
    22 1

    t.txt

     1 21
     2 -0.9602
     3 -0.5770
     4 -0.0729
     5 0.3771
     6 0.6405
     7 0.6600
     8 0.4609
     9 0.1336
    10 -0.2013
    11 -0.4344
    12 -0.5000
    13 -0.3930
    14 -0.1647
    15 0.0988
    16 0.3072
    17 0.3960
    18 0.3449
    19 0.1816
    20 -0.0312
    21 -0.2189
    22 -0.3201
  • 相关阅读:
    聊天系统Demo,增加文件传送功能(附源码)-- ESFramework 4.0 快速上手(14)
    文件传送,如此简单--ESFramework 4.0 快速上手(13)
    PAT B1012.数字分类
    PAT B1046.猜拳
    PAT B1016.部分A+B(15)
    记录:挑战搭建一个简易的成绩管理系统的数据库
    mysql学习记录(一)
    使用foreach一次性添加多个单选按钮
    通过metaclass实现精简的ORM框架
    高级BASH
  • 原文地址:https://www.cnblogs.com/guanghe/p/5556705.html
Copyright © 2011-2022 走看看