GRNN neural network principle and matlab implementation

1 Case background

1.1 Overview of GRNN neural network

        Generalized Regression Neural Network (GRNN Generalized Regression Neural Network) was proposed by American scholar Don-ald F. Specht in 1991. It is a kind of radial basis neural network. GRNN has strong nonlinear mapping ability, flexible network structure, high fault tolerance and robustness, and is suitable for solving nonlinear problems. GRNN has stronger advantages than RBF network in terms of approximation ability and learning speed. The network finally converges to the optimized regression surface with more sample size accumulation, and the prediction effect is better when the sample data is less. In addition, the network can also handle unstable data. Therefore, GRNN has been widely used in signal process, structural analysis, education industry, energy, food science, control decision system, drug design, financial field, bioengineering and other fields.

1.2 The network structure of GRNN

        (1) Input layer
        The number of neurons in the input layer and the dimensionality of the input vector in the learning sample, each neuron is a simple distribution unit, which directly transmits the input variable to the pattern layer.
        (2) Pattern layer
        The number of neurons in the pattern layer is equal to the number of learning samples , each neuron corresponds to a different sample, and the transfer function of the neurons in the pattern layer is
        (3) Summing layer
        Two types of neurons are used in the summation layer for summation.

        (4) Output layer

1.3 Theoretical Basis of GRNN

        The theoretical basis of generalized regression neural network is nonlinear regression analysis, and the regression analysis of dependent variable Y relative to independent variable I is actually to calculate y with the largest probability value. Let the joint probability density function of random variable x and random variable y be f(z, y), and the observed value of worker is known as X, then the regression of y relative to X, that is, the conditional mean is

1.4 Related Background of Freight Volume Forecast in Transportation System

        As a subsystem of the socio-economic system, the transportation system is influenced and affected by external factors, but also has a certain reaction to the external economic system, so that the transportation demand is affected by both internal and external factors of the transportation system. As the basis for investment decision-making in transportation infrastructure construction, transportation demand forecasting plays a very important role in national and regional economic development planning. An important question in the study of developmental relationships. Therefore, as an important index reflecting the demand of cargo transportation, the research and analysis of freight volume forecasting has strong practical and theoretical significance.
        Commonly used freight volume forecasting methods include time series methods, moving smoothing methods, exponential smoothing methods, random time series methods, correlation, regression analysis methods, gray forecasting methods, and combined forecasting methods that combine multiple methods. Most of these methods focus on the analysis of their causal regression models and time series models, and the established models cannot fully, scientifically and essentially reflect the internal structure and complex characteristics of the predicted dynamic data, and the amount of information is lost. As a parallel computing model, the artificial neural network has many advantages that traditional modeling methods do not have: it has good nonlinear mapping ability, requires little prior knowledge of the modeled object, and generally does not need to know the relevant information in advance. For the knowledge of the structure, parameters, and dynamic characteristics of the modeled object, it is only necessary to give the input and output data of the object, and through the learning function of the network itself, the complete agreement between input and output can be achieved.
        In this case, some domestic scholars have introduced neural network into freight volume forecasting. However, when BP neural network is used for function approximation, there are disadvantages such as slow convergence speed and local minimum, and the effect is not ideal when solving the problem of small sample size and large noise. GRNN has strong advantages in approximation ability, classification ability and learning speed. The network finally converges to the optimal regression surface with the largest sample size accumulation, and the effect is also better when the data is scarce. The network can handle unstable data, so this case uses GRNN to establish a freight volume forecasting model, and uses historical statistical data to predict freight volume.

2 model building

        According to the analysis of the influencing factors of freight volume, the gross domestic product (GDP), the total industrial output value, the length of railway transportation lines, the proportion of double-track mileage, the length of road transportation lines, the proportion of graded roads, the number of railway freight cars and the number of civilian trucks are taken respectively8 GRNN is built with three index factors as the network input, total freight volume, railway freight volume and road freight volume as the network output. Due to the lack of training data, the GRNN neural network is trained by cross-validation method, and the most optimal model is found by looping. Good spread.
        In this case, there are two sets of data p and t in data.mat, and each contains 13 sets of data, representing the freight volume from 1996 to 2008 and the values ​​of various variables related to it. The first 12 sets of data of p and t are used as the training data of the network, and the last set of data is used as the prediction data of the network, and the GRNN neural network is established to predict the freight volume.

3 MATLAB implementation

        The related function of GRNN network, its function name is newgrnn(). This function can be used to design a generalized regression neural network. The generalized regression neural network is a kind of RBF network, which is usually used for function approximation, and its calling format is

net = newgrnn(P.T,SPREAD)

        Among them, P is the R*Q dimensional matrix composed of Q groups of input vectors; T is the S*Q dimensional matrix composed of Q groups of target classification vectors; SPREAD is the expansion speed of the radial basis function, and the default value is 1.
        According to the network input and output determined above, use the historical data of a certain place from 1996 to 2007 as the training samples of the network, and the data in 2008 as the extrapolation test samples of the network. code show as below:

%% Matlab神经网络43个案例分析


 
%% 清空环境变量
clc;
clear all
close all
nntwarn off;

