Computational Intelligence Experiment BP Neural Network and Its Application


1. Demand Analysis

  1. Carry out verification learning of BP neural network code
  2. Carry out the construction of BP neural network
  3. Carry out the training of BP neural network
  4. Perform BP network prediction data

2. Outline design

2.1 Neural Network Toolbox Functions

The latest version of the MATLAB neural network toolbox covers almost all the basic common types of neural networks, and provides various learning algorithms for various network models. We can call the relevant design and training functions in the toolbox according to our needs. It is very convenient to design and simulate the neural network. At present, the neural network model provided by the neural network toolbox is mainly used for:
(1) number approximation and model fitting; (2) information processing and prediction; (3) neural network control; (4) fault diagnosis.
The neural network toolbox provides a wealth of tool functions, some of which are specific to a certain type of network, and some are general,

2.2 BP neural network model

BP network is a multi-layer feed-forward neural network, which consists of input layer, hidden layer and output layer. The structure of BP network model is shown in Figure 1. There is no connection between nodes at the same layer of the network, and there can be one or more nodes at the hidden layer. The learning process of the network consists of forward and backward propagation. In the forward propagation, the input signal is transmitted layer by layer from the input layer node to the output layer node through the hidden layer node. The state of neurons in each layer only affects the neuron network in the next layer. If the output layer cannot get the expected output, then it will turn to the error backpropagation process and return the error signal along the original connection path. By modifying each layer of neurons The weights of the elements are propagated to the input layer one by one for calculation. In the forward propagation process, these two processes are used repeatedly to make the error signal minimum or meet people's expected requirements, and the learning process ends.
Figure 1-1 neural network model diagram

2.3 Concrete steps for design and guidelines using the Neural Network Toolbox

(1) Determine the information expression method: abstract the actual problem into a data form that the neural network can accept; (2)
Determine the network model: select the type and structure of the network;
(3) Select the network parameters: such as neurons, hidden Including the number of layers, etc.;
(4) Determine the training mode: select the training algorithm, determine the training deployment, specify the training target error, etc.;
(5) Network testing: select the appropriate training samples for network testing

3. Detailed design and experimental code

3.1 The general steps of data analysis using neural network are as follows:

(1) Generate training set and test set.
(2) Data normalization (optional operation)
The values ​​of the output attributes are different and belong to the same order of magnitude, and the input variables are quite different. Therefore, before building the model, the data should be normalized first. However, it should be noted that normalization is not an indispensable processing step. Specific analysis should be carried out for specific problems to decide whether to perform normalization; (3) Create and train the
model
by calling the feedforwardnet() function and train( )function.
(4) Simulation test
Call the sim function.
(5) Denormalization (optional operation)
If step 2 is performed, the simulation test result data needs to be denormalized according to the previous normalization rules to obtain the final real data. (6)
, performance analysis

3.2 Neural network testing using the Boston house dataset:

Data prediction using Boston housing data. The Boston housing data set was started in 1978, with a total of 506 data points, covering information on 14 characteristics of housing in different suburbs of Boston, Massachusetts. We want to use the first 13 characteristic data as input neurons.
(1)
insert image description here
The data format saved in the imported data housing is as follows. The 1st to 13th columns are housing attributes, and the 14th column is the housing price. For the
insert image description here
specific meaning of each data feature, please refer to The figure below:
insert image description here
(2) Generate training set and test set.

%% 随机产生训练集和测试集合
features=X;
prices=Y;
len = length(prices);
index = randperm(len);%生成1~len 的随机数,打乱样本的排序
%训练集——前70%
p_train = features(index(1:round(len*0.7)),:);%训练样本输入
t_train = prices(index(1:round(len*0.7)),:);%训练样本输出
%测试集——后30%
p_test = features(index(round(len*0.9)+1:end),:);%测试样本输入
t_test = prices(index(round(len*0.9)+1:end),:);%测试样本输出


(3) Data normalization

