Main Content

Time Series Forecasting Using Deep Learning

此示例显示了如何使用长期短期内存(LSTM)网络预测时间序列数据。

一个网络是一个递归神经网络(RNN LSTM) that processes input data by looping over time steps and updating the network state. The network state contains information remembered over all previous time steps. You can use an LSTM network to forecast subsequent values of a time series or sequence using previous time steps as input. To train an LSTM network for time series forecasting, train a regression LSTM network with sequence output, where the responses (targets) are the training sequences with values shifted by one time step. In other words, at each time step of the input sequence, the LSTM network learns to predict the value of the next time step.

There are two methods of forecasting: open loop and closed loop forecasting.

  • 开放循环的预测仅使用输入数据预测序列的下一个时间步长。在对后续时间步骤进行预测时,您可以从数据源收集真实值,并将其用作输入。例如,说您想预测时间步的值 t of a sequence using data collected in time steps 1 through t - 1 . To make predictions for time step t + 1 , wait until you record the true value for time step t 并以此为输入来做出下一个预测。当您具有真实值以在进行下一个预测之前提供给网络时,请使用开放循环预测。

  • Closed loop forecasting predicts subsequent time steps in a sequence by using the previous predictions as input. In this case, the model does not require the true values to make the prediction. For example, say you want to predict the values for time steps t through t + k of the sequence using data collected in time steps 1 through t - 1 only. To make predictions for time step i ,使用预测值作为时间步长 i - 1 作为输入。Use closed loop forecasting to forecast multiple subsequent time steps or when you do not have the true values to provide to the network before making the next prediction.

该图显示了使用闭环预测的示例序列,其预测值。

This example uses the Waveform data set, which contains 2000 synthetically generated waveforms of varying lengths with three channels. The example trains an LSTM network to forecast future values of the waveforms given the values from previous time steps using both closed loop and open loop forecasting.

Load Data

Load the example data fromwaveformdata.mat. The data is anumObservations-1序列的序列阵列,其中numObservationsis the number of sequences. Each sequence is a数字-经过-numTimeStepsnumeric array, where数字是序列的通道数量numTimeStepsis the number of time steps of the sequence.

loadWaveformData

View the sizes of the first few sequences.

data(1:5)
ans =5×1 cell array{3×103 double} {3×136 double} {3×140 double} {3×124 double} {3×127 double}

查看频道的数量。要训​​练网络,每个序列必须具有相同数量的通道。

数字= size(data{1},1)
numchannels = 3

Visualize the first few sequences in a plot.

