【MATLAB第55期】#源码分享| 基于MATLAB的LSTM长短期记忆网络的一列数据时间序列预测模型
- 此示例说明如何使用长短期记忆(LSTM)网络预测时间序列数据。
- 为了预测序列的未来时间步长的值,可以训练一个序列到序列回归LSTM网络,其中的响应值移动了一个时间步长的训练序列。也就是说,在输入序列的每个时间步长,LSTM网络学习预测下一个时间步长的值。
- 要预测未来多个时间点的值,请使用forectAndUpdateState函数一次预测一个时间点,并在每次预测时更新网络状态。
一、加载数据
- 加载示例数据。某股票的收盘价,包含单个时间序列,时间步长为每日,值对应于每日收盘价。
- 输出是一个单元数组,其中每个元素都是单个时间步长。将数据重塑为行矢量。
% 导入数据
clear,clc
data = importdata('out.txt');
data = data';
figure()
plot(data,LineWidth=2)
二、 数据拆分
划分训练和测试数据。在序列的前90%进行训练,在最后10%进行测试。
% 定义测试与训练长度
numTimeStepsTrain = floor(0.9*numel(data));
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);
三、数据归一化
- 为了更好地匹配和防止训练发散,将训练数据标准化为零均值和单位方差。
- 在预测时,您必须使用与训练数据相同的参数来标准化测试数据。
% 训练数据归一化
mu = mean(dataTrain);
sig = std(dataTrain);
dataTrainStandardized = (dataTrain - mu) / sig;
要预测序列的未来时间步长的值,请将响应指定为值移位一个时间步长的训练序列。也就是说,在输入序列的每个时间步长,LSTM网络学习预测下一个时间步长的值。
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
四、定义网络结构
- 创建LSTM回归网络。
- 将LSTM层指定为具有128个隐藏单元。
- 训练参数可查看帮助,明白其中含义。
% 定义网络结构
layers = [
sequenceInputLayer(1,"Name","input")
lstmLayer(128,"Name","lstm")
% dropoutLayer(0.2,"Name","drop")
fullyConnectedLayer(1,"Name","fc")
regressionLayer];
% 定义训练参数
options = trainingOptions('adam', ...
'MaxEpochs',250, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
五、训练网络
使用TrainNetwork训练LSTM网络。
% 训练网络
net = trainNetwork(XTrain,YTrain,layers,options);
六、预测
1.使用预测值更新网络状态
- 要预测未来多个时间点的值,请使用forectAndUpdateState函数一次预测一个时间点,并在每次预测时更新网络状态。
- 对于每个预测,使用先前的预测作为函数的输入。使用与训练数据相同的参数对测试数据进行标准化。
% 测试集归一化
dataTestStandardized = (dataTest - mu) / sig;
XTest = dataTestStandardized(1:end-1);
% 预测
net = predictAndUpdateState(net,XTrain);
[net,YPred] = predictAndUpdateState(net,YTrain(end));
numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
end
- 为了初始化网络状态,首先对训练数据XTrain进行预测。
- 接下来,使用训练响应YTrain的最后一个时间步长进行第一个预测(完)。
- 循环其余的预测,并将先前的预测输入到forectAndUpdateState。
- 对于大型数据集合、长序列或大型网络,在GPU上计算预测通常比在CPU上计算预测快。否则,在CPU上进行预测的计算速度通常会更快。对于单时间步长预测,请使用CPU。要使用CPU进行预测,请将recrectAndUpdateState的‘ExecutionEnvironment’选项设置为‘CPU’
- 使用先前计算的参数来取消预测的标准化。
YPred = sig*YPred + mu;
用预测值绘制训练时间序列。
% 绘图
figure
plot(dataTrain(1:end-1))
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
plot(idx,[data(numTimeStepsTrain) YPred],'k.-'),hold on
plot(idx,data(numTimeStepsTrain:end-1),'r'),hold on
hold off
xlabel("Day")
ylabel("P")
title("Forecast")
legend(["Observed" "Forecast"])
2.使用测试数据更新网络状态
- 如果您可以访问预测之间时间步长的实际值,则可以使用观测值而不是预测值来更新网络状态。
- 首先,初始化网络状态。要对新序列进行预测,请使用Reset State重置网络状态。重置网络状态可防止先前的预测影响对新数据的预测。
- 重置网络状态,然后通过对训练数据进行预测来初始化网络状态。
net = resetState(net);
net = predictAndUpdateState(net,XTrain);
之后预测与前面基本一样,只不过用XTest中的数据更新网络状态
% 重置网络状态
net = resetState(net);
net = predictAndUpdateState(net,XTrain);
%
YPred = [];
numTimeStepsTest = numel(XTest);
for i = 1:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,XTest(:,i),'ExecutionEnvironment','cpu');
end
YPred = sig*YPred + mu;
% 绘图
figure
plot(dataTrain(1:end-1))
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
plot(idx,[data(numTimeStepsTrain) YPred],'k.-'),hold on
plot(idx,data(numTimeStepsTrain:end-1),'r'),hold on
hold off
xlabel("Day")
ylabel("P")
title("Forecast")
legend(["Observed" "Forecast"])
七、数据
数据重命名为out.txt
1.885999999999999899e+00
1.883000000000000007e+00
1.877999999999999892e+00
1.893999999999999906e+00
1.872999999999999998e+00
1.893000000000000016e+00
1.893000000000000016e+00
1.909999999999999920e+00
1.903999999999999915e+00
1.893000000000000016e+00
1.887000000000000011e+00
1.891999999999999904e+00
1.905000000000000027e+00
1.875999999999999890e+00
1.879999999999999893e+00
1.832000000000000073e+00
1.709000000000000075e+00
1.745000000000000107e+00
1.762999999999999901e+00
1.794000000000000039e+00
1.792000000000000037e+00
1.800999999999999934e+00
1.816000000000000059e+00
1.830999999999999961e+00
1.822999999999999954e+00
1.832999999999999963e+00
1.872999999999999998e+00
1.870999999999999996e+00
1.866000000000000103e+00
1.895999999999999908e+00
1.903000000000000025e+00
1.901000000000000023e+00
1.901999999999999913e+00
1.879999999999999893e+00
1.889999999999999902e+00
1.830000000000000071e+00
1.885999999999999899e+00
1.899000000000000021e+00
1.913999999999999924e+00
1.949000000000000066e+00
1.923000000000000043e+00
1.872000000000000108e+00
1.907000000000000028e+00
1.883000000000000007e+00
1.854000000000000092e+00
1.834999999999999964e+00
1.766999999999999904e+00
1.762999999999999901e+00
1.737999999999999989e+00
1.727000000000000091e+00
1.741999999999999993e+00
1.711999999999999966e+00
1.733000000000000096e+00
1.758999999999999897e+00
1.754999999999999893e+00
1.752999999999999892e+00
1.737999999999999989e+00
1.743000000000000105e+00
1.741999999999999993e+00
1.766000000000000014e+00
1.760000000000000009e+00
1.794000000000000039e+00
1.784000000000000030e+00
1.788000000000000034e+00
1.780999999999999917e+00
1.776000000000000023e+00
1.804000000000000048e+00
1.792000000000000037e+00
1.794000000000000039e+00
1.812999999999999945e+00
1.822000000000000064e+00
1.800000000000000044e+00
1.814999999999999947e+00
1.812000000000000055e+00
1.796999999999999931e+00
1.810000000000000053e+00
1.824000000000000066e+00
1.826000000000000068e+00
1.844999999999999973e+00
1.856000000000000094e+00
1.855299999999999949e+00
1.865899999999999892e+00
1.866200000000000081e+00
1.867699999999999916e+00
1.870500000000000052e+00
1.860300000000000065e+00
1.858400000000000052e+00
1.862400000000000055e+00
1.870800000000000018e+00
1.864800000000000013e+00
1.861399999999999944e+00
1.840200000000000058e+00
1.841599999999999904e+00
1.850500000000000034e+00
1.845099999999999962e+00
1.847699999999999898e+00
1.851399999999999935e+00
1.876600000000000046e+00
1.883199999999999985e+00
1.884200000000000097e+00
1.883299999999999974e+00
1.885399999999999965e+00
1.889000000000000012e+00
1.895899999999999919e+00
1.894800000000000040e+00
1.884400000000000075e+00
1.887100000000000000e+00
1.879000000000000004e+00
1.892099999999999893e+00
1.892500000000000071e+00
1.901599999999999957e+00
1.915399999999999991e+00
1.915999999999999925e+00
1.920500000000000096e+00
1.926900000000000057e+00
1.920600000000000085e+00
1.937300000000000022e+00
1.958399999999999919e+00
1.979000000000000092e+00
2.000500000000000167e+00
2.068700000000000205e+00
2.077300000000000146e+00
2.100999999999999979e+00
2.122700000000000031e+00
2.107899999999999885e+00
2.137100000000000222e+00
2.130500000000000060e+00
2.110300000000000065e+00
2.069199999999999928e+00
2.077999999999999847e+00
2.116400000000000059e+00
2.117799999999999905e+00
2.124600000000000044e+00
2.121300000000000185e+00
2.067299999999999915e+00
2.066399999999999793e+00
2.080299999999999816e+00
2.106100000000000083e+00
2.100600000000000023e+00
2.111200000000000188e+00
2.130199999999999871e+00
2.137399999999999967e+00
2.140099999999999891e+00
2.140000000000000124e+00
2.124800000000000022e+00
2.137599999999999945e+00
2.124800000000000022e+00
2.118700000000000028e+00
2.116400000000000059e+00
2.135899999999999910e+00
2.166500000000000092e+00
2.166700000000000070e+00
2.149099999999999788e+00
2.137700000000000156e+00
2.146900000000000031e+00
2.165299999999999780e+00
2.167699999999999960e+00
2.152800000000000047e+00
2.157799999999999940e+00
2.186399999999999899e+00
2.179100000000000037e+00
2.189099999999999824e+00
2.187199999999999811e+00
2.179399999999999782e+00
2.166500000000000092e+00
2.138799999999999812e+00
2.145599999999999952e+00
2.116400000000000059e+00
2.115800000000000125e+00
2.125799999999999912e+00
2.132699999999999818e+00
2.142999999999999794e+00
2.136699999999999822e+00
2.133000000000000007e+00
2.162700000000000067e+00
2.151800000000000157e+00
2.142100000000000115e+00
2.145599999999999952e+00
2.124800000000000022e+00
2.126900000000000013e+00
2.129500000000000171e+00
2.130900000000000016e+00
2.129599999999999937e+00
2.153799999999999937e+00
2.191899999999999959e+00
2.195300000000000029e+00
2.188499999999999890e+00
2.183100000000000041e+00
2.182399999999999896e+00
2.172099999999999920e+00
2.180600000000000094e+00
2.176899999999999835e+00
2.169999999999999929e+00
2.153399999999999981e+00
2.142500000000000071e+00
2.143100000000000005e+00
2.149300000000000210e+00
2.157399999999999984e+00
2.136000000000000121e+00
2.141700000000000159e+00
2.157000000000000028e+00
2.163600000000000190e+00
2.182199999999999918e+00
2.180800000000000072e+00
2.201999999999999957e+00
2.193599999999999994e+00
2.179399999999999782e+00
2.179399999999999782e+00
2.164499999999999869e+00
2.175300000000000011e+00
2.174100000000000144e+00
2.177500000000000213e+00
2.184699999999999864e+00
2.188099999999999934e+00
2.207500000000000018e+00
2.202100000000000168e+00
2.184000000000000163e+00
2.188000000000000167e+00
2.198700000000000099e+00
2.194700000000000095e+00
2.222199999999999953e+00
2.220499999999999918e+00
2.220600000000000129e+00
2.225400000000000045e+00
2.215500000000000025e+00
2.211899999999999977e+00
2.194700000000000095e+00
2.191800000000000193e+00
2.177299999999999791e+00
2.186999999999999833e+00
2.187600000000000211e+00
2.190399999999999903e+00
2.207599999999999785e+00
2.201099999999999834e+00
2.212499999999999911e+00
2.192499999999999893e+00
2.199399999999999800e+00
2.194700000000000095e+00
2.206799999999999873e+00
2.211800000000000210e+00
2.206100000000000172e+00
2.227100000000000080e+00
2.250799999999999912e+00
2.259500000000000064e+00
2.278099999999999792e+00
2.289000000000000146e+00
2.310599999999999987e+00
2.302799999999999958e+00
2.281499999999999861e+00
2.317600000000000104e+00
2.306299999999999795e+00
2.281699999999999839e+00
2.280800000000000161e+00
2.291700000000000070e+00
2.277099999999999902e+00
2.291799999999999837e+00
2.320100000000000051e+00
2.326299999999999812e+00
2.339399999999999924e+00
2.314299999999999802e+00
2.319900000000000073e+00
2.282000000000000028e+00
2.275700000000000056e+00
2.291999999999999815e+00
2.314499999999999780e+00
2.310700000000000198e+00
2.305699999999999861e+00
2.313699999999999868e+00
2.334000000000000075e+00
2.363300000000000178e+00
2.394400000000000084e+00
2.387100000000000222e+00
2.390299999999999869e+00
2.342499999999999805e+00
2.336199999999999832e+00
2.304800000000000182e+00
2.310400000000000009e+00
2.281899999999999817e+00
2.301499999999999879e+00
2.286799999999999944e+00
2.308800000000000185e+00
2.268899999999999917e+00
2.266500000000000181e+00
2.222599999999999909e+00
2.195600000000000218e+00
2.204600000000000115e+00
2.238199999999999967e+00
2.241099999999999870e+00
2.212800000000000100e+00
2.222300000000000164e+00
2.228600000000000136e+00
2.239399999999999835e+00
2.212000000000000188e+00
2.224400000000000155e+00
2.209900000000000198e+00
2.188099999999999934e+00
2.189799999999999969e+00
2.218300000000000161e+00
2.218100000000000183e+00
2.229299999999999837e+00
2.221600000000000019e+00
2.238599999999999923e+00
2.250900000000000123e+00
2.246700000000000141e+00
2.240200000000000191e+00
2.241200000000000081e+00
2.223899999999999988e+00
2.197000000000000064e+00
2.192200000000000149e+00
2.202700000000000102e+00
2.194500000000000117e+00
2.202799999999999869e+00
2.232099999999999973e+00
2.233800000000000008e+00
2.238399999999999945e+00
2.237999999999999989e+00
2.251399999999999846e+00
2.239900000000000002e+00
2.245400000000000063e+00
2.255300000000000082e+00
2.263199999999999878e+00
2.253200000000000092e+00
2.239500000000000046e+00
2.224400000000000155e+00
2.222100000000000186e+00
2.226900000000000102e+00
2.234399999999999942e+00
2.219800000000000217e+00
2.245200000000000085e+00
2.266999999999999904e+00
2.270799999999999930e+00
2.265600000000000058e+00
2.267599999999999838e+00
2.258199999999999985e+00
2.263399999999999856e+00
2.302400000000000002e+00
2.302000000000000046e+00
2.307399999999999896e+00
2.300199999999999800e+00
2.300800000000000178e+00
2.303799999999999848e+00
2.291399999999999881e+00
2.282500000000000195e+00
2.286099999999999799e+00
2.289200000000000124e+00
2.279500000000000082e+00
2.281099999999999905e+00
2.289499999999999869e+00
2.284899999999999931e+00
2.272200000000000220e+00
2.254900000000000126e+00
2.261899999999999800e+00
2.261499999999999844e+00
2.258799999999999919e+00
2.271100000000000119e+00
2.277600000000000069e+00
2.283300000000000107e+00
2.305499999999999883e+00
2.310400000000000009e+00
2.298799999999999955e+00
2.312699999999999978e+00
2.314799999999999969e+00
2.281099999999999905e+00
2.283900000000000041e+00
2.288400000000000212e+00
2.300499999999999989e+00
2.292300000000000004e+00
2.289600000000000080e+00
2.302200000000000024e+00
2.310099999999999820e+00
2.297099999999999920e+00
2.311799999999999855e+00
2.300499999999999989e+00
2.303900000000000059e+00
2.299100000000000144e+00
2.306599999999999984e+00
2.314499999999999780e+00
2.297400000000000109e+00
2.255999999999999783e+00
2.210799999999999876e+00
2.210100000000000176e+00
2.230799999999999894e+00
2.221699999999999786e+00
2.254199999999999982e+00
2.258399999999999963e+00
2.266399999999999970e+00
2.254700000000000149e+00
2.248800000000000132e+00
2.275199999999999889e+00
2.293000000000000149e+00
2.288899999999999935e+00
2.284199999999999786e+00
2.278999999999999915e+00
2.278599999999999959e+00
2.248699999999999921e+00
2.260899999999999910e+00
2.250799999999999912e+00
2.221400000000000041e+00
2.242100000000000204e+00
2.257200000000000095e+00
2.266199999999999992e+00
2.241899999999999782e+00
2.251100000000000101e+00
2.254199999999999982e+00
2.249099999999999877e+00
2.266300000000000203e+00
2.267399999999999860e+00
2.261099999999999888e+00
2.286200000000000010e+00
2.306999999999999940e+00
2.302599999999999980e+00
2.296400000000000219e+00
2.309499999999999886e+00
2.309299999999999908e+00
2.287999999999999812e+00
2.274399999999999977e+00
2.256200000000000205e+00
2.270599999999999952e+00
2.258000000000000007e+00
2.260499999999999954e+00
2.259399999999999853e+00
2.265800000000000036e+00
2.267100000000000115e+00
2.245899999999999785e+00
2.258300000000000196e+00
2.268499999999999961e+00
2.266500000000000181e+00
2.246999999999999886e+00
2.262599999999999945e+00
2.260200000000000209e+00
2.266799999999999926e+00
2.253499999999999837e+00
2.261200000000000099e+00
2.259999999999999787e+00
2.259900000000000020e+00