How to use LSTM and CNN to handle a regression problem?

조회 수: 49 (최근 30일)
Hui Li
Hui Li 2020년 7월 14일
댓글: rohini sharma 2022년 10월 20일
Hi, everyone!
I am working on a solar power prediction problem. The inputs of the network are some kinds of meteological data, and the outputs are multiple time-series solar power curves. I want to build a neural network combining LSTM and CNN to realize this function. I build a network without error like this:
layers1 = [...
sequenceInputLayer([25 168 1],'Name','input') % 25 is the number of feature dimension of meteological data, and 168 is the length of time series
sequenceFoldingLayer('Name','fold')
convolution2dLayer(5,1,'Padding','same','WeightsInitializer','he','BiasInitializer','zeros','Name','conv');
reluLayer('Name','relu')
sequenceUnfoldingLayer('Name','unfold')
flattenLayer('Name','flatten')
gruLayer(512,'OutputMode','sequence','Name','gru')
fullyConnectedLayer(25,'Name','fc2')
regressionLayer('Name','output')
];
lgraph = layerGraph(layers1);
lgraph = connectLayers(lgraph,'fold/miniBatchSize','unfold/miniBatchSize');
analyzeNetwork(lgraph);
However, the flattenLayer destory the time series, and the training cannot be finished.
Therefore, is there any solution about this problem? Or is there any other correct network can realize the same function?
Thanks in advance for your time and kindly help!
  댓글 수: 1
Davey Gregg
Davey Gregg 2022년 4월 6일
편집: Davey Gregg 2022년 4월 6일
How are you arranging the data into predictors and responses? I'm trying to do something similar and I just keep getting the "Invalid training data." error.

댓글을 달려면 로그인하십시오.

답변 (3개)

H Sanchez
H Sanchez 2021년 4월 30일
To Whoever is looking for a CNN-RNN
I have created a simple template for hybrids cnn-rnn for time series forecasting. https://www.mathworks.com/matlabcentral/fileexchange/91360-time-series-forecasting-using-hybrid-cnn-rnn

Abolfazl Nejatian
Abolfazl Nejatian 2020년 12월 10일
편집: KSSV 2022년 8월 7일
Dear Gupta,
i have written a prediction code that uses CNNs and LSTM to forecast future values.
please visit my Mathworks page,
  댓글 수: 5
Abolfazl Nejatian
Abolfazl Nejatian 2021년 12월 13일
yes, but it needs some minor changes.
Imola Fodor
Imola Fodor 2022년 3월 3일
what are the changes we need for the real time prediction? i have developed a regression model (for sysid) 1dcnn + LSTM on 1500 timesteps, and it works well, but when giving an input of 500 it is performing badly..i suppose the model needs the full input sequence to perform well, which is not what i would need

댓글을 달려면 로그인하십시오.


Raunak Gupta
Raunak Gupta 2020년 7월 19일
Hi,
I am unable to understand what exactly you are doing with input and output of the network, but I think its related to either sequence to sequence regression or time series forecasting. You may follow below mentioned examples for both cases and see if it matches with your application.
  댓글 수: 4
Nazila Pourhajy
Nazila Pourhajy 2021년 11월 3일
Hi. I have a question about LSTM. My problem about sequence to sequence reression. I have input matrix(1000*8) and I want to predict a price with this input matrix. output is a column that is a price. I train LSTM with input matrix and I predict LSTM with datatest(50*8). But I want to calculate error of LSTM and I use predict function for 10 times with the same datatest and I get predicted value every time that are not different from Previous time. How I calculate RMSE for LSTM with some predict function.Here is may code:
function LSTM_net(data,dataTest,filename,range,date,varargin)
%--------------80% of data for train and 20% for validation----------------
out_day=cell.empty;
index=size(data{1,1},1)*0.8;
findex=round(index,0);
dataTrain=data(1:findex,:);
dataval=data(findex+1:end,:);
%-----------------Normalization of training/validation data----------------
dataTrain(isnan(dataTrain))=0;
dataval(isnan(dataval))=0;
dataTrain=rescale(dataTrain,0,1);
dataval=rescale(dataval,0,1);
YTrain = dataTrain(:,end)';
XTrain = dataTrain(:,1:end-1)';
XTrain = num2cell(XTrain,1);
YTrain = num2cell(YTrain,1);
yval= dataval(:,end)';
xval = dataval(:,1:end-1)';
xval = num2cell(xval,1);
yval = num2cell(yval,1);
%-----------------------Define Network Architecture------------------------
numResponses = size(YTrain{1},1);
featureDimension = size(XTrain{1},1);
numHiddenUnits = 15;
layers = [ ...
sequenceInputLayer(featureDimension)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
dropoutLayer(0.5) %%0.5
fullyConnectedLayer(numResponses)
regressionLayer];
maxepochs = 500;
options = trainingOptions('sgdm', ...
'MaxEpochs',maxepochs, ...
'InitialLearnRate',0.01, ...
'L2Regularization',0.001,...
'ValidationData',{xval,yval},...
'ValidationPatience',5,...
'ValidationFrequency',10);
%---------------------------------set test data----------------------------
dataTest=rescale(dataTest,0,1);
YTest = dataTest{k,1}(:,end)';
XTest = dataTest{k,1}(:,1:end-1)';
XTest = num2cell(XTest,1);
YTest = num2cell(YTest,1);
%---------------------------------Train the Network------------------------
out_net=single.empty;
%load('net_checkpoint__110__2021_11_01__10_49_03_555','net');
[net1,info] = trainNetwork(XTrain,YTrain,layers,options);
for i=1:10
YPred = predict(net1,XTest);
net1 = resetState(net1);
%figure;
%subplot(2,1,1);
y1 = (cell2mat(YPred(1:end, 1:end)));
%plot(y1);
%title('Forcasted');
%subplot(2,1,2);
y2 = (cell2mat(YTest(1:end, 1:end))');
%plot(y2);
%title('Observed');
y1(isnan(y1))=0;
y2(isnan(y2))=0;
%----------------------------calculate MAE,RMSE,MAPE-----------------------
out_net(i,1)=mean(info.TrainingRMSE(1,:),2);
out_net(i,2)=mean(abs(y1-y2)); %MAE
out_net(i,3)=mean(abs((y1-y2)/mean(y1))); %MAPE
out_net(i,4) = sqrt(mean((y1-y2).^2)); %RMSE
if size(varargin,1)==1 %for plot regression
predict_y(:,i)=y1;
end
end
end
I trained LSTM one time and predict it for 10 times and I get the same YPred answer every time.Is my code true?Please help me.
rohini sharma
rohini sharma 2022년 10월 20일
is lstm can be applied in 2018 a matlab please reply

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by