当前位置:   article > 正文

数学建模智能优化算法之神经网络案例附Matlab代码_sim 是 matlab neural network toolbox 中的函数,用于执行神经网络的

sim 是 matlab neural network toolbox 中的函数,用于执行神经网络的仿真,即使用

读书使人充实,讨论使人机智,笔记使人准确…。凡有所学,皆成性格。
———— (英国)培根

使用MATLAB建立前馈神经网络主要会使用到下面3个函数:

newff:前馈神经网络创建函数;

train:训练一个神经网络;

sim:使用神经网络进行仿真。

下面简要介绍这3个函数的用法。

newff函数newff函数语法

语法:net=newff(P,T,[S1 S2…S(N-l)],{TF1 TF2…TFN},BTF,BLF,PF,IPF,OPF,DDF)

P:输入矩阵向量;

T:目标矩阵向量;

[S1 S2…S(N-l)]:神经网络前N-1层每层神经元数;

{TF1 TF2…TFN}:神经网络激活函数,默认为‘tansig’;

BTF:学习规则采用的训练算法,默认为‘trainlm’;

BLF:BP权值/偏差学习函数,默认为‘learngdm’;

PF:性能函数,默认为‘mse’;

IPF:输入处理函数;

OPF:输出处理函数;

DDF:验证数据划分函数。一般在使用过程中设置前7个参数,后3个参数采用系统默认参数即可

常用的激活函数

常用的激活函数有
线性函数 f ( x ) = x f(x)=x f(x)=x该函数的字符串为‘purelin’
对数S形转移函数 f ( x ) = 1 1 + e − x f(x)=\frac{1}{1+e^{-x}} f(x)=1+ex1 ( 0 < f ( x ) < 1 ) (0 < f(x) < 1) (0<f(x)<1)该函数的字符串为‘logsig’
双曲正切S形函数 f ( x ) = 2 1 + e − x − 1 f(x)=\frac{2}{1+e^{-x}}-1 f(x)=1+ex21​​​​​ ( − 1 < f ( x ) < 1 ) (-1 < f(x) < 1) (1<f(x)<1)​​​​​该函数的字符串为‘tansig’

只有当希望对网络的输出进行限制(如限制在0和1之间)时在输出层才应当包含S形激活函数。在一般情况下,均是在隐含层采用双极S形激活函数,而输出层采用线性激活函数。

常用的训练函数有:
trainbfgBFGS拟牛顿BP算法训练函数
trainbr贝叶斯正则化算法的BP算法训练函数
traingd梯度下降的BP算法训练函数
traingda梯度下降自适应lr的BP算法训练函数
traingdm梯度下降动量的BP算法训练函数
traingdx梯度下降动量和自适应lr的BP算法训练函数
trainlmLevenberg-Marquardt的BP算法训练函数
trainrp具有弹性的BP算法训练函数
trains顺序递增BP训练函数
trainscg量化连接梯度BP训练函数
常用的学习函数
learngdBP学习规则
learngdm带动量项的BP学习规则
常用的性能函数
mse均方误差函数
msereg均方误差规范化函数
配置参数一些重要的网络配置参数如下
net.trainparam.goal神经网络训练的目标误差
net.trainparam.show显示中间结果的周期
net.trainparam.epochs最大迭代次数
net.trainParam.lr学习率

train函数,train函数即神经网络训练学习函数。

语法:[net,tr,Y1,E]=train(net,X,Y)

X:网络输入矩阵;Y:网络输出矩阵;tr:训练跟踪信息;Y1:网络实际输出;E:误差矩阵。

sim函数,sim函数即神经网络仿真计算函数。

语法:Y=sim(net,X)

net:训练好的神经网络;X:网络输入矩阵;Y:网络输出矩阵。

MATLAB仿真实例

采用贝叶斯正则化算法提高BP网络的推广能力。用来训练BP网络,使其能够拟合某一附加有白噪声的正弦样本数据。

解:仿真过程如下:

(1)构建一个3层BP神经网络:输入层结点数为1个,隐含层结点数为3个,隐含层的激活函数为‘tansig’;输出层结点数为1个,输出层的激活函数为‘logsig’。

(2)采用贝叶斯正则化算法‘trainbr’训练BP网络,目标误差goal=1×10-3,学习率lr=0.05,最大迭代次数epochs=500,拟合附加有白噪声的正弦样本数据,拟合数据均方根误差为0.0054,拟合后的图形如图所示。

