Input Size Mismatch on LSTM model development

7 vues (au cours des 30 derniers jours)
Babajide Osonuga
Babajide Osonuga le 1 Mar 2024
Good day,
Given that the input to the LSTM has three dimensions: num_features, num_time_steps as 24, and num_training_samples. i intend to predict a 24 hours day-ahead GHI. X_train_3D = 4xs4x1461; X_test_3D = 4x24x365; y_train_reshaped = 24x1x1461.
Below are the codes:
best_rmse = inf;
best_mae = inf;
best_r2 = -inf;;
best_hyperparameters = [];;
% Perform grid search
for optimizer_val = optimizers
for num_lstm_layers_val = num_lstm_layers
for num_hidden_layers_val = num_hidden_layers
for hidden_units_val = hidden_units
for learning_rate_val = learning_rates
for num_epochs_val = num_epochs
for dropout_rate_val = dropout_rates
for batch_size_val = batch_sizes
% Build LSTM model
layers = [ ...
sequenceInputLayer([num_features num_time_steps 1],'Name', 'input')
];
for i = 1:num_lstm_layers_val
layers = [layers
lstmLayer(hidden_units_val,'OutputMode','sequence')
dropoutLayer(dropout_rate_val)
];
end
layers = [layers
fullyConnectedLayer(num_time_steps)
regressionLayer];
% Specify options for training
options = trainingOptions(optimizer_val{1}, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',10, ...
'LearnRateDropFactor',0.9, ...
'InitialLearnRate',learning_rate_val, ...
'MaxEpochs',num_epochs_val, ...
'MiniBatchSize',batch_size_val, ...
'SequenceLength','longest', ...
'Shuffle','never', ...
'Verbose',0, ...
'Plots','training-progress');
% Train LSTM model
net = trainNetwork(X_train_3D, y_train_3D, layers, options);
% Make predictions on test set
y_pred = predict(net, X_test_3D);
% Denormalize predicted GHI values
y_pred_denormalized = y_pred * range(features.GHI) + min(features.GHI);
% Compute evaluation metrics
rmse = sqrt(mean((y_test - y_pred_denormalized).^2));
mae = mean(abs(y_test - y_pred_denormalized));
r2 = 1 - sum((y_test - y_pred_denormalized).^2) / sum((y_test - mean(y_test)).^2);
% Update best hyperparameters if metrics improve
if rmse < best_rmse
best_rmse = rmse;
best_mae = mae;
best_r2 = r2;
best_hyperparameters = [optimizer_val{1}, num_lstm_layers_val, num_hidden_layers_val, hidden_units_val, learning_rate_val, num_epochs_val, dropout_rate_val, batch_size_val];
end
end
end
end
end
end
end
end
end
Error using trainNetwork (line 184)
Invalid network.
Caused by:
Layer 2: Input size mismatch. LSTM layers must have scalar input size, but input size (4×24×1) was received. Try
using a flatten layer before the LSTM layer.
when i first used sequenceInputLayer([num_features num_time_steps],'Name', 'input'), it displayed
Error using sequenceInputLayer (line 131)
Invalid argument at position 1. Expected input to be a scalar, 3 element vector or 4 element vector.'
then I included flattenedLayer before the LSTM layer, it displayed
;Error using trainNetwork (line 184)
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size
for that dimension.
Caused by:
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate
size for that dimension.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 2 Mar 2024
The input to the lstmLayer is the number of features (a scaler), not the size of the input.

Plus de réponses (0)

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by