BP neural network implementation example 2 gasoline octane number prediction in matlab

1. Introduction

1. First get the data, for example, the data is imported from exce l, or it can be a ready-made .mat file. 60 groups of gasoline samples were scanned by Fourier near-infrared transform spectrometer. The scanning range was 900-1700 nm, and the scanning interval was 2 nm. The spectral curve of each sample contained 401 wavelength points.
2. Some functions that need to be used
Normalization function (mapminmax)

```[Y,P] = mapminmax(X,YMIN,YMAX)
YMIN is we expect the normalized matrix Y the minimum value for each row,
YMAX is we expect the normalized matrix Y The maximum value for each row.

Y = mapminmax('apply',X,PS)
PS is the mapping of the training samples, and the test samples should be preprocessed in the same way as the training samples. just map PS apply to the test sample.;apply use the previous method
```

Why normalize?
The units of input data are different, and the range of some data may be particularly large, resulting in slow neural network convergence and long training time.
newff (feedforward backpropagation network)

```net = newff(P,T,S)                             % Both definitions can be
net = newff(P,T,S,TF,BTF,BLF,PF,IPF,OPF,DDF)
P: Input parameter matrix.(RxQ1)，in Q1 represent R The input vector of elements. Its data meaning is a matrix P Have Q1 columns, each column is a sample, and each sample has R attributes (features). general matrix P need to be normalized, i.e. P Each row of is normalized to[0 1]or[-1 1].
T: target parameter matrix.(SNxQ2)，Q2 represent SN Meta target vector.
S: N-1 the number of hidden layers ( S(i)arrive S(N-1))，Default is empty matrix[]. The number of units in the output layer SN depending on T. return N layer feedforward BP Neural Networks
TF: The transfer function of the relevant layer, the default hidden layer is tansig function, the output layer is purelin function.
BTF: BP The neural network learns the training function, the default value is trainlm function.
BLF: Weight learning function, the default value is learngdm.
PF: performance function, default is mse，optional sse，sae，mae，crossentropy.
IPF，OPF，DDF All are default values.

2，Transfer Function TF
purelin:  Linear transfer function.
tansig : Tangent S type transfer function.
logsig : logarithm S type transfer function.
The selection pair of hidden layer and output layer functions BP The prediction accuracy of the neural network has a great influence. Generally, the hidden layer node transfer function is selected. tansig function or logsig function, output layer node transfer function selection tansig function or purelin function.
３，Learn the training function BTF
traingd: steepest descent BP algorithm.
traingdm: momentum BP algorithm.
trainda: Steepest descent with variable learning rate BP algorithm.
traindx: Momentum with variable learning rate BP algorithm.
trainrp: Elastic Algorithm.
traincgf(Fletcher-Reeves correction algorithm)
traincgp(Polak_Ribiere correction algorithm)
traincgb(Powell-Beale reset algorithm)
trainbfg(BFGS Quasi-Newton algorithm)
trainoss(OSS algorithm)

４，Parameter Description
pass net.trainParam parameters can be viewed
Show Training Window Feedback showWindow: true
Show Command Line Feedback showCommandLine: false
Command Line Frequency show: Number of training sessions between displays
Maximum Epochs epochs: training times
Maximum Training Time time: Maximum training time (seconds)
Performance Goal goal: network performance goals
Maximum Validation Checks max_fail: Maximum number of verification failures
Learning Rate lr: learning rate
Learning Rate Increase lr_inc: learning rate increase
Learning Rate lr_dec: Learning rate drop value
Maximum Performance Increase max_perf_inc:
Momentum Constant mc: Momentum factor
```

2. Example 2 Prediction of gasoline octane number

1. Problem introduction
background
Octane number is the most important quality indicator of gasoline. Traditional laboratory testing methods have problems such as large sample consumption, long testing cycle and high cost, which are not suitable for production control, especially online testing. The near-infrared spectral analysis method (NIR) developed in recent years, as a rapid analysis method, has been widely used in agriculture, pharmacy, biochemical industry, petroleum products and other fields. Its advantages are non-destructive testing, low cost, no pollution, online analysis, more suitable for production and control needs.
Problem Description
Aiming at the 60 groups of gasoline samples collected, Fourierto near-infrared transform spectrometer was used to scan them. The scanning range was 900-1700 nm, and the scanning interval was 2 nm. The spectral curve of each sample contained a total of 401 wavelength points and the near-infrared spectrum of the sample. The curve is shown in Figure 25-3. At the same time, its octane number content was determined by traditional laboratory testing methods. Now it is required to use BP and RBF neural networks to establish mathematical models between the near-infrared spectrum of gasoline samples and their octane number respectively, and to evaluate the performance of the models.
Model building
According to the requirements in the problem description, the model establishment and performance evaluation of BEP and RBF neural networks can be roughly divided into the following steps.

2.matlab code steps

