赞
踩
LSTM在1997年被提出,从发表时间上来看已经是个"老"方法了。和其他的神经网络一样,LSTM可用于分类、回归以及时间序列预测等。原理部分的介绍可参考这篇博客。本文主要涉及利用matlab实现LSTM。
任务:以青霉素发酵过程仿真数据为例,利用LSTM建模预测质量变量。
青霉素发酵过程仿真过程简介:共有18个过程变量,其中15个可测变量,剩余3个一般作为质量变量。共生成30个批次数据,每批次运行时长为400小时,采样时间为1小时,其中25批次用于训练,5批次用于测试。
本文所用数据下载,基于matlab深度学习工具箱实现青霉素浓度的预测。
XTrain_mu = mean([XTrain{:}],2);
XTrain_sig = std([XTrain{:}],0,2);
XTest_mu = mean([XTest{:}],2);
XTest_sig = std([XTest{:}],0,2);
YTrain_mu = mean([YTrain{:}],2);
YTrain_sig = std([YTrain{:}],0,2);
YTest_mu = mean([YTest{:}],2);
YTest_sig = std([YTest{:}],0,2);
for i = 1:numel(XTrain)
XTrain{i} = (XTrain{i} - XTrain_mu) ./ XTrain_sig ;
YTrain{i}=(YTrain{i} - YTrain_mu) ./ YTrain_sig;
end
for i = 1:numel(XTest)
XTest{i}=(XTest{i} - XTest_mu) ./ XTest_sig;
YTest{i}=(YTest{i} - YTest_mu) ./ YTest_sig;
end
numResponses = size(YTrain{1},1);
numHiddenUnits = 200;
numFeatures=15;%变量个数
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(50)
dropoutLayer(0.5)
fullyConnectedLayer(numResponses)
regressionLayer];
maxEpochs = 90;
options = trainingOptions('adam', ...
'MaxEpochs',maxEpochs, ...
'InitialLearnRate',0.01, ...
'GradientThreshold',1, ...
'Shuffle','never', ...
'Plots','training-progress',...
'Verbose',0);
net = trainNetwork(XTrain,YTrain,layers,options);
YPred = predict(net,XTest);
idx = randperm(numel(YPred),4);
figure
for i = 1:numel(idx)
subplot(2,2,i)
plot(YTest{idx(i)},'--')
hold on
plot(YPred{idx(i)},'.-')
hold off
title("Test Observation " + idx(i))
xlabel("Time Step")
ylabel("青霉素浓度")
rmse = sqrt(mean((YPred{i} - YTest{i}).^2))
end
legend(["True" "Predicted"],'Location','southeast')
XTrain_mu = mean([XTrain{:}],2);
XTrain_sig = std([XTrain{:}],0,2);
XTest_mu = mean([XTest{:}],2);
XTest_sig = std([XTest{:}],0,2);
YTrain_mu = mean([YTrain{:}],2);
YTrain_sig = std([YTrain{:}],0,2);
YTest_mu = mean([YTest{:}],2);
YTest_sig = std([YTest{:}],0,2);
for i = 1:numel(XTrain)
XTrain{i} = (XTrain{i} - XTrain_mu) ./ XTrain_sig ;
YTrain{i}=(YTrain{i} - YTrain_mu) ./ YTrain_sig;
end
for i = 1:numel(XTest)
XTest{i}=(XTest{i} - XTest_mu) ./ XTest_sig;
YTest{i}=(YTest{i} - YTest_mu) ./ YTest_sig;
end
numResponses = size(YTrain{1},1);
numHiddenUnits = 200;
numFeatures=15;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(50)
dropoutLayer(0.5)
fullyConnectedLayer(numResponses)
regressionLayer];
maxEpochs = 90;
options = trainingOptions('adam', ...
'MaxEpochs',maxEpochs, ...
'InitialLearnRate',0.01, ...
'GradientThreshold',1, ...
'Shuffle','never', ...
'Plots','training-progress',...
'Verbose',0);
net = trainNetwork(XTrain,YTrain,layers,options);
YPred = predict(net,XTest);
idx = randperm(numel(YPred),4);
figure
for i = 1:numel(idx)
subplot(2,2,i)
plot(YTest{idx(i)},'--')
hold on
plot(YPred{idx(i)},'.-')
hold off
title("Test Observation " + idx(i))
xlabel("Time Step")
ylabel("青霉素浓度")
rmse = sqrt(mean((YPred{i} - YTest{i}).^2))
end
legend(["True" "Predicted"],'Location','southeast')
备注:市面上主流的网络都可以使用matlab的深度学习工具箱自行搭建,避免复杂的环境配置,如果不搞算法研究的话还是很好用的,强烈推荐。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。