当前位置:网站首页>备战数学建模34-BP神经网络预测2
备战数学建模34-BP神经网络预测2
2022-06-30 15:44:00 【nuist__NJUPT】
目录
BP神经网络模型,包含输入层,隐含层和输出层,正向传播过程是通过输入样本到输入层,通过输入层经过各层隐藏层,最后到达输出层;若输出层输出值与期望值的输出不符,然后误差反向传播,修正各个单元的权值,网络输出的误差达到可接受的程度或进行到预设的学习次数时终止。本次就不介绍理论部分了,直接看例题,需要学习理论部分,可以看我的上一篇神经网络的博客。
一、辛烷值的预测
1-题目分析与原理介绍
我们看一下这个例题1, 对于50组数据,我们把它看成大样本,可以考虑建立神经网络模型进行预测,因为辛烷值作为预测对象,我们将吸光度作为输入,辛烷值作为输出。
使用神经网络进行预测,我们需要知道三个名词:训练集,验证集,测试集,具体如下所示,我们此次选70%作为训练集,剩余30%分别作为验证集和测试集。
关于刚开始使用Matlab写神经网络出现Input data size does not match net.inputs{1}.size问题,是因为训练数据的输入和输出维度不一致,要保证输入的列数等于输出的列数。
2-神经网络建立过程
将吸光度作为输入,辛烷值作为输出,建立神经网络,其中隐藏层神经元的个数为7个,输出层神经元的个数为1个,具体如下:
50组数据,前面40组作为训练集,后面10组作为测试集,训练集训练模型,测试集进行泛化能力评估,我们计算均方根误差和均值误差。最后对10组数据进行预测。
对网络进行训练,然后进行预测,MATLAB代码和未来10年的预测结果如下所示:
clear; clc
load data_Octane.mat
%%第一步 读取数据
input=X; %载入输入数据
output=Y; %载入输出数据
%% 第二步 设置训练数据和预测数据
input_train = input(1:40,:)';
output_train =output(1:40,:)';
input_test = input(41:50,:)';
output_test =output(41:50,:)';
input_10 = new_X(1:10,:)' ;
output_10 = zeros(10,1)' ;
%节点个数
inputnum=2; % 输入层节点数量
hiddennum=7;% 隐含层节点数量
outputnum=1; % 输出层节点数量
%% 第三本 训练样本数据归一化
[inputn,inputps]=mapminmax(input_train);%归一化到[-1,1]之间,inputps用来作下一次同样的归一化
[outputn,outputps]=mapminmax(output_train);
%% 第四步 构建BP神经网络
net=newff(inputn,outputn,hiddennum,{'tansig','purelin'},'trainlm');% 建立模型,传递函数使用purelin,采用梯度下降法训练
W1= net. iw{1, 1};%输入层到中间层的权值
B1 = net.b{1};%中间各层神经元阈值
W2 = net.lw{2,1};%中间层到输出层的权值
B2 = net. b{2};%输出层各神经元阈值
%% 第五步 网络参数配置( 训练次数,学习速率,训练目标最小误差等)
net.trainParam.epochs=1000; % 训练次数,这里设置为1000次
net.trainParam.lr=0.01; % 学习速率,这里设置为0.01
net.trainParam.goal=0.00001; % 训练目标最小误差,这里设置为0.00001
%% 第六步 BP神经网络训练
net=train(net,inputn,outputn);%开始训练,其中inputn,outputn分别为输入输出样本
%% 第七步 测试样本归一化
inputn_test=mapminmax('apply',input_test,inputps);% 对样本数据进行归一化
inputn_10 = mapminmax('apply',input_10,inputps);
%% 第八步 BP神经网络预测
an=sim(net,inputn_test); %用训练好的模型进行仿真
an1 = sim(net,inputn_10) ;%最后10组预测
%% 第九步 预测结果反归一化与误差计算
test_simu=mapminmax('reverse',an,outputps); %把仿真得到的数据还原为原始的数量级
output_10 = mapminmax('reverse',an1,outputps);
error=test_simu-output_test; %预测值和真实值的误差
%%第十步 真实值与预测值误差比较
figure(1);
plot(output_test,'bo-')
hold on
plot(test_simu,'r*-')
hold on
plot(error,'square','MarkerFaceColor','b')
legend('期望值','预测值','误差')
xlabel('数据组数')
ylabel('样本值')
title('BP神经网络测试集的预测值与实际值对比图')
[c,l]=size(output_test);
MAE1=sum(abs(error))/l;
MSE1=error*error'/l;
RMSE1=MSE1^(1/2);
disp(['-----------------------误差计算--------------------------']);
disp(['隐含层节点数为',num2str(hiddennum),'时的误差结果如下:']);
disp(['平均绝对误差MAE为:',num2str(MAE1)]);
disp(['均方误差MSE为: ',num2str(MSE1)]);
disp(['均方根误差RMSE为: ',num2str(RMSE1)]);
disp('最后10组辛烷的预测结果为:') ;
disp(output_10) ;
figure(2);
plot(51:60,output_10,'o-') ;
xlabel('样本ID');
ylabel('辛烷预测值') ;
3-预测结果分析
我们看一下测试集预测值和真实值的对比分析,可以发现误差很小,几乎接近于0,可以认为模型训练的较好。
然后我们看一下,最后10组辛烷值的预测结果,具体如下:
我们看一下预测均方误差和均值误差,具体如下:
我们看一下训练集、验证集、测试集和总体的均方误差随训练次数的变化图像,基本上验证集和测试集5次就收敛了,训练集要6次以上。
将拟合值对真实值回归,拟合优度越高,说明拟合的的效果越好,我们可以看出来集合的拟合优度都是接近于1的。
边栏推荐
- '<', hexadecimal value 0x3C, is an invalid 问题解决
- Data governance Market: Yixin Huachen faces left, Huaao data faces right
- Using asp Net core creating web API series
- Asp. NETCORE uses cache and AOP to prevent repeated commit
- MicroBlaze serial port learning · 2
- MySQL master-slave configuration
- How to connect the Internet Reading Notes - Summary
- How the edge computing platform helps the development of the Internet of things
- '<', Hexadecimal value 0x3c, is an invalid problem solving
- Implementation of Devops in the core field of qunar, the Internet R & D Efficiency
猜你喜欢
The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning
ASP. Net core Middleware
Generating verification code with sring
Phone number shielding function
'<', hexadecimal value 0x3C, is an invalid 问题解决
Policy Center-Permissions and APIs that Access Sensitive Information
Cesium-1.72 learning (earth model creation online offline tile)
CloudXR如何推动XR的未来发展
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
What is the difference between real-time rendering and pre rendering
随机推荐
MySQL8.0开启远程连接权限的方法步骤
[download attached] installation and use of penetration test artifact Nessus
Policy Center > Misrepresentation
Three development trends of enterprise application viewed from the third technological revolution
Compulsory national standard for electronic cigarette GB 41700-2022 issued and implemented on October 1, 2022
The difference between intermodulation and intermodulation
Reptile (1) - Introduction to basic reptile theory
思源笔记:能否提供页面内折叠所有标题的快捷键?
15年做糊21款硬件,谷歌到底栽在哪儿?
IIS无法加载字体文件(*.woff,*.svg)的解决办法
KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system
构建适合组织的云原生可观测性能力
Mysql代理中间件Atlas安装和配置
Cloud XR, how to help industrial upgrading
What are the reasons for the errors reported by the Flink SQL CDC synchronization sqlserver
CVPR 2022丨特斯联AI提出:基于图采样深度度量学习的可泛化行人重识别
《网络是怎么样连接的》读书笔记 - 汇总篇
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
智慧风电:数字孪生 3D 风机智能设备运维
招标公告:天津市住房公积金管理中心数据库一体机及数据库软件项目(预算645万)