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

时间:2018-05-24 12:07:03

标签: matlab deep-learning time-series lstm

我在https://uk.mathworks.com/help/nnet/examples/time-series-forecasting-using-deep-learning.html

中使用MathWorks的时间序列预测示例

上述网址中的输出是:

enter image description here

我只更改了数据集并运行了算法。令人惊讶的是,该算法对我的数据集效果不佳,并按如下方式生成一条预测线:

enter image description here

我真的很困惑,我无法理解背后的原因。我可能需要调整我不知道的算法中的参数。我正在使用的代码是:

%% Load Data
%data = chickenpox_dataset;
%data = [data{:}];

data = xlsread('data.xlsx');
data = data';

%% Divide Data: Training and Testing
numTimeStepsTrain = floor(0.7*numel(data));
XTrain = data(1:numTimeStepsTrain);
YTrain = data(2:numTimeStepsTrain+1);
XTest = data(numTimeStepsTrain+1:end-1);
YTest = data(numTimeStepsTrain+2:end);

%% Standardize Data
mu = mean(XTrain);
sig = std(XTrain);

XTrain = (XTrain - mu) / sig;
YTrain = (YTrain - mu) / sig;
XTest = (XTest - mu) / sig;

%% Define LSTM Network
inputSize = 1;
numResponses = 1;
numHiddenUnits = 500;

layers = [ ...
    sequenceInputLayer(inputSize)
    lstmLayer(numHiddenUnits)
    fullyConnectedLayer(numResponses)
    regressionLayer];

%% Training Options
opts = trainingOptions('adam', ...
    'MaxEpochs',500, ...
    'GradientThreshold',1, ...
    'InitialLearnRate',0.005, ...
    'LearnRateSchedule','piecewise', ...
    'LearnRateDropPeriod',125, ...
    'LearnRateDropFactor',0.2, ...
    'Verbose',0, ...
    'Plots','training-progress');

%% Train Network
net = trainNetwork(XTrain,YTrain,layers,opts);

%% Forecast Future Time Steps
net = predictAndUpdateState(net,XTrain);
[net,YPred] = predictAndUpdateState(net,YTrain(end));

numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
    [net,YPred(1,i)] = predictAndUpdateState(net,YPred(i-1));
end

%% Unstandardize the predictions using mu and sig calculated earlier.
YPred = sig*YPred + mu;

%% RMSE and MAE Calculation
rmse = sqrt(mean((YPred-YTest).^2))
MAE = mae(YPred-YTest)

%% Plot results
figure
plot(data(1:numTimeStepsTrain))
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
plot(idx,[data(numTimeStepsTrain) YPred],'.-')
hold off
xlabel("Month")
ylabel("Cases")
title("Forecast")
legend(["Observed" "Forecast"])

%% Compare the forecasted values with the test data
figure
subplot(2,1,1)
plot(YTest)
hold on
plot(YPred,'.-')
hold off
legend(["Observed" "Forecast"])
ylabel("Cases")
title("Forecast")

subplot(2,1,2)
stem(YPred - YTest)
xlabel("Month")
ylabel("Error")
title("RMSE = " + rmse)

data.xlsx位于:https://www.dropbox.com/s/vv1apug7iqlocu1/data.xlsx?dl=1

1 个答案:

答案 0 :(得分:-2)

您想要在数据中找到时间模式。 Matlab的数据看起来像带有噪声的正弦波,非常清晰的图案。您的数据远未显示清晰的模式。您的数据需要预处理。我会从消除缓慢的漂移开始。某种高通或带通滤波器是有道理的。这是一个简单的行,只是为了在没有慢频的情况下快速查看数据:

T=readtable('data.xlsx','readvariablenames',0);
figure; plot(T.Var1-smoothdata(T.Var1,'movmean',200))