%%数据归一化
%输入样本归一化
[pn_train,ps1] = mapminmax(p_train');
pn_test = mapminmax('apply',p_test',ps1);
%输出样本归一化
[tn_train,ps2] = mapminmax(t_train');

(4) Create and train the model

%% BP神经网络创建,训练和仿真测试
net = feedforwardnet(5,'trainlm');%创建网络,隐含神经元个数
net.trainParam.epochs = 5000;%设置训练次数(迭代次数)
net.trainParam.goal=0.0000001;%设置收敛误差,mse均方根误差小于这个值训练结束
net.trainParam.Ir=0.5;
net.trainParam.mc=0.6;
[net,tr]=train(net,pn_train,tn_train);%训练网络

(5) Simulation test

 %% 网络仿真
b=sim(net,pn_test);%放入到网络输出数据

(6), denormalization (optional operation)

%% 结果分析和反归一化
%结果反归一化
predict_prices = mapminmax('reverse',b,ps2);

(7), performance analysis

%结果分析
t_test = t_test';
err_prices = abs(t_test-predict_prices);
figure;
subplot(2,1,1)
plot(err_prices);
title('误差')
legend({'误差值=房价预测值-房价实际值'})
xlabel('训练数据组数');
ylabel('误差值');
[mean(err_prices) std(err_prices)]%求平均,标准差
subplot(2,1,2)
plot(t_test);
hold on;
plot(predict_prices,'r');
xlim([1 length(t_test)]);
hold off;
title('波士顿房价')
legend({'房价实际值','房价预测值'})
xlabel('训练数据组数');
ylabel('波士顿房价值');

4. Test results and analysis
4.1 By modifying the function expression of feedforwardnet (feedforward neural network), that is, modifying hiddenSizes (the number of hidden neurons) and trainFcn (the function used for training network performance) in feedforwardnet (hiddenSizes, trainFcn) , to compare the accuracy of the predicted value of the neural network
(1) When net = feedforwardnet(5,'trainbr'), that is, the number of hidden neurons is 5, and the designated training function is Bayesian regularization algorithm.
insert image description here
insert image description here
insert image description here
insert image description here
(2) When net = feedforwardnet(12,'trainbr'), that is, the number of neurons is 12, and the designated training function is the Bayesian regularization algorithm.
insert image description here
insert image description here
insert image description here
(3) When net = feedforwardnet(2,'trainbr'), that is, the number of neurons is 2, and the designated training function is Bayesian regularization algorithm.
insert image description here
insert image description here
insert image description here
insert image description here

4. Summary:

In the above test results, the training samples are the first 70% and the test samples are the last 30% for summary analysis.
insert image description here

From this table, we can see that through different combinations of the number of neurons in the hidden layer and the specified training function, the error and standard deviation are basically in the range of 2-4, which proves that the overall effect of the BP neural network on data prediction is relatively good. good.
In the case of a certain number of input neurons, in the same specified training function, namely trainbr and trainlm, as the number of hidden neurons increases, the value of All Roc also increases, that is, the fitting effect is better than good.
We can also see that among the seven specified training functions, traind has the most iterations, about 3,000 times, and the learning speed is the slowest, while trainbr has more iterations, basically hundreds of times, and the learning speed is slow, but all The value of All Roc using trainbr is also basically at a high value, while the relative number of iterations of trainlm is the least, basically around a dozen times, and the learning speed is relatively fast.
At the same time, we can also see that there is no corresponding theoretical guidance for the selection of the number of network layers and the number of neurons, and the number of iterations is mainly affected by the specified training function.

insert image description here

From the above table, we can see that under certain conditions, the division of data sets often affects our accuracy. But the size of the training set and the size of the test set do not directly affect the accuracy of the predicted data. When the test set is constant, there is no linear relationship between the size of the training set and the average error; when the training set is constant, there is no linear relationship between the size of the test set and the average error; the combined size of the training set and the test set There is no linear relationship with the average value of the error.

Guess you like

Origin blog.csdn.net/qq_43741419/article/details/116451410