目录
【附件下载】 含数据集与Matlab源码等文件下载(百度云)
本文按照 案例-分析-代码-原理 四段式进行介绍,为便于有一定理论基础的童鞋进行学习,将原理以链接其他博客的形式给出,有兴趣的童鞋可以跳转进行学习和推敲。
【案例介绍】利用BP神经网络建立近红外光谱-辛烷值模型
辛烷值是汽油最重要的品质指标,传统的实验室检测方法存在样品用量大,测试周期长和费用高等问题,不适用于生产控制,特别是在线测试。近年发展起来的近红外光谱分析方法(NIR),作为一种快速分析方法,已广泛应用于农业、制药、生物化工、石油产品等领域。其优越性是无损检测、低成本、无污染,能在线分析,更适合于生产和控制的需要。实验采集得到60组汽油样品,利用傅里叶近红外变换光谱仪对其进行扫描,扫描范围900~1700nm,扫描间隔2nm,每个样品的光谱曲线共含401个波长点。样品的近红外光谱曲线如下图所示:
同时,利用传统实验室检测方法测定其辛烷值含量。现要求利用BP神经网络建立汽油样品近红外光谱及其辛烷值之间的数学模型,并对模型的性能进行评价。
其中,评价分为两部分,分别为相对误差E和决定系数
,分别定义如下:
其中,
为第
个样品的预测值;
为第
个样品的真实值;
为样品的数目。
注:相对误差越小,表明模型的性能越好;决定系数范围在[0,1]内,愈接近于1,表明模型的性能愈好;反之,愈趋近于0,表明模型的性能愈差。
【代码分析】Matlab BP神经网络建立及测试代码
%%
% 数据由spectra_data.mat录入,分为NIR(60*401)[double]与octane(60*1)[double]
% 上述数据项分别为输入数据和标签数据,且被分为50个训练数据集和10个测试数据集
% 神经网络采用9个隐藏层设计,结果对比附在代码后部分%% I. 清空环境变量
clear all
clc%% II. 训练集/测试集产生
%%
% 1. 导入数据
load spectra_data.mat%%
% 2. 随机产生训练集和测试集
temp = randperm(size(NIR,1));
% 训练集——50个样本
P_train = NIR(temp(1:50),:)';
T_train = octane(temp(1:50),:)';
% 测试集——10个样本
P_test = NIR(temp(51:end),:)';
T_test = octane(temp(51:end),:)';
N = size(P_test,2);%% III. 数据归一化
[p_train, ps_input] = mapminmax(P_train,0,1);
p_test = mapminmax('apply',P_test,ps_input);[t_train, ps_output] = mapminmax(T_train,0,1);
%% IV. BP神经网络创建、训练及仿真测试
%%
% 1. 创建网络
net = newff(p_train,t_train,9);%%
% 2. 设置训练参数
net.trainParam.epochs = 1000;
net.trainParam.goal = 1e-3;
net.trainParam.lr = 0.01;%%
% 3. 训练网络
net = train(net,p_train,t_train);%%
% 4. 仿真测试
t_sim = sim(net,p_test);%%
% 5. 数据反归一化
T_sim = mapminmax('reverse',t_sim,ps_output);%% V. 性能评价
%%
% 1. 相对误差error
error = abs(T_sim - T_test)./T_test;%%
% 2. 决定系数R^2
R2 = (N * sum(T_sim .* T_test) - sum(T_sim) * sum(T_test))^2 / ((N * sum((T_sim).^2) - (sum(T_sim))^2) * (N * sum((T_test).^2) - (sum(T_test))^2));%%
% 3. 结果对比
result = [T_test' T_sim' error']%% VI. 绘图
figure
plot(1:N,T_test,'b:*',1:N,T_sim,'r-o')
legend('真实值','预测值')
xlabel('预测样本')
ylabel('辛烷值')
string = {'测试集辛烷值含量预测结果对比';['R^2=' num2str(R2)]};
title(string)
测试集预测结果对比如下:
训练过程误差:
训练参数变化:
回归分析:
总结:
- 反向传播采用Levenberg-Marquardt,即莱文贝格-马夸特方法。
- 代价函数采用Mean Squared Error,即均方误差。
- 关于训练采用的激活函数及反向传递算法等可在Matlab中的newff函数中进行设置,下图贴出newff函数的描述:
% NEWFF(P,T,S) takes,
% P - RxQ1 matrix of Q1 representative R-element input vectors.
% T - SNxQ2 matrix of Q2 representative SN-element target vectors.
% Si - Sizes of N-1 hidden layers, S1 to S(N-1), default = [].
% (Output layer size SN is determined from T.)
% and returns an N layer feed-forward backprop network.
%
% NEWFF(P,T,S,TF,BTF,BLF,PF,IPF,OPF,DDF) takes optional inputs,
% TFi - Transfer function of ith layer. Default is 'tansig' for
% hidden layers, and 'purelin' for output layer.
% BTF - Backprop network training function, default = 'trainlm'.
% BLF - Backprop weight/bias learning function, default = 'learngdm'.
% PF - Performance function, default = 'mse'.
% IPF - Row cell array of input processing functions.
% Default is {'fixunknowns','remconstantrows','mapminmax'}.
% OPF - Row cell array of output processing functions.
% Default is {'remconstantrows','mapminmax'}.
% DDF - Data division function, default = 'dividerand';
% and returns an N layer feed-forward backprop network.
%
% The transfer functions TF{i} can be any differentiable transfer
% function such as TANSIG, LOGSIG, or PURELIN.
%
% The training function BTF can be any of the backprop training
% functions such as TRAINLM, TRAINBFG, TRAINRP, TRAINGD, etc.
%
% *WARNING*: TRAINLM is the default training function because it
% is very fast, but it requires a lot of memory to run. If you get
% an "out-of-memory" error when training try doing one of these:
%
% (1) Slow TRAINLM training, but reduce memory requirements, by
% setting NET.<a href="matlab:doc nnproperty.net_efficiency">efficiency</a>.<a href="matlab:doc nnproperty.net_efficiency_memoryReduction">memoryReduction</a> to 2 or more. (See HELP TRAINLM.)
% (2) Use TRAINBFG, which is slower but more memory efficient than TRAINLM.
% (3) Use TRAINRP which is slower but more memory efficient than TRAINBFG.
%
% The learning function BLF can be either of the backpropagation
% learning functions such as LEARNGD, or LEARNGDM.
%
% The performance function can be any of the differentiable performance
% functions such as MSE or MSEREG.
【附件下载】 含数据集与Matlab源码等文件下载(百度云)
1、通过百度云进行下载:
链接:https://pan.baidu.com/s/1YhWXYjDcspmukSQFFo-PEQ
提取码:sb5h
2、使用微信扫描进行文件下载:
【原理链接】 BP神经网络原理介绍