LSTM Input and Output Dimension Problem

24 vues (au cours des 30 derniers jours)
Justin
Justin le 3 Nov 2024
Commenté : Justin le 6 Nov 2024
I have a univariate time series for forecasting using LSTM and I split it into 9600 samples for training. For each training sample, the input is a sequence of 20 datapoints, and the output is the next datapoint in time (i.e., using the previous 20 datapoints to predict the next point in time).
My LSTM setup code is like this:
numChannels = 1;
numResponses = 1;
numHiddenUnits = 128;
layers = [
sequenceInputLayer(numChannels)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
];
However, MATLAB gives an error saying
Size of predictions and targets must match.
Size of predictions:
1(C) × 128(B) × 20(T)
Size of targets:
1(C) × 128(B) × 1(T)
Then I am not sure how to shape my samples in a way allowed by MATLAB?

Réponse acceptée

Gayathri
Gayathri le 4 Nov 2024
Modifié(e) : Gayathri le 4 Nov 2024
Hello @Justin,
I understand that you have to train a LSTM network for forcasting univariate time series. I created a sample input data based on the sizes mentioned in the question. I was not able to reproduce the error as I do not have access to the complete code for training. Please refer to the below code for training a LSTM network for time series forcasting. I have specified the 'OutputMode' to be 'last' as we only need the prediction for the final time step of the sequence.
% Sample data generation (replace with your actual data)
data = sin(0:0.01:100)'; % Example time series data
% Parameters
numTimeSteps = 20; % Number of timesteps in each input sequence
numSamples = 9600; % Total number of samples
numChannels = 1; % Univariate data (one feature per time step)
numResponses = 1; % Single output prediction
numHiddenUnits = 128; % Number of hidden units in LSTM
% Prepare input sequences and targets
X = cell(numSamples, 1);
Y = zeros(numSamples, 1);
for i = 1:numSamples
X{i} = data(i:i+numTimeSteps-1)'; % Each cell contains a 1x20 sequence
Y(i) = data(i+numTimeSteps); % Each target is a scalar
end
% Define LSTM network architecture
layers = [
sequenceInputLayer(numChannels, 'Name', 'input')
lstmLayer(numHiddenUnits, 'OutputMode', 'last', 'Name', 'lstm')
fullyConnectedLayer(numResponses, 'Name', 'fc')
regressionLayer('Name', 'output')
];
% Training options
options = trainingOptions('adam', ...
'MaxEpochs', 5, ...
'GradientThreshold', 1, ...
'InitialLearnRate', 0.01, ...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.2, ...
'LearnRateDropPeriod', 10, ...
'Verbose', 0, ...
'Plots', 'training-progress');
% Train the network
net = trainNetwork(X, Y, layers, options);
For more information on "lstmLayer" please refer to the below mentioned link.
Also, for more information about LSTM, please refer to the below link.
Hope you find this information helpul.
  1 commentaire
Justin
Justin le 6 Nov 2024
It works. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur AI for Signals dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by