LSTM Input and Output Dimension Problem
51 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Gayathri
2024-11-4,3:24
编辑:Gayathri
2024-11-4,4:42
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sequence and Numeric Feature Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!