1, Introduction
1 Overview
BP (Back Propagation) neural network was proposed by the scientific research group headed by Rumelhart and McCelland in 1986. See their paper learning representations by Back Propagation errors published in Nature.
BP neural network is a multilayer feedforward network trained by error back propagation algorithm. It is one of the most widely used neural network models at present. BP network can learn and store a large number of inputoutput mode mapping relationships without revealing the mathematical equations describing this mapping relationship in advance. Its learning rule is to use the steepest descent method and continuously adjust the weight and threshold of the network through back propagation to minimize the sum of squares of the network error.
2 basic idea of BP algorithm
Last time, we said that multilayer perceptron encountered a bottleneck in how to obtain the weight of hidden layer. Since we can't get the weight of the hidden layer directly, can we indirectly adjust the weight of the hidden layer by getting the error between the output result and the expected output from the output layer first? BP algorithm is designed with this idea. Its basic idea is that the learning process consists of two processes: forward propagation of signal and back propagation of error.
During forward propagation, the input samples are transmitted from the input layer, processed layer by layer by hidden layer, and then transmitted to the output layer. If the actual output of the output layer is inconsistent with the expected output (teacher signal), it will turn to the back propagation stage of error.
During back propagation, the output is transmitted back to the input layer layer by layer through the hidden layer in some form, and the error is allocated to all units of each layer, so as to obtain the error signal of each layer unit, which is used as the basis for correcting the weight of each unit.
The specific processes of these two processes will be introduced later.
The signal flow diagram of BP algorithm is shown in the figure below
3 characteristic analysis of BP network  three elements of BP
When we analyze an ANN, we usually start with its three elements, namely
1) Network topology;
2) Transfer function;
3) Learning algorithm.
The characteristics of each element together determine the functional characteristics of the ANN. Therefore, we also study BP network from these three elements.
3.1 topology of BP network
As I said last time, BP network is actually a multilayer perceptron, so its topology is the same as that of multilayer perceptron. Because the single hidden layer (threelayer) perceptron can solve simple nonlinear problems, it is most widely used. The topology of the threelayer perceptron is shown in the figure below.
The simplest threetier BP:
3.2 transfer function of BP network
The transfer function adopted by BP network is a nonlinear transformation function  Sigmoid function (also known as S function). Its characteristic is that the function itself and its derivatives are continuous, so it is very convenient in processing. Why should we choose this function? I will give a further introduction when introducing the learning algorithm of BP network.
Unipolar Stype function curve is shown in the figure below.
The bipolar Stype function curve is shown in the figure below.
3.3 learning algorithm of BP network
The learning algorithm of BP network is BP algorithm, also known as BP algorithm δ Algorithm (in the learning process of ANN, we will find many terms with multiple names). Taking the threelayer perceptron as an example, when the network output is different from the expected output, there is an output error E, which is defined as follows
Next, we will introduce the specific process of BP network learning and training.
4 training decomposition of BP network
Training a BP neural network is actually adjusting the weight and bias of the network. The training process of BP neural network is divided into two parts:
Forward transmission, wave transmission output value layer by layer;
Reverse feedback, reverse layer by layer adjustment of weight and bias;
Let's first look at forward transmission.
Forward transmission (feed forward feedback)
Before training the network, we need to initialize weights and offsets randomly, take a random real number of [− 1,1] [ 1,1] [− 1,1] for each weight, and a random real number of [0,1] [0,1] [0,1] for each offset, and then start forward transmission.
The training of neural network is completed by multiple iterations. Each iteration uses all records of the training set, while each training network uses only one record. The abstract description is as follows:

while termination condition not met:

for record:dataset:

trainModel(record)
 1
 2
 3
 4
