最近利用LSTM预测老龄化比率时,计算出的数据令人疑惑,希望得到的解释:代码有没有错误,该如何预测。
附MATLAB代码
load('p.mat');
data=p(:,2)';
numTimeStepsTrain = 33;
dataTrain = data;
mu = mean(dataTrain);
sig = std(dataTrain);
dataTrainStandardized = (dataTrain - mu) / sig;
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',1000, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.0001, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',1, ...
'LearnRateDropFactor',1, ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(XTrain,YTrain,layers,options);
net = predictAndUpdateState(net,XTrain);
[net,YPred] = predictAndUpdateState(net,YTrain(end));
for i = 2:6
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
end
YPred=YPred*sig+mu;
plot(1:33,data,'bo-',34:39,YPred,'r*-')
legend('data','pred')
按照前面的数据,应该会保持上升一段时间,可LSTM算法预测出来却不对