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?

 채택된 답변

Gayathri
Gayathri 2024년 11월 4일
편집: Gayathri 2024년 11월 4일

0 개 추천

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.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 AI for Signals에 대해 자세히 알아보기

제품

릴리스

R2024a

질문:

2024년 11월 3일

댓글:

2024년 11월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by