figure tiledlayout(2,2)fori = 1:4 nexttile stackedplot(data{i}') xlabel("Time Step")结尾

Partition the data into training and test sets. Use 90% of the observations for training and the remainder for testing.

numObservations = numel(data); idxTrain = 1:floor(0.9*numObservations); idxTest = floor(0.9*numObservations)+1:numObservations; dataTrain = data(idxTrain); dataTest = data(idxTest);

Prepare Data for Training

To forecast the values of future time steps of a sequence, specify the targets as the training sequences with values shifted by one time step. In other words, at each time step of the input sequence, the LSTM network learns to predict the value of the next time step. The predictors are the training sequences without the final time step.

forn = 1:numel(dataTrain) X = dataTrain{n}; XTrain{n} = X(:,1:end-1); TTrain{n} = X(:,2:end);结尾

For a better fit and to prevent the training from diverging, normalize the predictors and targets to have zero mean and unit variance. When you make predictions, you must also normalize the test data using the same statistics as the training data. To easily calculate the mean and standard deviation over all sequences, concatenate the sequences in the time dimension.

muX = mean(cat(2,XTrain{:}),2); sigmaX = std(cat(2,XTrain{:}),0,2); muT = mean(cat(2,TTrain{:}),2); sigmaT = std(cat(2,TTrain{:}),0,2);forn = 1:numel(XTrain) XTrain{n} = (XTrain{n} - muX) ./ sigmaX; TTrain{n} = (TTrain{n} - muT) ./ sigmaT;结尾

Define LSTM Network Architecture

创建LSTM回归网络。

  • Use a sequence input layer with an input size that matches the number of channels of the input data.

  • 使用带有128个隐藏单元的LSTM层。隐藏单元的数量确定了该层学到多少信息。使用更多的隐藏单元可以产生更准确的结果,但更有可能导致对培训数据的过度拟合。

  • To output sequences with the same number of channels as the input data, include a fully connected layer with an output size that matches the number of channels of the input data.

  • Finally, include a regression layer.

layers = [ sequenceInputLayer(numChannels) lstmLayer(128) fullyConnectedLayer(numChannels) regressionLayer];

Specify Training Options

Specify the training options.

  • Train using Adam optimization.

  • Train for 200 epochs. For larger data sets, you might not need to train for as many epochs for a good fit.

  • In each mini-batch, left-pad the sequences so they have the same length. Left-padding prevents the network from predicting padding values at the ends of sequences.

  • Shuffle the data every epoch.

  • 在情节中显示训练进度。

  • Disable the verbose output.

options = trainingOptions("adam",...MaxEpochs=200,...SequencePaddingDirection="left",...Shuffle=“每个段”,...Plots="training-progress",...Verbose=0);

火车神经网络

Train the LSTM network with the specified training options using the火车网功能。

net = trainNetwork(XTrain,TTrain,layers,options);

测试网络

Prepare the test data for prediction using the same steps as for the training data.

使用培训数据计算出的统计数据将测试数据归一化。将目标指定为具有值的测试序列,将一个时间步骤移动,而预测因子为测试序列,而没有最后的时间步长。

forn = 1:size(dataTest,1) X = dataTest{n}; XTest{n} = (X(:,1:end-1) - muX) ./ sigmaX; TTest{n} = (X(:,2:end) - muT) ./ sigmaT;结尾

使用测试数据进行预测。指定与培训相同的填充选项。

YTest = predict(net,XTest,SequencePaddingDirection="left");

To evaluate the accuracy, for each test sequence, calculate the root mean squared error (RMSE) between the predictions and the target.

fori = 1:size(YTest,1) rmse(i) = sqrt(mean((YTest{i} - TTest{i}).^2,"all");结尾

可视化直方图中的错误。较低的值表示更高的精度。

figure histogram(rmse) xlabel("RMSE") ylabel("Frequency")

计算所有测试观测值的平均RMSE。

mean(rmse)
ans =single0.5080

预测未来时间步骤

Given an input time series or sequence, to forecast the values of multiple future time steps, use thepredictAndUpdateState功能一次预测时间步骤,并在每个预测中更新网络状态。对于每个预测,请使用先前的预测作为函数的输入。

Visualize one of the test sequences in a plot.

idx = 2; X = XTest{idx}; T = TTest{idx}; figure stackedplot(X',DisplayLabels="Channel "+(1:numchannels))xlabel("Time Step") 标题(“测试观察”+ idx)

Open Loop Forecasting

开放循环的预测仅使用输入数据预测序列的下一个时间步长。在对后续时间步骤进行预测时,您收集真实值会形成数据源并将其用作输入。例如,说您想预测时间步的值 t of a sequence using data collected in time steps 1 through t - 1 . To make predictions for time step t + 1 , wait until you record the true value for time step t 并以此为输入来做出下一个预测。当您具有真实值以在进行下一个预测之前提供给网络时,请使用开放循环预测。

Initialize the network state by first resetting the state using theresetStatefunction, then make an initial prediction using the first few time steps of the input data. Update the network state using the first 75 time steps of the input data.

net =重置(net);偏移= 75;[net,〜] = preditionAndUpDateState(net,x(:,1:offset));

为了预测进一步的预测,请循环循环时间步骤,并使用predictAndUpdateState功能。Forecast values for the remaining time steps of the test observation by looping over the time steps of the input data and using them as input to the network. The first prediction is the value corresponding to the time step偏移 + 1.

numTimeSteps = size(X,2); numPredictionTimeSteps = numTimeSteps - offset; Y = zeros(numChannels,numPredictionTimeSteps);fort = 1:numPredictionTimeSteps Xt = X(:,offset+t); [net,Y(:,t)] = predictAndUpdateState(net,Xt);结尾

Compare the predictions with the target values.

figure t = tiledlayout(numChannels,1); title(t,"Open Loop Forecasting")fori = 1:numChannels nexttile plot(T(i,:)) holdonplot(offset:numTimeSteps,[T(i,offset) Y(i,:)],' - ') ylabel("Channel "+ i)结尾xlabel("Time Step")nexttile(1)传说([[“输入”“预测”)))

闭环预测

Closed loop forecasting predicts subsequent time steps in a sequence by using the previous predictions as input. In this case, the model does not require the true values to make the prediction. For example, say you want to predict the value for time steps t through t + k of the sequence using data collected in time steps 1 through t - 1 only. To make predictions for time step i ,使用预测值作为时间步长 i - 1 作为输入。使用闭环预测来预测多个后续的时间步骤,或者在进行下一个预测之前没有真正向网络提供的值时。

Initialize the network state by first resetting the state using theresetStatefunction, then make an initial predictionZ使用输入数据的前几个时间步骤。使用输入数据的前75个时间步骤更新网络状态。

net =重置(net);offset = size(X,2); [net,Z] = predictAndUpdateState(net,X);

为了预测进一步的预测,请循环循环时间步骤,并使用predictAndUpdateState功能。通过迭代将先前的预测值传递给网络,预测接下来的200个时间步骤。由于网络不需要输入数据来做出任何进一步的预测,因此您可以指定任何时间步骤进行预测。

numPredictionTimesteps = 200;XT = Z(:,End);y = zeros(numchannels,numPredictionTimesteps);fort = 1:numPredictionTimeSteps [net,Y(:,t)] = predictAndUpdateState(net,Xt); Xt = Y(:,t);结尾

可视化图中的预测值。

numTimeSteps = offset + numPredictionTimeSteps; figure t = tiledlayout(numChannels,1); title(t,"Closed Loop Forecasting")fori = 1:numChannels nexttile plot(T(i,1:offset)) holdonplot(offset:numTimeSteps,[T(i,offset) Y(i,:)],' - ') ylabel("Channel "+ i)结尾xlabel("Time Step")nexttile(1)传说([[“输入”“预测”)))

Closed loop forecasting allows you to forecast an arbitrary number of time steps, but can be less accurate when compared to open loop forecasting because the network does not have access to the true values during the forecasting process.

See Also

|||

Related Topics