```%% clear environment variables
%save: save( 'filename', 'net')filename can contain paths, such as D:\matlab\.. ..
clear all
clc

%% II. Training set/Test set generation
% 1.Import data, 60 groups of gasoline samples, and scan them with Fourier near-infrared transform spectrometer, with a scanning range of 900~1700nm，Scan interval 2 nm，The spectral curve of each sample contains a total of 401 wavelength points.

% 2. Randomly generate training and test sets
%size(a,1)represents the number of rows of the display matrix, size(a,2)Indicates the number of columns in the display matrix
%randperm(60)Generate 60 shuffled sequences
temp = randperm(size(NIR,1));
% Training set - 50 samples
P_train = NIR(temp(1:50),:)';%Take out the first 50 rows of all column data as the training set input, transpose
T_train = octane(temp(1:50),:)';%Take out the first 50 rows of all column data as the training set output, transpose
% Test set - 10 samples
P_test = NIR(temp(51:end),:)';
T_test = octane(temp(51:end),:)';
N = size(P_test,2);

%% 3.data normalization
%Map data to a reasonable value range to speed up training
[p_train, ps_input] = mapminmax(P_train,0,1);%normalized calling function mapminmax，normalized data at 0-1 between and exist p_train In the matrix, the normalization method exists ps_input
p_test = mapminmax('apply',P_test,ps_input);%apply Using the previous method P_test Normalized

[t_train, ps_output] = mapminmax(T_train,0,1);

%% 4. BP Neural network creation, training and simulation testing
% 1. Create a network
net = newff(p_train,t_train,9);%9 hidden neurons

% 2. Set training parameters
net.trainParam.epochs = 1000;%number of iterations
net.trainParam.goal = 1e-3;%stop target
net.trainParam.lr = 0.01;%learning rate

% 3. train the network
net = train(net,p_train,t_train);

% 4. Simulation test, use the input data of the test sample to simulate, and test the error of the output
t_sim = sim(net,p_test);

% 5. Data de-normalization, restore test data, and compare with real data
T_sim = mapminmax('reverse',t_sim,ps_output);

%% V. Performance evaluation
% 1. Relative error error
error = abs(T_sim - T_test)./T_test;

%%
% 2. decisive factor R^2
R2 = (N * sum(T_sim .* T_test) - sum(T_sim) * sum(T_test))^2 / ((N * sum((T_sim).^2) - (sum(T_sim))^2) * (N * sum((T_test).^2) - (sum(T_test))^2));

%%
% 3. Comparative Results
result = [T_test' T_sim' error']

%% VI. drawing
figure
plot(1:N,T_test,'b:*',1:N,T_sim,'r-o')
legend('actual value','Predictive value')
xlabel('prediction sample')
ylabel('Octane number')
string = {'Comparison of prediction results of octane number content in test set';['R^2=' num2str(R2)]};
title(string)
```

retrain once

Later, it is modified according to the number of neurons or the number of hidden layers or the training optimization algorithm.
Expression 1:

```net=newff([T,C,{'tansig','purelin'},'traingd');
%net = newff(T,C,[The number of neurons in the hidden layer, the number of neurons in the output layer],{The transfer function of the hidden layer neurons, the transfer function of the output layer｝,'The training function for backpropagation'),in p for input data, t for output data
%The transfer function of the neural network, defaults to'tansig'The function is the transfer function of the hidden layer
%purelin The function is the transfer function of the output layer, as follows
%TF1 = 'tansig';TF2 = 'logsig';
%TF1 = 'logsig';TF2 = 'purelin';
%TF1 = 'logsig';TF2 = 'logsig';
%TF1 = 'purelin';TF2 = 'purelin';
TF1  = 'tansig';TF2 = 'purelin';
```

Expression 2:

```net=newff(T,C,{TF1 TF2},'traingdm');%network creation
%net.trainParam.epochs=10000;%Training times settings
%net.trainParam.goal=1e-7;%training goal setting
%net.trainParam.lr=0.01;%Learning rate settings,It should be set to a small value. If it is too large, it will start to speed up the convergence, but when it approaches the optimal point, it will cause turbulence and make it impossible to converge.
%net.trainParam.mc=0.9;%The setting of the momentum factor, the default is 0.9
%net.trainParam.show=25;%number of intervals displayed
%%
net.trainFcn = 'traingd'; % Gradient Descent Algorithm
net.trainFcn = 'traingdm'; % Momentum Gradient Descent Algorithm
net.trainFcn = 'traingda'; % Variable Learning Rate Gradient Descent Algorithm
net.trainFcn = 'traingdx'; % Variable Learning Rate Momentum Gradient Descent Algorithm

% (Preferred algorithm for large networks)

net.trainFcn = 'trainrp'; % RPROP(elasticity BP)algorithm,Minimal memory requirements

net.trainFcn = 'traincgf'; %Fletcher-Reeves Correction algorithm
net.trainFcn = 'traincgp'; %Polak-Ribiere Correction algorithm,memory requirement ratio Fletcher-Reeves The correction algorithm is slightly larger
net.trainFcn = 'traincgb'; % Powell-Beal Reset algorithm,memory requirement ratio Polak-Ribiere The correction algorithm is slightly larger

% (Preferred algorithm for large networks)

net.trainFcn = 'trainscg'; % ScaledConjugate Gradient algorithm,memory requirements and Fletcher-Reeves The correction algorithm is the same,The amount of calculation is much smaller than the above three algorithms
net.trainFcn = 'trainbfg'; %Quasi-Newton Algorithms - BFGS Algorithm,Both the computation and memory requirements are larger than the conjugate gradient algorithm,But it converges faster
net.trainFcn = 'trainoss'; % OneStep Secant Algorithm,Computational amount and memory requirement BFGS Algorithm is small,Slightly larger than the conjugate gradient algorithm

% (Preferred algorithm for medium-sized networks)

net.trainFcn = 'trainlm'; %Levenberg-Marquardt algorithm,Maximum memory requirement,Fastest convergence
net.trainFcn = 'trainbr'; % Bayesian regularization algorithm
The five representative algorithms are::'traingdx','trainrp','trainscg','trainoss', 'trainlm'
%Here is usually selected'trainlm'function to train, and its arithmetic pair corresponds to Levenberg-Marquardt algorithm
```

Posted by adamlacombe on Sat, 01 Oct 2022 08:23:02 +0530