How to train multiple time series using one LSTM scriptt?

6 visualizaciones (últimos 30 días)
Fahad Butt
Fahad Butt el 17 de Dic. de 2022
Respondida: Rahul el 10 de Mzo. de 2025
For example, I have electricity usage data from different cities as:
year :1 2 3 4 5 6 [years]
city 1 : 1 2 3 4 5 6 [GWh]
city 2 : 1 2 3 4 5 6 [GWh]
city 3 : 1 2 3 4 5 6 [GWh]
I want to predict for year 7. I can do thhat with narxnet or narnet by using a 3x6 matrix, but these two are inconsistant. LSTM seems to be mroe accurate.
I can predict for one city using the following script. P1 is electricity data for city 1.
%% clear variables
% clc; clear; close all
%% load data
data=p1;
%% partition data
trec=length(data);
trrec=0.9*trec;
NTST=round(trrec);
datatrain=data(1:NTST+1);
datatest=data(NTST+1:end);
%% standardize data
mu=mean(datatrain);
sig=std(datatrain);
datatrainstd=(datatrain-mu)/sig;
%% display standardized data
% figure,
% hold on; grid on; box on, grid minor
% set(gca,'FontSize',40)
% set(gca, 'FontName', 'Times New Roman')
% plot(datatrainstd)
% xlabel('Months')
% ylabel('Cases')
%% train network separate predictors and response
xtrain=datatrainstd(1:end-1);
ytrain=datatrainstd(2:end);
%% define lstm network architecture
nof=1;
nor=1;
nhu=10;
layers=[
sequenceInputLayer(nof,'Name','ip')
lstmLayer(nhu,"Name",'lstm')
fullyConnectedLayer(nor,'Name','fc')
regressionLayer('Name','rg')];
%% plot layers
% lgraph=layerGraph(layers);
% plot(lgraph)
%% training options
options=trainingOptions('adam',...
'MaxEpochs',200,...
'GradientThreshold',1,...
'InitialLearnRate',0.1,...
'LearnRateSchedule','piecewise',...
'LearnRateDropperiod',100,...
'LearnRateDropFactor',0.2,...
'Verbose',0,...
'plots','training-progres');
%% training lstm network
net=trainNetwork(xtrain,ytrain,layers,options);
%% validation
%% standardize validation data using same parameters of training phase
datateststd=(datatest-mu)/sig;
xtest=datateststd(1:end-1);
ytest=datatest(2:end);
net=predictAndUpdateState(net,xtrain);
[net, ypred] = predictAndUpdateState(net, ytrain(end));
NTSTs=length(xtest);
for i=2:NTSTs
[net, ypred(:,i)] = predictAndUpdateState(net, ytrain(:,i-1));
end
%% unstandarize data
ypred=sig*ypred+mu;
rmse=sqrt(mean(ypred-ytest).^2);
ase=mean(mode(ypred-ytest));
ASME=mode(mean((ypred-ytest)));
%% plotting
it=1:length(ypred);
figure,
hold on
plot(datatrain(1:end-1))
startpnt=NTST;
endpt=NTST+NTSTs;
idx=startpnt:endpt;
plot(idx,[data(startpnt) ypred],'o--')
plot(idx,[data(startpnt) ytest],'+--')
hold off

Respuestas (1)

Rahul
Rahul el 10 de Mzo. de 2025
In order to train LSTM model over multiple time series data using a single script, consider representating all time series data in a single matrix like this:
data = [p1; p2; p3];
% Considering 'p1', 'p2', 'p3' contain the electricity data different cities
numCities = size(data, 1);
trec = size(data, 2);
Now, apply the same logic of training the LSTM as mentioned in the question. The difference would be to apply the logic iteratively using for loops. Here is an example:
nets = cell(1, numCities);
for i = 1:numCities
nets{i} = trainNetwork(squeeze(xtrain(i, :)), squeeze(ytrain(i, :)), layers, options);
end
If the time series data from different cities share similar patterns that can be standardized collectively, then concatenating the data and training can be an effective option.
concatenatedData = data(:)'; % Convert matrix to a single row vector
Note: The concatenated sequence can become very long, which may require adjustments in model architecture or training options to handle effectively.
The following MathWorks documentations can be referred:
Thanks.

Categorías

Más información sobre Deep Learning Toolbox en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by