如何用MATLAB的神经网络工具箱实现三层BP网络
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
如何用MA TLAB的神经网络工具箱实现三层BP网络?
% 读入训练数据和测试数据
Input = [];
Output = [];
str = {'Test','Check'};
Data = textread([str{1},'.txt']);
% 读训练数据
Input = Data(:,1:end-1);
% 取数据表的前五列(主从成分)
Output = Data(:,end);
% 取数据表的最后一列(输出值)
Data = textread([str{2},'.txt']);
% 读测试数据
CheckIn = Data(:,1:end-1);
% 取数据表的前五列(主从成分)
CheckOut = Data(:,end);
% 取数据表的最后一列(输出值)
Input = Input';
Output = Output';
CheckIn = CheckIn';
CheckOut = CheckOut';
% 矩阵赚置
[Input,minp,maxp,Output,mint,maxt] = premnmx(Input,Output);
% 标准化数据
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%
% 神经网络参数设置
%====可以修正处
Para.Goal = 0.0001;
% 网络训练目标误差
Para.Epochs = 800;
% 网络训练代数
Para.LearnRate = 0.1;
% 网络学习速率
%====
Para.Show = 5;
% 网络训练显示间隔
Para.InRange = repmat([-1 1],size(Input,1),1);
% 网络的输入变量区间
Para.Neurons = [size(Input,1)*2+1 1];
% 网络后两层神经元配置
Para.TransferFcn= {'logsig' 'purelin'};
% 各层的阈值函数
Para.TrainFcn = 'trainlm';
% 网络训练函数赋值
% traingd : 梯度下降后向传播法
% traingda : 自适应学习速率的梯度下降法
% traingdm : 带动量的梯度下降法
% traingdx :
% 带动量,自适应学习速率的梯度下降法
Para.LearnFcn = 'learngdm';
% 网络学习函数
Para.PerformFcn = 'sse';
% 网络的误差函数
Para.InNum = size(Input,1);
% 输入量维数
Para.IWNum = Para.InNum*Para.Neurons(1);
% 输入权重个数
Para.LWNum = prod(Para.Neurons);
% 层权重个数
Para.BiasNum = sum(Para.Neurons);
% 偏置个数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%
Net = newff(Para.InRange,Para.Neurons,Para.TransferFcn,...
Para.TrainFcn,Para.LearnFcn,Para.PerformFcn);
% 建立网络%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%
Net.trainParam.show = Para.Show;
% 训练显示间隔赋值
Net.trainParam.goal = Para.Goal;
% 训练目标误差赋值
Net.trainParam.lr = Para.LearnRate;
% 网络学习速率赋值
Net.trainParam.epochs = Para.Epochs;
% 训练代数赋值
Net.trainParam.lr = Para.LearnRate;
Net.performFcn = Para.PerformFcn;
% 误差函数赋值
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 调试
Out1 =sim(Net,Input);
% 仿真刚建立的网络
Sse1 =sse(Output-Out1);
% 刚建立的网络误差
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [Net TR] = train(Net,Input,Output);
% 训练网络并返回%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Out3 =sim(Net,Input);
% 对学习训练后的网络仿真