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

55 vues (au cours des 30 derniers jours)
Hui Li
Hui Li le 14 Juil 2020
Commenté : rohini sharma le 20 Oct 2022
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 commentaire
Davey Gregg
Davey Gregg le 6 Avr 2022
Modifié(e) : Davey Gregg le 6 Avr 2022
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.

Connectez-vous pour commenter.

Réponses (3)

H Sanchez
H Sanchez le 30 Avr 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
Abolfazl Nejatian le 10 Déc 2020
Modifié(e) : KSSV le 7 Août 2022
Dear Gupta,
i have written a prediction code that uses CNNs and LSTM to forecast future values.
please visit my Mathworks page,
  5 commentaires
Abolfazl Nejatian
Abolfazl Nejatian le 13 Déc 2021
yes, but it needs some minor changes.
Imola Fodor
Imola Fodor le 3 Mar 2022
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

Connectez-vous pour commenter.


Raunak Gupta
Raunak Gupta le 19 Juil 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 commentaires
Nazila Pourhajy
Nazila Pourhajy le 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.
rohini sharma
rohini sharma le 20 Oct 2022
is lstm can be applied in 2018 a matlab please reply

Connectez-vous pour commenter.

Catégories

En savoir plus sur Sequence and Numeric Feature Data Workflows dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by