Error in using LSTM for multipe responses

2 views (last 30 days)
clear all; clc;
Data=xlsread("C:\Users\vsvbh\OneDrive\Documents\LSTMPRBS.xlsx")
height1=Data(:,2)
height2=Data(:,3)
height3=Data(:,4)
flowrate1=Data(:,5)
flowrate2=Data(:,6)
[row col]=size(Data);
%create database for lstm
SequenceLength=1
Database={}
resultDatabase=[]
count=0
for i=row:-1:SequenceLength
count=count+1
a=height1(i-SequenceLength+1:i)';
a=flip(a);
b=height2(i-SequenceLength+1:i)';
b=flip(b);
c=height3(i-SequenceLength+1:i)';
c=flip(c);
d=flowrate1(i-SequenceLength+1:i)';
d=flip(d);
e=flowrate2(i-SequenceLength+1:i)';
e=flip(e);
Database{count,1}=[a;b;c;d;e];
end
[rt ct]=size(Database)
count=0
for i=1:rt-1
count=count+1
a=Database{count,1};
b=Database{count,1};
c=Database{count,1};
resultDatabase1(count,1)=a(1,end);
resultDatabase2(count,1)=b(2,end);
resultDatabase3(count,1)=c(3,end);
end
HeightDatabase=Database;
save HeightDatabase
%lstm creation
for i=1:rt-300
stockTrain{i,1}=Database{i,1}
end
restTrain1=resultDatabase1(1:rt-300)
restTrain2=resultDatabase2(1:rt-300)
restTrain3=resultDatabase3(1:rt-300)
restTrain={}
for i=1:700
restTrain{i,1}={restTrain1(i,:);restTrain2(i,:);restTrain3(i,:)}
end
numFeatures=5
numHiddenLayers1=100
numHiddenLayers2=100
numResponses=3
Layers=[ sequenceInputLayer(numFeatures),...
lstmLayer(numHiddenLayers1,'OutputMode','Last'),...
lstmLayer(numHiddenLayers2,'OutputMode','Last'),...
fullyConnectedLayer(numResponses),...
regressionLayer
]
miniBatchSize=20;
Epoch=200;
options=trainingOptions('adam',...
'ExecutionEnvironment','auto',...
'MaxEpochs',Epoch,...
'MiniBatchSize',miniBatchSize,...
'plots','training-progress')
Trainedheight=trainNetwork(stockTrain,restTrain,Layers,options)
save Trainedheight
i need to trian the data for three responses i have 1000 data points out of which i used 700 to train . so i need to train the data for all the three but it was giving a error

Answers (1)

Milan Bansal
Milan Bansal on 15 Sep 2023
Hi,
In my understanding you are facing an error when training regression LSTM (Long Short Term Memory) model for multiple responses (3 in this case).
The issue is with the data type of "restTrain" which is a "cell array". The target response in "trainNetwork" function for the regression task must be a vector or a matrix.
Create "restTrain" as a matrix instead of a cell array. See the code below.
restTrain = [restTrain1 restTrain2 restTrain3];
Refer to the documentation link to know more about format of target responses in "trainNetwork" functionf for different tasks.

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!