当前位置:网站首页>备战数学建模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的。

边栏推荐
- Which direction should college students choose to find jobs after graduation?
- mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT解决办法
- 波导的种类
- Create statement for Oracle export view
- 大学生研究生毕业找工作,该选择哪个方向?
- Policy Center > Malware > Malware
- 今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
- Data governance Market: Yixin Huachen faces left, Huaao data faces right
- 【算法篇】四种链表总结完毕,顺手刷了两道面试题
- Alibaba cloud OSS object storage cross domain settings
猜你喜欢

15年做糊21款硬件,谷歌到底栽在哪儿?

Simulation of two-color ball system to judge the winning situation

爬虫(1) - 爬虫基础入门理论篇

几百行代码实现一个 JSON 解析器

Implementation of Devops in the core field of qunar, the Internet R & D Efficiency

Generating verification code with sring

Which direction should college students choose to find jobs after graduation?

边缘计算平台如何助力物联网发展

新茶饮“死去活来”,供应商却“盆满钵满”?

Deep understanding Net (2) kernel mode 1 Kernel mode construct event event
随机推荐
The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
Arcmap操作系列:80平面转经纬度84
Mysql8.0 method and steps for enabling remote connection permission
There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
Google play index table
Using asp Net core creating web API series
Build cloud native observability capability suitable for organizations
MySQL8.0开启远程连接权限的方法步骤
Solution for IIS failing to load font files (*.woff, *.svg)
Reptile (1) - Introduction to basic reptile theory
边缘计算平台如何助力物联网发展
Policy Center > Device and Network Abuse
云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目
MySQL master-slave configuration
Two methods for MySQL to open remote connection permission
Modifying MySQL password under Linux: error 1396 (HY000): Operation alter user failed for 'root' @ 'localhost‘
[cve-2019-0193] - Apache Solr dataimport remote command execution analysis
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
如何得到股票开户的优惠活动?在线开户安全么?
'<', hexadecimal value 0x3C, is an invalid 问题解决