%% 载入数据
load data;
% 载入数据并将数据分成训练和预测两类
p_train=p(1:12,:);
t_train=t(1:12,:);
p_test=p(13,:);
t_test=t(13,:);
%% 交叉验证
desired_spread=[];
mse_max=10e20;
desired_input=[];
desired_output=[];
result_perfp=[];
indices = crossvalind('Kfold',length(p_train),4);
h=waitbar(0,'正在寻找最优化参数....');
k=1;
for i = 1:4
    perfp=[];
    disp(['以下为第',num2str(i),'次交叉验证结果'])
    test = (indices == i); train = ~test;
    p_cv_train=p_train(train,:);
    t_cv_train=t_train(train,:);
    p_cv_test=p_train(test,:);
    t_cv_test=t_train(test,:);
    p_cv_train=p_cv_train';
    t_cv_train=t_cv_train';
    p_cv_test= p_cv_test';
    t_cv_test= t_cv_test';
    [p_cv_train,minp,maxp,t_cv_train,mint,maxt]=premnmx(p_cv_train,t_cv_train);
    p_cv_test=tramnmx(p_cv_test,minp,maxp);
    for spread=0.1:0.1:2;
        net=newgrnn(p_cv_train,t_cv_train,spread);
        waitbar(k/80,h);
        disp(['当前spread值为', num2str(spread)]);
        test_Out=sim(net,p_cv_test);
        test_Out=postmnmx(test_Out,mint,maxt);
        error=t_cv_test-test_Out;
        disp(['当前网络的mse为',num2str(mse(error))])
        perfp=[perfp mse(error)];
        if mse(error)<mse_max
            mse_max=mse(error);
            desired_spread=spread;
            desired_input=p_cv_train;
            desired_output=t_cv_train;
        end
        k=k+1;
    end
    result_perfp(i,:)=perfp;
end;
close(h)
disp(['最佳spread值为',num2str(desired_spread)])
disp(['此时最佳输入值为'])
desired_input
disp(['此时最佳输出值为'])
desired_output
%% 采用最佳方法建立GRNN网络
net=newgrnn(desired_input,desired_output,desired_spread);
p_test=p_test';
p_test=tramnmx(p_test,minp,maxp);
grnn_prediction_result=sim(net,p_test);
grnn_prediction_result=postmnmx(grnn_prediction_result,mint,maxt);
grnn_error=t_test-grnn_prediction_result';
disp(['GRNN神经网络三项流量预测的误差为',num2str(abs(grnn_error))])
save best desired_input desired_output p_test t_test grnn_error mint maxt

 The result of the operation is as follows:

最佳spread值为0.5
此时最佳输入值为

desired_input =

   -1.0000   -0.8993   -0.7948   -0.5023   -0.2955   -0.0574    0.1602    0.6652    1.0000
   -0.9998   -1.0000   -0.1291   -0.0072    0.2070    0.3417    0.5137    0.7838    1.0000
   -1.0000   -0.8616   -0.4969   -0.4969    0.1950    0.3333    0.4465    0.6604    1.0000
   -1.0000   -0.5385   -0.0769    0.5385    0.2308    0.3846    0.3846    0.6923    1.0000
   -1.0000   -0.9429   -0.9175   -0.7778   -0.5937   -0.3270   -0.0286    0.5619    1.0000
   -1.0000   -1.0000   -1.0000   -0.5000   -0.3000   -0.2000    0.0000    0.5000    1.0000
    0.0141   -1.0000    0.0187    0.0187    0.2477    0.3682    0.4944    0.7735    1.0000
   -1.0000   -0.9211   -0.8826   -0.9563   -0.7786   -0.6099   -0.3042    0.2843    1.0000

此时最佳输出值为

desired_output =

   -1.0000   -0.9839   -0.9838   -0.7127   -0.4503   -0.2463    0.0126    0.5394    1.0000
   -1.0000   -0.9040   -0.8604   -0.6403   -0.3950   -0.2293   -0.0769    0.4116    1.0000
   -1.0000   -0.8020   -0.8042   -0.5446   -0.2471   -0.0500    0.0416    0.4693    1.0000

GRNN神经网络三项流量预测的误差为9959.185       1777.8231       14498.027

        It can be seen from the results after the program runs that when the SPREAD value is set to 0.5, the prediction of the training data is better. The smaller the value of SPREAD, the stronger the approximation of the network to the sample; the larger the value of SPREAD, the smoother the approximation process of the network to the sample data, but the error also increases accordingly. In practical application, in order to select the best SPREAD value, the loop training method in this case is generally adopted, so as to achieve the best prediction effect.

4 case extension

        Both GRNN neural network and BP network can be used to predict freight volume, etc., but for specific network training, GRNN needs to adjust fewer parameters, only one SPREAD parameter, so it can predict the network faster and has a larger Computational advantage. The following will use the BP neural network model to predict the traffic data for this case data. code show as below:

%% 以下程序为案例扩展里的GRNN和BP比较 需要load chapter8.1的相关数据
clear all
load best
n=13;
p=desired_input;
t=desired_output;
net_bp=newff(minmax(p),[n,3],{'tansig','purelin'},'trainlm');
% 训练网络
net.trainParam.show=50;
net.trainParam.epochs=2000;
net.trainParam.goal=1e-3;
%调用TRAINLM算法训练BP网络
net_bp=train(net_bp,p,t);
bp_prediction_result=sim(net_bp,p_test);
bp_prediction_result=postmnmx(bp_prediction_result,mint,maxt);
bp_error=t_test-bp_prediction_result';
disp(['BP神经网络三项流量预测的误差为',num2str(abs(bp_error))])

operation result:

BP神经网络三项流量预测的误差为6967.20678      11142.5527      4156.17376

Get the complete code:

https://download.csdn.net/download/weixin_44209907/88142535

Guess you like

Origin blog.csdn.net/weixin_44209907/article/details/132047725