BP网络拟合附加白噪声的正弦曲线
在这里插入图片描述

MATLAB源程序如下:

%%%%%%%%%%%%%运用BP网络拟合白噪声的正弦样本数据%%%%%%%%%%%%%%%%
clear all;                      %清除所有变量
close all;                      %清图
clc;                            %清屏
%%%%%%%%%%%%%%%%%%%%%定义训练样本矢量%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%P 为输入矢量%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P = [-1:0.05:1];
%%%%%%%%%%%%%%%%%%%%%%T 为目标矢量%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T = sin(2*pi*P)+0.1*randn(size(P));
%%%%%%%%%%%%%%%%%%%%%%绘制样本数据点%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
plot(P,T,'+');
hold on;
plot(P,sin(2*pi*P),':');
%%%%%%%%%%%%%%%%%%%%%绘制不含噪声的正弦曲线%%%%%%%%%%%%%%%%%%%
net=newff(P,T,20,{'tansig','purelin'});
%%%%%%%%%%%%%%%%%%%采用贝叶斯正则化算法 TRAINBR%%%%%%%%%%%%%%%
net.trainFcn='trainbr';
%%%%%%%%%%%%%%%%%%%%%%设置训练参数%%%%%%%%%%%%%%%%%%%%%%%%%%%%
net.trainParam.show = 50;              %显示中间结果的周期
net.trainParam.lr = 0.05;              %学习率
net.trainParam.epochs = 500;           %最大迭代次数
net.trainParam.goal = 1e-3;            %目标误差
net.divideFcn = ''; %清除样本数据分为训练集、验证集和测试集命令
%%%%%%%%%%%%%%%%%%%%用相应算法训练 BP 网络%%%%%%%%%%%%%%%%%%%%
[net,tr]=train(net,P,T);
%%%%%%%%%%%%%%%%%%%%%对 BP 网络进行仿真%%%%%%%%%%%%%%%%%%%%%%%
A = sim(net,P);
%%%%%%%%%%%%%%%%%%%%%%计算仿真误差%%%%%%%%%%%%%%%%%%%%%%%%%%%%
E = T - A;
MSE=mse(E);
%%%%%%%%%%%%%%%%%%%%%%绘制匹配结果曲线%%%%%%%%%%%%%%%%%%%%%%%%
plot(P,A,P,T,'+',P,sin(2*pi*P),':');
legend('样本点','标准正弦曲线','拟合正弦曲线');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

表中所示为某药品的月度销售情况,利用BP神经网络对药品的销售进行预测,预测方法采用滚动预测方式,即用前3个月的销售量来预测第4个月的销售量。如用1、2、3月的销售量为输入,预测第4个月的销售量;用2、3、4月的销售量为输入,预测第5个月的销售量。反复迭代,直至满足预测精度要求为止。

月份123456
销量205623952600229816341600
月份789101112
销量187314781900150020461556

解:仿真过程如下:

(1)构建一个3层BP神经网络对药品的销售进行预测:输入层结点数为3个,隐含层结点数为5,隐含层的激活函数为‘tansig’;输出层结点数为1个,输出层的激活函数为‘logsig’。

(2)采用梯度下降动量和自适应lr算法‘traingdx’训练BP网络,目标误差goal= 1 × 1 0 − 3 1×10^{-3} 1×103,学习率lr=0.05,最大迭代次数epochs=1000,其销售实际值和预测值对比曲线如图所示。

在这里插入图片描述

药品销售实际值和预测值对比曲线

MATLAB源程序如下:

%%%%%%%%%%%%%%%%%%%%%%运用BP网络预测数据%%%%%%%%%%%%%%%%%%%%%%%%
clear all;                      %清除所有变量
close all;                      %清图
clc;                            %清屏
%%%%%%%%%%%%%%%%%%%%%%%%%%%%原始数据%%%%%%%%%%%%%%%%%%%%%%%%%%%%
p=[2056 2395 2600 2298 1634 1600 1873 1478 1900
   2395 2600 2298 1634 1600 1873 1478 1900 1500
   2600 2298 1634 1600 1873 1478 1900 1500 2046];
