MATLAB Answers

stacked LSTm Code for time series forecasting

78 views (last 30 days)
NN
NN on 11 Dec 2020
Commented: Abhishek Gupta on 11 Jan 2021
Can anyone guide me how to write code for stacked lstm in the below code:
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];

  0 Comments

Sign in to comment.

Answers (2)

Abhishek Gupta
Abhishek Gupta on 15 Dec 2020
Hi,
As per my understanding, you want to define an LSTM model comprise of multiple LSTM layers. One can perform this task as follows: -
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits1)
lstmLayer(numHiddenUnits2)
fullyConnectedLayer(numClasses)
regressionLayer];
Referring to the following documentation for more information: -

  0 Comments

Sign in to comment.


nahed zemouri
nahed zemouri on 11 Jan 2021
hello
ihave a question about times series forecasting using LSTM
this is my program and i have this error please help me
close all; clear; clc;
load Tucson;
Input1= GHI_hour_Y1;
Input2=GHI_hour_Y2;
Pmpp=[Input1, Input2];%%%%%vecteur
%Output data
for i=1:length(Pmpp)
if Pmpp(i) <0
Pmpp(i)=0;
else
Pmpp(i)=Pmpp(i);
end
end
%Partition the training and test data.
numTimeStepsTrain = floor(0.7*numel(Pmpp));
dataTrain_S = Pmpp(1:numTimeStepsTrain+1);
dataTest_S = Pmpp(numTimeStepsTrain+1:end);
dataTrain_Pmpp = Pmpp(1:numTimeStepsTrain+1);
dataTest_Pmpp = Pmpp(numTimeStepsTrain+1:end);
%%Standardize Data
mu_S = mean(dataTrain_S);
sig_S = std(dataTrain_S);
dataTrainStandardized_S = (dataTrain_S - mu_S) / sig_S;
mu_Pmpp = mean(dataTrain_Pmpp);
sig_Pmpp = std(dataTrain_Pmpp);
dataTrainStandardized_Pmpp = (dataTrain_Pmpp - mu_Pmpp) / sig_Pmpp;
%%Prepare Predictors and Responses
XTrain = dataTrainStandardized_S(1:end-1);
XTrain = XTrain';
YTrain = dataTrainStandardized_Pmpp(1:end-1);
YTrain = YTrain';
for i=1:length(YTrain)
if YTrain(i) <0
YTrain(i)=0;
else
YTrain(i)=YTrain(i);
end
end
%%Define LSTM Network Architecture
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 2;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',2, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
%%Train LSTM Network
net = trainNetwork(XTrain',YTrain',layers,options);
return
%%Forecast Future Time Steps
%Standardize the test data using the same parameters as the training data.
net = predictAndUpdateState(net,XTrain);
Error using nnet.internal.cnn.util.NetworkDataValidator/assertValidSequenceInput
(line 493)
The prediction sequences are of feature dimension 4949 but the input layer expects
sequences of feature dimension 1.

  1 Comment

Abhishek Gupta
Abhishek Gupta on 11 Jan 2021
To get a quick answer, I suggest you create a new question rather than posting it as an answer here.
For more information: -

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!