主要内容

时间序列预测使用深度学习

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

为了预测序列的未来时间步骤的值,您可以训练序列到序列回归LSTM网络,其中响应是训练序列,值逐步移动一个时间步。也就是说,在输入序列的每个时间步骤中,LSTM网络都学会预测下一步步骤的值。

To forecast the values of multiple time steps in the future, use thepredictAndUpdateStatefunction to predict time steps one at a time and update the network state at each prediction.

此示例使用数据集chanchpox_dataset。The example trains an LSTM network to forecast the number of chickenpox cases given the number of cases in previous months.

Load Sequence Data

Load the example data.chanchpox_datasetcontains a single time series, with time steps corresponding to months and values corresponding to the number of cases. The output is a cell array, where each element is a single time step. Reshape the data to be a row vector.

data = chickenpox_dataset; data = [data{:}]; figure plot(data) xlabel("Month")ylabel("Cases") 标题(“每月水痘案例”)

Partition the training and test data. Train on the first 90% of the sequence and test on the last 10%.

numtimestepstrain = floor(0.9*numel(data));datatrain = data(1:numtimestepstrain+1);dataTest = data(numtimestepstrain+1:end);

标准化数据

For a better fit and to prevent the training from diverging, standardize the training data to have zero mean and unit variance. At prediction time, you must standardize the test data using the same parameters as the training data.

mu =平均值(dataTrain);sig = std(dataTrain);dataTrainStandardized =(DataTrain -Mu) / sig;

准备预测因素和响应

To forecast the values of future time steps of a sequence, specify the responses to be the training sequences with values shifted by one time step. That is, 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.

Xtrain = DataTrainStandardized(1:End-1);ytrain = dataTrainStandardized(2:end);

Define LSTM Network Architecture

创建LSTM回归网络。指定LSTM层具有200个隐藏单元。

numFeatures = 1; numResponses = 1; numHiddenUnits = 200; layers = [。。。sequenceInputLayer(numFeatures) lstmLayer(numHiddenUnits) fullyConnectedLayer(numResponses) regressionLayer];

Specify the training options. Set the solver to'adam'和250个时代的火车。为防止梯度爆炸,请将梯度阈值设置为1。指定初始学习率为0.005,并在125个时期后通过乘以0.2倍来降低学习率。

选项=训练('adam',。。。“MaxEpochs”,250,。。。“梯度阈值”,1,。。。'InitialLearnRate',0.005,。。。“学习者”,'piecewise',。。。'LearnRateDropPeriod',125,。。。'LearnRateDropFactor',0.2,。。。“冗长”,0,。。。“阴谋”,'training-progress');

Train LSTM Network

Train the LSTM network with the specified training options by usingtrainNetwork

net = trainnetwork(Xtrain,Ytrain,Layers,options);

Forecast Future Time Steps

To forecast the values of multiple time steps in the future, use thepredictAndUpdateStatefunction to predict time steps one at a time and update the network state at each prediction. For each prediction, use the previous prediction as input to the function.

Standardize the test data using the same parameters as the training data.

dataTestStandardized = (dataTest - mu) / sig; XTest = dataTestStandardized(1:end-1);

To initialize the network state, first predict on the training dataXTrain。接下来,使用培训响应的最后时间进行第一个预测ytrain(结束)。在其余预测上循环并输入先前的预测predictAndUpdateState

For large collections of data, long sequences, or large networks, predictions on the GPU are usually faster to compute than predictions on the CPU. Otherwise, predictions on the CPU are usually faster to compute. For single time step predictions, use the CPU. To use the CPU for prediction, set the'ExecutionEnvironment'option ofpredictAndUpdateStateto'cpu'

net = preditionAndUpDateState(net,Xtrain);[net,ypred] = preditionAndUpDateState(net,ytrain(end));numtimestepstest = numel(xtest);fori = 2:numtimestepstest [net,ypred(:,i)] = predictandupdateState(net,ypred(:,, i-1),'ExecutionEnvironment','cpu');end

使用前面计算的参数来清除预测。

ypred = sig*ypred + mu;

训练进度图报告了从标准化数据计算出的根平方误差(RMSE)。从非标准化的预测中计算RMSE。

YTest = dataTest(2:end); rmse = sqrt(mean((YPred-YTest).^2))
rmse =single248.5531

Plot the training time series with the forecasted values.

图图(DataTrain(1:End-1))保持idx = numtimestepstrain :( numtimestepstrain+numtimestepstest);绘图(idx,[data(numtimestepstrain)ypred],'.-') 抓住离开Xlabel("Month")ylabel("Cases") 标题("Forecast") 传奇(["Observed""Forecast")))

将预测值与测试数据进行比较。

图子图(2,1,1)图(YTest)保持plot(YPred,'.-') 抓住离开传奇(["Observed""Forecast")))ylabel("Cases") 标题("Forecast") subplot(2,1,2) stem(YPred - YTest) xlabel("Month")ylabel("Error") 标题(“ rmse =”+ rmse)

Update Network State with Observed Values

If you have access to the actual values of time steps between predictions, then you can update the network state with the observed values instead of the predicted values.

首先,初始化网络状态。要对新序列进行预测,请使用重置。重置网络状态可防止以前的预测影响新数据的预测。重置网络状态,然后通过预测培训数据来初始化网络状态。

net =重置(net);net = preditionAndUpDateState(net,Xtrain);

Predict on each time step. For each prediction, predict the next time step using the observed value of the previous time step. Set the'ExecutionEnvironment'option ofpredictAndUpdateStateto'cpu'

ypred = [];numtimestepstest = numel(xtest);fori = 1:numtimestepstest [net,ypred(:,i)] = predictAndUpDateState(net,xtest(::,i),'ExecutionEnvironment','cpu');end

使用前面计算的参数来清除预测。

ypred = sig*ypred + mu;

计算根平方误差(RMSE)。

rmse = sqrt(mean((YPred-YTest).^2))
RMSE = 158.0959

将预测值与测试数据进行比较。

图子图(2,1,1)图(YTest)保持plot(YPred,'.-') 抓住离开传奇(["Observed"“预料到的”)))ylabel("Cases") 标题(“预测更新”) subplot(2,1,2) stem(YPred - YTest) xlabel("Month")ylabel("Error") 标题(“ rmse =”+ rmse)

在这里,使用观察值而不是预测值更新网络状态时,预测更为准确。

See Also

|||

相关话题