t=[2298 1634 1600 1873 1478 1900 1500 2046 1556];
%%%%%%%%%%%%%%%%%%%%%%%%%%原始数据归一化%%%%%%%%%%%%%%%%%%%%%%%%%
pmax=max(max(p));·
pmin=min(min(p));
P=(p-pmin)./(pmax-pmin);                 %输入数据矩阵
tmax=max(t);
tmin=min(t);
T=(t-tmin)./(tmax-tmin);                 %目标数据向量
%%%%%%%%%%%%%%%%%%%创建一个新的前向神经网络%%%%%%%%%%%%%%%%%%%%%%%
net=newff(P,T,5,{'tansig','purelin'},'traingdx');
%%%%%%%%%%%%%%%%%%%%%%%设置训练参数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
net.trainParam.show = 50;              %显示中间结果的周期
net.trainParam.lr = 0.01;              %学习率
net.trainParam.epochs = 1000;          %最大迭代次数
net.trainParam.goal = 1e-3;            %目标误差
net.divideFcn = ''; %清除样本数据分为训练集、验证集和测试集命令
%%%%%%%%%%%%%%%%%调用 TRAINGDM 算法训练 BP 网络%%%%%%%%%%%%%%%%%%
[net,tr]=train(net,P,T);
%%%%%%%%%%%%%%%%%%%%%对 BP 网络进行仿真%%%%%%%%%%%%%%%%%%%%%%%%%%
A = sim(net,P);
%%%%%%%%%%%%%%%%%%%%%%计算预测数据原始值%%%%%%%%%%%%%%%%%%%%%%%%%
a = A.*(tmax-tmin)+tmin;               
%%%%%%%%%%%%%%%%%%%%%绘制实际值和预测值曲线%%%%%%%%%%%%%%%%%%%%%%
x=4:12;
figure
plot(x,t,'+');
hold on;
plot(x,a,'or');
hold off
xlabel('月份')
ylabel('销量')
legend('实际销量 ','预测销量');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

表中所示为某地区公路运力的历史统计数据表,请建立相应的BP神经网络预测模型,并根据给出的2010年和2011年的数据,预测相应的公路客运量和货运量。
在这里插入图片描述

解:仿真过程如下:

(1)构建一个3层BP神经网络对该地区公路运力进行预测:输入层结点数为3个,隐含层结点数为8,隐含层的激活函数为‘tansig’;输出层结点数为2个,输出层的激活函数为‘purelin’。

(2)采用梯度下降动量和自适应lr算法‘traingdx’训练BP网络,目标误差goal=1×10-3,学习率lr=0.035,最大迭代次数epochs=2000。拟合的历年公路客运量曲线和历年公路货运量曲线分别如图9.6和图9.7所示。预测结果为:2010年公路客运量为4.5277亿人,公路货运量为2.2290亿吨;2011年公路客运量为4.5308亿人,公路货运量为2.2296亿吨。

历年公路客运量拟合曲线

在这里插入图片描述

历年公路货运量拟合曲线

在这里插入图片描述

MATLAB源程序如下:

%%%%%%%%%%%%%%%%%%%%%%运用BP网络预测数据%%%%%%%%%%%%%%%%%%%%%%%%
clear all;                      %清除所有变量
close all;                      %清图
clc;                            %清屏
%%%%%%%%%%%%%%%%%%%%%%%%%%%%原始数据%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%人数(单位:万人)%%%%%%%%%%%%%%%%%%%%%%%%%
sqrs=[20.55 22.44 25.37 27.13 29.45 30.10 30.96 34.06 36.42 38.09...
    39.13 39.99 41.93 44.59 47.30 52.89 55.73 56.76 59.17 60.63];
%%%%%%%%%%%%%%%%%%%%%%机动车数(单位:万辆)%%%%%%%%%%%%%%%%%%%%%%%%
sqjdcs=[0.6 0.75 0.85 0.9 1.05 1.35 1.45 1.6 1.7 1.85 2.15 2.2...
    2.25 2.35 2.5 2.6 2.7 2.85 2.95 3.1];
%%%%%%%%%%%%%%%%%%%%公路面积(单位:万平方公里)%%%%%%%%%%%%%%%%%%%%
sqglmj=[0.09 0.11 0.11 0.14 0.20 0.23 0.23 0.32 0.32 0.34 0.36...
    0.36 0.38 0.49 0.56 0.59 0.59 0.67 0.69 0.79];
%%%%%%%%%%%%%%%%%%%%%公路客运量(单位:万人)%%%%%%%%%%%%%%%%%%%%%%%
glkyl=[5126 6217 7730 9145 10460 11387 12353 15750 18304 19836 ... 
    21024 19490 20433 22598 25107 33442 36836 40548 42927 43462];