4.1 back propagation
4.2 training termination conditions
Each round of training uses all records of the data set, but when to stop, there are two stopping conditions:
Set the maximum number of iterations, such as stopping training after 100 iterations with the dataset
Calculate the prediction accuracy of the training set on the network, and stop the training after reaching a certain threshold
5. Specific process of BP network operation
5.1 network structure
There are n nn neurons in the input layer, p pp neurons in the hidden layer and q qq neurons in the output layer.
5.2 variable definition
Step 9: judge the rationality of the model
Judge whether the network error meets the requirements.
When the error reaches the preset accuracy or the learning times are greater than the designed maximum times, the algorithm is ended.
Otherwise, select the next learning sample and the corresponding output expectation, return to the third part and enter the next round of learning.
6 design of BP network
In the design of BP network, we should generally consider the number of layers of the network, the number of neurons and activation function in each layer, initial value and learning rate. The following are some selection principles.
6.1 layers of network
Theory has proved that the network with deviation and at least one Stype hidden layer plus a linear output layer can approach any rational function. Increasing the number of layers can further reduce the error and improve the accuracy, but it is also the complexity of the network. In addition, the singlelayer network with only nonlinear activation function can not be used to solve the problem, because the problem that can be solved with singlelayer network can also be solved with adaptive linear network, and the operation speed of adaptive linear network is faster. For the problem that can only be solved with nonlinear function, the singlelayer accuracy is not high enough, and only increasing the number of layers can achieve the desired result.
6.2 number of neurons in the hidden layer
The improvement of network training accuracy can be obtained by using a hidden layer and increasing the number of neurons, which is much simpler in structure than increasing the number of network layers. Generally speaking, we use the accuracy and the time of training the network to measure the quality of a neural network design:
(1) When the number of neurons is too small, the network can not learn well, the number of training iterations is relatively large, and the training accuracy is not high.
(2) When the number of iterations of fitting is too large, the more powerful the fitting function of the network may be, the more the fitting function is.
Therefore, we get the selection principle of the number of neurons in the hidden layer of neural network: on the premise of solving the problem, add one or two neurons to speed up the decline of error.
6.3 selection of initial weight
Generally, the initial weight is a random number with a value between (− 1,1). In addition, after analyzing how the twolayer network trains a function, wedrow et al. Proposed a strategy to select the initial weight order as s √ r, where r is the number of inputs and S is the number of neurons in the first layer.
6.4 learning rate
The learning rate is generally 0.01 − 0.8. A large learning rate may lead to the instability of the system, but a small learning rate leads to too slow convergence and requires a long training time. For more complex networks, different learning rates may be required at different positions of the error surface. In order to reduce the training times and time to find the learning rate, a more appropriate method is to use the variable adaptive learning rate to set different learning rates in different stages.
6.5 selection of expected error
In the process of designing the network, the expected error value should also determine an appropriate value after comparative training, which is determined relative to the required number of hidden layer nodes. In general, two networks with different expected error values can be trained at the same time, and finally one of them can be determined by comprehensive factors.
7 limitations of BP network
BP network has the following problems:
(1) It needs a long training time: This is mainly caused by the small learning rate, which can be improved by changing or adaptive learning rate.
(2) Completely unable to train: This is mainly reflected in the paralysis of the network. Generally, in order to avoid this situation, one is to select a smaller initial weight, but use a smaller learning rate.
(3) Local minimum value: the gradient descent method adopted here may converge to the local minimum value, and it is possible to obtain better results by using multilayer network or more neurons.
8 improvement of BP network
The main goal of the improvement of P algorithm is to speed up the training speed and avoid falling into local minima. The common improvement methods include the algorithm with momentum factor, adaptive learning rate, changing learning rate and action function contraction method. The basic idea of momentum factor method is to add a value proportional to the previous weight change to each weight change on the basis of back propagation, and generate a new weight change according to the back propagation method. The method of adaptive learning rate is aimed at some specific problems. The principle of the method of changing the learning rate is that if the sign of the objective function for the reciprocal of a weight is the same in several consecutive iterations, the learning rate of this weight will increase, on the contrary, if the sign is opposite, the learning rate will decrease. The contraction rule of the action function is to translate the action function, that is, add a constant.
2, Source code
lc close all clear all load A_fear fearVec; load F_happiness hapVec; load N_neutral neutralVec; load T_sadness sadnessVec; load W_anger angerVec; trainsample(1:30,1:140)=angerVec(:,1:30)'; trainsample(31:60,1:140)=hapVec(:,1:30)'; trainsample(61:90,1:140)=neutralVec(:,1:30)'; trainsample(91:120,1:140)=sadnessVec(:,1:30)'; trainsample(121:150,1:140)=fearVec(:,1:30)'; trainsample(1:30,141)=1; trainsample(31:60,141)=2; trainsample(61:90,141)=3; trainsample(91:120,141)=4; trainsample(121:150,141)=5; testsample(1:20,1:140)=angerVec(:,31:50)'; testsample(21:40,1:140)=hapVec(:,31:50)'; testsample(41:60,1:140)=neutralVec(:,31:50)'; testsample(61:80,1:140)=sadnessVec(:,31:50)'; testsample(81:100,1:140)=fearVec(:,31:50)'; testsample(1:20,141)=1; testsample(21:40,141)=2; testsample(41:60,141)=3; testsample(61:80,141)=4; testsample(81:100,141)=5; class=trainsample(:,141); sum=bpnn(trainsample,testsample,class); figure(1) bar(sum,0.5); set(gca,'XTickLabel',{'get angry','happy','neutral','Sadness','fear'}); ylabel('Recognition rate'); xlabel('Five basic emotions'); p_train=trainsample(:,1:140)'; t_train=trainsample(:,141)'; p_test=testsample(:,1:140)'; t_test=testsample(:,141)'; sumpnn=pnn(p_train,t_train,p_test,t_test); figure(2) bar(sumpnn,0.5); set(gca,'XTickLabel',{'get angry','happy','neutral','Sadness','fear'}); ylabel('Recognition rate'); xlabel('Five basic emotions'); sumlvq=lvq(trainsample,testsample,class); function sum=bpnn(trainsample,testsample,class) %Input parameters: trainsample It's a training sample,testsample It's a test sample,class Indicates the category of training samples, and trainsample Data correspondence in %sum: Recognition rate of five basic emotions for i=1:140 feature(:,i)= trainsample(:,i); end %Eigenvalue normalization [input,minI,maxI] = premnmx( feature') ; %Construct output matrix s = length( class ) ; output = zeros( s , 5 ) ; for i = 1 : s output( i , class( i ) ) = 1 ; end %Creating neural networks net = newff( minmax(input) , [10 5] , { 'logsig' 'purelin' } , 'traingdx' ) ; %Create feedforward neural network %Set training parameters net.trainparam.show = 50 ; net.trainparam.epochs = 150 ; net.trainparam.goal = 0.1 ; net.trainParam.lr = 0.05 ; %Start training net = train( net, input , output' ) ; %Read test data for i=1:140 featuretest(:,i)= testsample(:,i); end c=testsample(:,141); %Normalization of test data testInput = tramnmx(featuretest' , minI, maxI ) ; %simulation Y = sim( net , testInput ) sum=[0 0 0 0 0]; %Number of correct recognition of each type of emotion %Count the number of correctly identified samples for i=1:20 if Y(1,i)>Y(2,i)&&Y(1,i)>Y(3,i)&&Y(1,i)>Y(4,i)&&Y(1,i)>Y(5,i) sum(1)=sum(1)+1; end function sumlvq=lvq(trainsample,testsample,class) P=trainsample(:,1:140)'; C=class'; T=ind2vec(C); net=newlvq(minmax(P),20,[0.2 0.2 0.2 0.2 0.2],0.1); %establish lvq network w1=net.IW{1}; net.trainParam.epochs=100; net=train(net,P,T); y=sim(net,testsample(:,1:140)'); y3c=vec2ind(y); sumlvq=[0 0 0 0 0]; %Number of correct recognition of each type of emotion %Count the number of correctly identified samples for i=1:20 if y3c(i)==1 sumlvq(1)=sumlvq(1)+1; end end for i=21:40 if y3c(i)==2 sumlvq(2)=sumlvq(2)+1; end end for i=41:60 if y3c(i)==3 sumlvq(3)=sumlvq(3)+1; end end for i=61:80 if y3c(i)==4 sumlvq(4)=sumlvq(4)+1; end end for i=81:100 end
3, Operation results
4, Remarks
Complete code or write on behalf of QQ1575304183