Neural network based on Eorr back propagation typical BP networks C + + implementation

Source: Internet
Author: User

Reference: Artificial neural network-Han Liqun ppt   looking at some of the language models based on neural networks, compared with traditional language models, there is no need for additional smoothing algorithms In addition to the amount of computational effort, which makes them surprisingly effective. These networks can see the shadow of BP, it can be said that the BP network is the most basic, master solid, understanding of other structures will be more profound, so early in learning the language model before I have written in C + + A simple BP network, although the function is simple, only the most basic three-layer structure, But let yourself understand the error back more deeply. At that time, I have not started to blog, now put the previous code up, that time to write code did not consider any optimization, to achieve the function, so many modules did not make a function, and did not consider how to speed up the network, such as matrix multiplication, I use the most honest multi-loop to calculate. Finally, this network is used to give a simple example-using this BP network to calculate the XOR operation. This is easier to understand, the network first trained, by the human given the different or operation of the sample rules, and then let it self-learning, and finally the test module (at that time I wrote the function called work, then did not know the exact name should be test).
This article does not deduce the BP network, because the online random search is many, below is a typical three-storey structure, the figure is from artificial neural network-Han Liqun ppt

Here is the code, the code is organized in accordance with the standard formula deduced, previously written this code when referring to the Internet, but now do not find where it is, there is no way to quote. The code is directly below, because the comments are very detailed, so the process does not explain.
first, the structure of the BP network is defined in BpNet.h, which reads as follows:
#include <iostream> #include <fstream> #include <ctime> #include <cmath>using namespace std;/* Constant area */#define N 4//Samples number of # define in 2//input layer neurons # define HN 2//hidden layer Neurons number # # on 1//output layer neurons//define the structure of the Learning sample class studydata{ Public:float input[in];//Input Sample float teach[on];//desired output (teacher signal) Studydata (); virtual ~studydata ();};/ /define the class structure of the BP neural network class Bpnet {public:void getoutput ();//input data from the keyboard, output to save void work (char *weight, char *threshold);// Work on the trained data double getsumerr ();//Get total error//training void Train (char *samplefilename, Char *weight, char *threshold); void Readweight (char *weight, char *threshold);//Read weight threshold into the neural network void savebpnet (char *weight, char *threshold);//Save weight Void Updateweight (int m);//update weights void errorsignal (int m);//Calculation error signal void Netinputoutput (int m);//calculate each layer output input void Gettrainingdata ( char *samplefilename);//Get sample set void Startshow (void) from external memory; Studydata studydata[n];//Storage Array of multiple learning samples float w[hn][in];//input layer to hidden layer weight array float v[on][hn];//hidden layer to output layer weight array float hu_hn[hn];// Implicit layer neuron threshold array float hu_on[on];//output layer neuron threshold array float in_hn[hn];//input flThe output of the input float out_on[on];//output layer of the in_on[on];//output layer of the oat out_hn[hn];//the output float e[n];//the sample group error array, each component is a set of sample error float sturate1;//output layer to hidden layer learning efficiency float sturate2;//layer to input layer learning efficiency float ERRSIGNALON[ON];//ΔK, output layer error signal array float errsignalhn[hn];//δj, An array of error signals in the hidden layer bpnet (); virtual ~bpnet ();};


then the implementation of BpNet.h in BpNet.cpp, the content is as follows:
BpNet.cpp:implementation of the Bpnet class.//////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "BpNet.h"/////////////////////////////////////////////////////////////////////// /Construction/destruction///////////////////////////////////////////////////////////////////////*bpnet Constructor, Initialize the variable */bpnet::bpnet () {Srand (Time (NULL)),///random number seed int I, j;for (i=0; i

Finally, the contents of the main function, used to do an XOR test, the content is as follows:sample1.txt content is as follows:0 0 0
0 1 1
1 0 1
1 1 0
rules that represent an XOR operation
#include "stdafx.h" #include "BpNet.h" int main (int argc, char* argv[]) {/*BP is divided into two phases: the function used for training train (), the function work () used for the job () */ Bpnet bp;/* or sample training *///bp. Train ("Sample1.txt", "Weight1.txt", "Yuzhi1.txt"), or/* Perform work tests on the results of or sample training */bp. Work ("Weight1.txt", "Yuzhi1.txt"); return 0;}

The final test result diagram is as follows:


Neural network based on Eorr back propagation typical BP networks C + + implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.