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

46 views (last 30 days)

Show older comments

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 Comment

Davey Gregg
on 6 Apr 2022

Edited: Davey Gregg
on 6 Apr 2022

### Answers (3)

H Sanchez
on 30 Apr 2021

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
on 10 Dec 2020

Edited: KSSV
on 7 Aug 2022

Dear Gupta,

i have written a prediction code that uses CNNs and LSTM to forecast future values.

please visit my Mathworks page,

##### 5 Comments

Imola Fodor
on 3 Mar 2022

Raunak Gupta
on 19 Jul 2020

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 Comments

Nazila Pourhajy
on 3 Nov 2021

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.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!