An example to explain how to use BP neural network (with code)

Original articles on this site, please explain the reprint from "Old Cake Explanation-BP Neural Network" bp.bbbdata.com

BP neural network is a widely used model.
This article uses an example as the main line to explain how to build and use a BP neural network for beginners. It
helps beginners who have not been exposed to BP neural network to quickly get started with BP neural network.

Table of contents

1. How to build a BP neural network  

1.1 Problem statement

1.2 Introduction and implementation of BP neural network model

2. How to use BP neural network

2.1 How to use the trained BP neural network for prediction

2.2 About the extraction of formulas


1. How to build a BP neural network  

This section explains how to build a BP neural network

1.1 Problem statement


The existing data is as follows

x_1,x_2For the input, \text{y}for the corresponding output,
it is now necessary to train a BP neural network, and x_1,x_2predict\text{y} 



1.2 Introduction and implementation of BP neural network model


BP neural network refers to building the following model to predict data

Its mathematical expression is as follows

     
For the above model, the number of nodes in the hidden layer (that is, the number of tansig) needs to be set by ourselves.
tansig is the default common function of BP, and we can also replace it with other functions.
After setting the number of hidden nodes, we use the software It is enough to solve w and b in the model.
Generally, matlab is used to solve the problem, and the effect will be better than that of python.
The matlab code is as follows

% 数据生成
x1 = [-3,-2.7,-2.4,-2.1,-1.8,-1.5,-1.2,-0.9,-0.6,-0.3,0,0.3,0.6,0.9,1.2,1.5,1.8];   % x1:x1 = -3:0.3:2;
x2 = [-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,-2.2204,0.2,0.4,0.6,0.8,1,1.2]; % x2:x2 = -2:0.2:1.2;
y  = [0.6589,0.2206,-0.1635,-0.4712,-0.6858,-0.7975,-0.8040,...
          -0.7113,-0.5326,-0.2875 ,0,0.3035,0.5966,0.8553,1.0600,1.1975,1.2618];    % y: y = sin(x1)+0.2*x2.*x2;

inputData  = [x1;x2];                       % 将x1,x2作为输入数据
outputData = y;                             % 将y作为输出数据
setdemorandstream(88888);                   % 指定随机种子,这样每次训练出来的网络都一样

%使用用输入输出数据(inputData、outputData)建立网络
%隐节点个数设为3.其中隐层、输出层的传递函数分别为tansig和purelin,使用trainlm方法训练
net = newff(inputData,outputData,3,{'tansig','purelin'},'trainlm');

%设置一些常用参数,然后训练网络
net.trainparam.goal = 0.0001;               % 训练目标:均方误差低于0.0001
net.trainparam.show = 400;                  % 每训练400次展示一次结果
net.trainparam.epochs = 15000;              % 最大训练次数:15000.
[net,tr] = train(net,inputData,outputData); % 调用matlab神经网络工具箱自带的train函数训练网络

% 查看网络的预测结果
simout = sim(net,inputData);                % 调用matlab神经网络工具箱自带的sim函数得到网络的预测值
figure;                                     % 新建画图窗口窗口
t=1:length(simout);
plot(t,y,t,simout,'r')                      % 画图,对比原来的y和网络预测的y

After running the code, the fitting effect diagram of the training data is as follows
 


It can be seen that for the training data, the prediction of the network is basically consistent with the original data


2. How to use BP neural network

     
This section explains how to use the trained BP neural network to make predictions

2.1 How to use the trained BP neural network for prediction


After the network is trained, you can use the network to predict new samples.
If you want to know the value of x1=0.5, x2=0.5,
you can enter the following code

x    =[0.5;0.5];          
simy = sim(net,x)     

The command window output is as follows

 In this way, when the input is [0.5,0.5], the predicted value of y is obtained. The
above mentioned is to use the prediction function sim of the software package to make predictions
. In fact, the BP neural network is the above mathematical expression, and
we can also Extract the w and b in the model, and substitute them into the formula for prediction
\begin{aligned} \text{y}&=0.85935*\textbf{tansig}(-0.38884*x_1+1.1133*x_2+2.4104) \\ &-1.4681*\textbf{tansig}(-0.90109*x_1+0.25378*x_2+0.059689) \\ &+1.9212*\textbf{tansig}(-0.57759*x_1-0.34253*x_2-2.3064) \\ &+1.1246 \end{aligned}
Substituting x1=0.5, x2=0.5 into it, you can get


Except for the very small difference in calculation accuracy, it is basically consistent with the prediction results in the toolbox


2.2 About the extraction of formulas


Due to the high degree of encapsulation of matlab, extracting mathematical expressions is not easy to explain in a few words,
so it is recommended to use the sim function that comes with matlab directly for prediction.
If you must extract mathematical expressions, you can refer to "BP Modeling Application" Contents of the chapter "Formula Extraction"


related articles


The Principle and Essence of BP Neural Network: Explanation of Old Cake | [Principle] Principle and Essence of BP Neural Network

Data Processing of BP: Explanation of Old Cake | [Introduction] About Data Processing in BP Neural Network Modeling

Simple DEMO of BP: Explanation of Old Cake | [Example] matlab-BP neural network simple DEMO

Guess you like

Origin blog.csdn.net/dbat2015/article/details/130234681