MATLAB Answers

stacked LSTm Code for time series forecasting

35 views (last 30 days)
NN
NN on 11 Dec 2020
Answered: NEGAR MONFARED on 2 Jul 2021 at 13:32
Can anyone guide me how to write code for stacked lstm in the below code:
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];

Answers (3)

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: -

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.


NEGAR MONFARED
NEGAR MONFARED on 2 Jul 2021 at 13:32
Hello . I hope you have a good day. I sent the article to your service. I implemented the coding part in the MATLAB software, but to implement my network, two lines of setlayers, training MATLAB 2014 give me an error. What other function do you think I should replace? Do you think the codes I wrote are correct?( I used gait-in-neurodegenerative-disease-database in physionet website.) Thanks a lot

Community Treasure Hunt

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

Start Hunting!

Translated by