%%%%%%%%%%%%%%%%%%%%%公路货运量(单位:万吨)%%%%%%%%%%%%%%%%%%%%%%%
glhyl=[1237 1379 1385 1399 1663 1714 1834 4322 8132 8936 11099 ...
    11203 10524 11115 13320 16762 18673 20724 20803 21804];
%%%%%%%%%%%%%%%%%%%%%%%%%%输入数据矩阵%%%%%%%%%%%%%%%%%%%%%%%%%%%
p=[sqrs;sqjdcs;sqglmj]; 
%%%%%%%%%%%%%%%%%%%%%%%%%%目标数据矩阵%%%%%%%%%%%%%%%%%%%%%%%%%%%
t=[glkyl;glhyl];    
%%%%%%%%%%%%%%%%%%%%%%%%%原始样本归一化%%%%%%%%%%%%%%%%%%%%%%%%%%
[P,PSp] = mapminmax(p);
[T,PSt] = mapminmax(t);
%%%%%%%%%%%%%%%%%%%创建一个新的前向神经网络%%%%%%%%%%%%%%%%%%%%%%%
net=newff(P,T,8,{'tansig','purelin'},'traingdx');
%%%%%%%%%%%%%%%%%%%%%%%设置训练参数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
net.trainParam.show = 50;              %显示中间结果的周期
net.trainParam.lr = 0.035;             %学习率
net.trainParam.epochs = 1000;          %最大迭代次数
net.trainParam.goal = 1e-3;            %目标误差
net.divideFcn = ''; %清除样本数据分为训练集、验证集和测试集命令
%%%%%%%%%%%%%%%%%调用 TRAINGDM 算法训练 BP 网络%%%%%%%%%%%%%%%%%%
[net,tr]=train(net,P,T);
%%%%%%%%%%%%%%%%%%%%%对 BP 网络进行仿真%%%%%%%%%%%%%%%%%%%%%%%%%%
A = sim(net,P);
a=mapminmax('reverse',A,PSt);
%%%%%%%%%%%%%%%%%%%%优化后输入层权值和阈值%%%%%%%%%%%%%%%%%%%%%%%
inputWeights=net.IW{1,1};
inputbias=net.b{1};
%%%%%%%%%%%%%%%%%%%%优化后网络层权值和阈值%%%%%%%%%%%%%%%%%%%%%%%
layerWeights=net.LW{2,1};
layerbias=net.b{2};
%%%%%%%%%%%%%%%%%%%%%%%%%时间轴刻度%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=1990:2009;   
%%%%%%%%%%%%%%%%%%%%%%%网络输出客运量%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
newk=a(1,:);       
%%%%%%%%%%%%%%%%%%%%%%%网络输出货运量%%%%%%%%%%%%%%%%%%%%%%%%%%%%
newh=a(2,:);                                      
%%%%%%%%%%%%%%%%%%%%绘值公路客运量对比图%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
plot(x,newk,'r-o',x,glkyl,'b--+')  
legend('网络输出客运量','实际客运量');
xlabel('年份');ylabel('客运量/万人');
%%%%%%%%%%%%%%%%%%%%%%绘制公路货运量对比图%%%%%%%%%%%%%%%%%%%%%%%
figure
plot(x,newh,'r-o',x,glhyl,'b--+')     
legend('网络输出货运量','实际货运量');
xlabel('年份');ylabel('货运量/万吨');
%%%%%%%%%%%%%%%%%%%%%利用训练好的网络进行预测%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%2010年和2011年的相关数据%%%%%%%%%%%%%%%%%%%%%
pnew=[73.39 75.55;3.9 4.1;0.98 1.02];                     
SamNum=size(pnew,2);  
%%%%%%%%%%%利用原始输入数据的归一化参数对新数据进行归一化%%%%%%%%%%% 
pnewn=mapminmax('apply',pnew,PSp);
%%%%%%%%%%%%%%%%%%%%%%%隐含层输出预测结果%%%%%%%%%%%%%%%%%%%%%%%%%
HiddenOut=tansig(inputWeights*pnewn+repmat(inputbias,1,SamNum)); 
%%%%%%%%%%%%%%%%%%%%%%%输出层输出预测结果%%%%%%%%%%%%%%%%%%%%%%%%%
anewn=purelin(layerWeights*HiddenOut+repmat(layerbias,1,SamNum));  
%%%%%%%%%%%%%%把网络预测得到的数据还原为原始的数量级%%%%%%%%%%%%%%%
anew=mapminmax('reverse',anewn,PSt);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/105732
推荐阅读
相关标签
  

闽ICP备14008679号