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

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!

2 件のコメント

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.
Okello
Okello 2024 年 12 月 19 日
Consider this function to prepare data into sequences.
function [feature_sequences, target_sequences] = prepareSequences(data)
% Prepare sequences for LSTM
[n_samples, n_features] = size(data);
window_size=30;
targets=data(:,1);
features=data(:,2:end);
num_sequences = n_samples - window_size;
feature_sequences = cell(num_sequences, 1);
target_sequences = cell(num_sequences, 1);
% Create sequences
for i = 1:num_sequences
feature_sequences{i} = features(i:i+window_size-1, :);
% Targets: sequence of target values matching input sequence length
target_sequences{i} = targets(i:i+window_size-1, :);% for sequence to one,
% use targets(i+window_size) as Target: single value at the end of the sequence
end
end

サインインしてコメントする。

回答 (3 件)

H Sanchez
H Sanchez 2021 年 4 月 30 日

4 投票

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

1 件のコメント

Muhammad Waqar
Muhammad Waqar 2021 年 12 月 22 日
This is an excellent contribution.

サインインしてコメントする。

Abolfazl Nejatian
Abolfazl Nejatian 2020 年 12 月 10 日
編集済み: KSSV 2022 年 8 月 7 日

1 投票

Dear Gupta,
i have written a prediction code that uses CNNs and LSTM to forecast future values.
please visit my Mathworks page,

5 件のコメント

Hiren Mewada
Hiren Mewada 2021 年 11 月 22 日
I think, your code is uses either LSTM or MLP. its not combination of CNN and LSTM.
Abolfazl Nejatian
Abolfazl Nejatian 2021 年 11 月 25 日
dear Hiren,
there are several version of codes on that link.
as i remember, MLP, LSTM, CNN, LSTM with Bayesian Optimization, Hybrid CNN_LSTM, Hybrid CNN_LSTM with Bayesian Optimization exist.
so the combination of CNN_LSTM exist on my mathwork acocunt
Hamed Majidiyan
Hamed Majidiyan 2021 年 12 月 12 日
Dear Abolfozl,
If the code can be developed for real-time data?
Regards
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 日

0 投票

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 件のコメント

Hui Li
Hui Li 2020 年 7 月 19 日
Hello, Raunak!
Thanks for your reply. What I want to do is just like the time series forecasting of solar power. The input of the network is meteological time series for 5 solar farms, such as temperature, humidity, etc, and the number of input feature is 25, the number of time step is 24. However, this problem is a little dissimilar to the common time series forecasting. I want to use 5 curves to predict the potential solar power of one solar farm in the future, so the dimension of output is 25.
I want to build my network with LSTM and CNN. However, if I use the 'flattenLayer' to link LSTM and CNN, the time step will be destroyed, and the error will be thrown out. Therefore, I wonder if there is any solution to solve my problem.
Thanks again for your kindly help!
Raunak Gupta
Raunak Gupta 2020 年 7 月 19 日
Hello,
I don't see any lstmLayer inside the Layer Graph, maybe you can add it. Also can you share a sample input and output with the code you trying with. It will help me recreating the problem and also checking if flattenLayer is indeed required or not.
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 7 月 14 日

コメント済み:

2024 年 12 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by