I am modeling Hybrid model for load forecasting. I have ran the HW and FOA part but when I merge LSTM then I am getting error of "TrainNetwork"
6 views (last 30 days)
Show older comments
I have four years of Electricity data from a city for load forecasting and I am unable to train my LSTM network and getting the error again and again.
My LSTM modeling is as follow:
% Prepare Data for LSTM
% Calculate Residuals
residuals = actual_data_2021 - forecasted_values_2021;
% Ensure residuals is a column vector
residuals = residuals(:);
% Prepare Data for LSTM
sequence_length = 24; % Number of time steps
num_samples = length(residuals) - sequence_length;
X = zeros(num_samples, sequence_length); % Preallocate for efficiency
Y = zeros(num_samples, 1); % Output for LSTM
for i = 1:num_samples
X(i, :) = residuals(i:i+sequence_length-1); % Last 24 hours data
Y(i) = residuals(i + sequence_length); % Next hour data
end
% Alternative Approach: Using `permute` for Reshaping
X = permute(X, [1, 2, 3]);
% Check dimensions of X and Y
disp(['Size of X: ', mat2str(size(X))]);
disp(['Size of Y: ', mat2str(size(Y))]);
% Define LSTM Network Architecture
numFeatures = 1; % Number of features (univariate time series)
numHiddenUnits = 50; % Number of hidden units in LSTM layer
numResponses = 1; % Number of responses (single value prediction)
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
% Training Options
options = trainingOptions('adam', ...
'MaxEpochs',100, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',50, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
% Train LSTM
net = trainNetwork(X, Y, layers, options);
and I am getting the following error again and again.
LSTM_model
Size of X: [8688 24]
Size of Y: [8688 1]
Error using trainNetwork
The training sequences are of feature dimension 8688
but the input layer expects sequences of feature
dimension 1.
Error in LSTM_model (line 69)
net = trainNetwork(X, Y, layers, options);
Can anyone suggest me how to resolve this problem please?
I would highly appreacite and if needed I can pay for it.
Email: Farhanee.suit@gmail.com
Thanks
0 Comments
Answers (2)
Ben
on 5 Jan 2024
When you have multiple time-series observations you need to put the data into cell arrays. This is because each time-series can potentially be a different sequence length in general. Here's an example with the sizes you have:
% Set up fake data with same sizes
num_samples = 8688;
sequence_length = 24;
X = zeros(num_samples,sequence_length);
Y = zeros(num_samples,1);
% Convert to cell array
Xc = num2cell(X,2);
Note that now Xc is a cell array, and there are num_samples entries in Xc, and Xc{i} has size 1 x sequence_length.
Note that the way your data seems to be set up is a sequence-to-one task, so the target data Y aren't sequences. In this case you need a model that outputs non-sequence data, e.g. by specifying OutputMode="last" in lstmLayer. Here's some dummy code to show training can work:
net = [sequenceInputLayer(1);lstmLayer(1,OutputMode="last");regressionLayer];
opts = trainingOptions("adam",MaxEpochs=1);
trainednet = trainNetwork(Xc,Y,net,opts);
2 Comments
Ben
on 9 Jan 2024
Unfortunately I'm not familiar with the functions here where the error is occuring such as idpack.iodata.extractRefDataFromAnalysisInputList>localDeduceSampling
Do you have custom layers that call these functions?
Venu
on 7 Jan 2024
Edited: Venu
on 7 Jan 2024
The 'permute' function is used to rearrange the dimensions of an array without changing the total number of dimensions.
In your code:
X = permute(X, [1, 2, 3]);
If X were initially a 2D array, 'permute' would not add a third dimension; it would only rearrange existing dimensions if you specified a different order. You need to reshape it to a 3D array with the second dimension being 1 (since you have a univariate time series).
X = reshape(X, [sequence_length, 1, num_samples]);
Hope this helps!
0 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!