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"

3 次查看(过去 30 天)
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

回答(2 个)

Ben
Ben 2024-1-5
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 个评论
Muhammad Farhan
Muhammad Farhan 2024-1-9
thank you so much for your valuable answer.
It works but I am getting another error now.
Here is an error window.
Can you also help me to resolve this problem please?
Million of thanks in Advance.
Thanks
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
>> LSTM_model
Training on single CPU.
|========================================================================================|
| Epoch | Iteration | Time Elapsed | Mini-batch | Mini-batch | Base Learning |
| | | (hh:mm:ss) | RMSE | Loss | Rate |
|========================================================================================|
| 1 | 1 | 00:00:00 | 0.05 | 1.3e-03 | 0.0010 |
| 1 | 50 | 00:00:00 | 0.03 | 3.6e-04 | 0.0010 |
| 1 | 67 | 00:00:00 | 0.06 | 1.8e-03 | 0.0010 |
|========================================================================================|
Training finished: Max epochs completed.
Warning: Data sample time is assumed to be 1 seconds. To specify a different value for the data
sample time consider providing data using a timetable or an iddata object.
> In idpack.iodata.extractRefDataFromAnalysisInputList>localDeduceSampling (line 412)
In idpack.iodata.extractRefDataFromAnalysisInputList (line 108)
In idmodel.parseCompareResidPredictInputs (line 66)
In predict (line 83)
In LSTM_model (line 69)
Error using predict
The number of inputs and outputs of the model must match that of the data.
Error in LSTM_model (line 69)
lstmForecastedResiduals = predict(net, X);
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Ben
Ben 2024-1-9
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
Venu 2024-1-7
编辑:Venu 2024-1-7
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!

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by