Invalid training data Predictors must be a N-by-1 cell array of sequences
% Reshape training data for LSTM input and output
num_features = size(X_train, 2);
num_samples_per_day = 24; % Number of hours in a day
num_days_train = size(X_train, 1) / num_samples_per_day;
X_train_reshaped = cell(num_days_train, 1);
y_train_reshaped = cell(num_days_train, 1);
for a = 1:num_days_train
start_idx = (a – 1) * num_samples_per_day + 1;
end_idx = a * num_samples_per_day;
% Extract data for the current day
current_day_data = X_train(start_idx:end_idx, :);
current_day_target = y_train(start_idx:end_idx);
% Reshape data into a cell array of matrices [num_features x num_samples_per_day]
reshaped_data = num2cell(current_day_data’, 1);
X_train_reshaped{a} = reshaped_data;
% Reshape target values into a cell array of column vectors
reshaped_target = reshape(current_day_target, [], 1);
y_train_reshaped{a} = reshaped_target;
end
% Reshape validation data for LSTM input
num_days_val = size(X_val, 1) / num_samples_per_day;
X_val_reshaped = cell(num_days_val, 1);
y_val_reshaped = cell(num_days_val, 1);
for b = 1:num_days_val
start_idx = (b – 1) * num_samples_per_day + 1;
end_idx = b * num_samples_per_day;
% Extract data for the current day
current_day_data = X_val(start_idx:end_idx, :);
current_day_target = y_val(start_idx:end_idx);
% Reshape data into a matrix [num_features x num_samples_per_day]
reshaped_data = current_day_data’;
% Reshape target values into a column vector
reshaped_target = current_day_target’;
% Store reshaped data and target for the current day
X_val_reshaped{b} = reshaped_data;
y_val_reshaped{b} = reshaped_target;
end
% Reshape test data for LSTM input
num_days_test = size(X_test, 1) / num_samples_per_day;
X_test_reshaped = cell(num_days_test, 1);
for c = 1:num_days_test
start_idx = (c – 1) * num_samples_per_day + 1;
end_idx = c * num_samples_per_day;
% Extract data for the current day
current_day_data = X_test(start_idx:end_idx, :);
% Reshape data into a matrix [num_features x num_samples_per_day]
reshaped_data = current_day_data’;
% Store reshaped data for the current day
X_test_reshaped{c} = reshaped_data;
end
% Define hyperparameters for grid search
hidden_layers = [1, 2, 3];
num_hidden_units_range = [24, 48, 96, 192];
dropout_rates = [0.1, 0.2, 0.3, 0.4, 0.5];
learning_rates = [0.05, 0.01, 0.005, 0.001, 0.0005];
optimization_solvers_list = {‘adam’, ‘sgdm’, ‘rmsprop’};
num_epochs_range = [50, 100, 150, 200, 250];
% Build LSTM model
layers = [
sequenceInputLayer(num_features)
];
for m = 1:num_layers
layers = [
layers
lstmLayer(num_units, ‘OutputMode’, ‘sequence’)
dropoutLayer(dropout_rate)
];
end
% Connect the last LSTM layer to a fully connected layer
layers = [
layers
fullyConnectedLayer(num_units)
dropoutLayer(dropout_rate)
fullyConnectedLayer(24) % Output layer with 24 units for 24-hour GHI forecasting
regressionLayer % Output layer for regression tasks
];
% Set options for training
options = trainingOptions(optimizer, …
‘MaxEpochs’, num_epochs, …
‘MiniBatchSize’, 64, …
‘InitialLearnRate’, learning_rate, …
‘LearnRateSchedule’, ‘piecewise’, …
‘LearnRateDropPeriod’, 50, …
‘LearnRateDropFactor’, 0.1, …
‘ValidationData’, {X_val_reshaped, y_val_reshaped}, …
‘ValidationFrequency’, 10, …
‘ValidationPatience’, 20, …
‘Verbose’, 0, …
‘Plots’, ‘training-progress’);
% Train the LSTM model
lstm_model = trainNetwork(X_train_reshaped, y_train_reshaped, layers, options);
% Predict GHI using the trained model on validation data
y_pred_val = predict(lstm_model, X_val_reshaped);
% Denormalize predictions
y_pred_denormalized = y_pred_val * (max(features.GHI) – min(features.GHI)) + min(features.GHI);
% Calculate evaluation metrics
rmse_val = calculate_rmse(y_val, y_pred_val_denormalized);
mae_val = calculate_mae(y_val, y_pred_val_denormalized);
end
Error using trainNetwork (line 184)
Invalid training data. Predictors must be a N-by-1 cell array of sequences, where N is the number of sequences. All
sequences must have the same feature dimension and at least one time step.% Reshape training data for LSTM input and output
num_features = size(X_train, 2);
num_samples_per_day = 24; % Number of hours in a day
num_days_train = size(X_train, 1) / num_samples_per_day;
X_train_reshaped = cell(num_days_train, 1);
y_train_reshaped = cell(num_days_train, 1);
for a = 1:num_days_train
start_idx = (a – 1) * num_samples_per_day + 1;
end_idx = a * num_samples_per_day;
% Extract data for the current day
current_day_data = X_train(start_idx:end_idx, :);
current_day_target = y_train(start_idx:end_idx);
% Reshape data into a cell array of matrices [num_features x num_samples_per_day]
reshaped_data = num2cell(current_day_data’, 1);
X_train_reshaped{a} = reshaped_data;
% Reshape target values into a cell array of column vectors
reshaped_target = reshape(current_day_target, [], 1);
y_train_reshaped{a} = reshaped_target;
end
% Reshape validation data for LSTM input
num_days_val = size(X_val, 1) / num_samples_per_day;
X_val_reshaped = cell(num_days_val, 1);
y_val_reshaped = cell(num_days_val, 1);
for b = 1:num_days_val
start_idx = (b – 1) * num_samples_per_day + 1;
end_idx = b * num_samples_per_day;
% Extract data for the current day
current_day_data = X_val(start_idx:end_idx, :);
current_day_target = y_val(start_idx:end_idx);
% Reshape data into a matrix [num_features x num_samples_per_day]
reshaped_data = current_day_data’;
% Reshape target values into a column vector
reshaped_target = current_day_target’;
% Store reshaped data and target for the current day
X_val_reshaped{b} = reshaped_data;
y_val_reshaped{b} = reshaped_target;
end
% Reshape test data for LSTM input
num_days_test = size(X_test, 1) / num_samples_per_day;
X_test_reshaped = cell(num_days_test, 1);
for c = 1:num_days_test
start_idx = (c – 1) * num_samples_per_day + 1;
end_idx = c * num_samples_per_day;
% Extract data for the current day
current_day_data = X_test(start_idx:end_idx, :);
% Reshape data into a matrix [num_features x num_samples_per_day]
reshaped_data = current_day_data’;
% Store reshaped data for the current day
X_test_reshaped{c} = reshaped_data;
end
% Define hyperparameters for grid search
hidden_layers = [1, 2, 3];
num_hidden_units_range = [24, 48, 96, 192];
dropout_rates = [0.1, 0.2, 0.3, 0.4, 0.5];
learning_rates = [0.05, 0.01, 0.005, 0.001, 0.0005];
optimization_solvers_list = {‘adam’, ‘sgdm’, ‘rmsprop’};
num_epochs_range = [50, 100, 150, 200, 250];
% Build LSTM model
layers = [
sequenceInputLayer(num_features)
];
for m = 1:num_layers
layers = [
layers
lstmLayer(num_units, ‘OutputMode’, ‘sequence’)
dropoutLayer(dropout_rate)
];
end
% Connect the last LSTM layer to a fully connected layer
layers = [
layers
fullyConnectedLayer(num_units)
dropoutLayer(dropout_rate)
fullyConnectedLayer(24) % Output layer with 24 units for 24-hour GHI forecasting
regressionLayer % Output layer for regression tasks
];
% Set options for training
options = trainingOptions(optimizer, …
‘MaxEpochs’, num_epochs, …
‘MiniBatchSize’, 64, …
‘InitialLearnRate’, learning_rate, …
‘LearnRateSchedule’, ‘piecewise’, …
‘LearnRateDropPeriod’, 50, …
‘LearnRateDropFactor’, 0.1, …
‘ValidationData’, {X_val_reshaped, y_val_reshaped}, …
‘ValidationFrequency’, 10, …
‘ValidationPatience’, 20, …
‘Verbose’, 0, …
‘Plots’, ‘training-progress’);
% Train the LSTM model
lstm_model = trainNetwork(X_train_reshaped, y_train_reshaped, layers, options);
% Predict GHI using the trained model on validation data
y_pred_val = predict(lstm_model, X_val_reshaped);
% Denormalize predictions
y_pred_denormalized = y_pred_val * (max(features.GHI) – min(features.GHI)) + min(features.GHI);
% Calculate evaluation metrics
rmse_val = calculate_rmse(y_val, y_pred_val_denormalized);
mae_val = calculate_mae(y_val, y_pred_val_denormalized);
end
Error using trainNetwork (line 184)
Invalid training data. Predictors must be a N-by-1 cell array of sequences, where N is the number of sequences. All
sequences must have the same feature dimension and at least one time step. % Reshape training data for LSTM input and output
num_features = size(X_train, 2);
num_samples_per_day = 24; % Number of hours in a day
num_days_train = size(X_train, 1) / num_samples_per_day;
X_train_reshaped = cell(num_days_train, 1);
y_train_reshaped = cell(num_days_train, 1);
for a = 1:num_days_train
start_idx = (a – 1) * num_samples_per_day + 1;
end_idx = a * num_samples_per_day;
% Extract data for the current day
current_day_data = X_train(start_idx:end_idx, :);
current_day_target = y_train(start_idx:end_idx);
% Reshape data into a cell array of matrices [num_features x num_samples_per_day]
reshaped_data = num2cell(current_day_data’, 1);
X_train_reshaped{a} = reshaped_data;
% Reshape target values into a cell array of column vectors
reshaped_target = reshape(current_day_target, [], 1);
y_train_reshaped{a} = reshaped_target;
end
% Reshape validation data for LSTM input
num_days_val = size(X_val, 1) / num_samples_per_day;
X_val_reshaped = cell(num_days_val, 1);
y_val_reshaped = cell(num_days_val, 1);
for b = 1:num_days_val
start_idx = (b – 1) * num_samples_per_day + 1;
end_idx = b * num_samples_per_day;
% Extract data for the current day
current_day_data = X_val(start_idx:end_idx, :);
current_day_target = y_val(start_idx:end_idx);
% Reshape data into a matrix [num_features x num_samples_per_day]
reshaped_data = current_day_data’;
% Reshape target values into a column vector
reshaped_target = current_day_target’;
% Store reshaped data and target for the current day
X_val_reshaped{b} = reshaped_data;
y_val_reshaped{b} = reshaped_target;
end
% Reshape test data for LSTM input
num_days_test = size(X_test, 1) / num_samples_per_day;
X_test_reshaped = cell(num_days_test, 1);
for c = 1:num_days_test
start_idx = (c – 1) * num_samples_per_day + 1;
end_idx = c * num_samples_per_day;
% Extract data for the current day
current_day_data = X_test(start_idx:end_idx, :);
% Reshape data into a matrix [num_features x num_samples_per_day]
reshaped_data = current_day_data’;
% Store reshaped data for the current day
X_test_reshaped{c} = reshaped_data;
end
% Define hyperparameters for grid search
hidden_layers = [1, 2, 3];
num_hidden_units_range = [24, 48, 96, 192];
dropout_rates = [0.1, 0.2, 0.3, 0.4, 0.5];
learning_rates = [0.05, 0.01, 0.005, 0.001, 0.0005];
optimization_solvers_list = {‘adam’, ‘sgdm’, ‘rmsprop’};
num_epochs_range = [50, 100, 150, 200, 250];
% Build LSTM model
layers = [
sequenceInputLayer(num_features)
];
for m = 1:num_layers
layers = [
layers
lstmLayer(num_units, ‘OutputMode’, ‘sequence’)
dropoutLayer(dropout_rate)
];
end
% Connect the last LSTM layer to a fully connected layer
layers = [
layers
fullyConnectedLayer(num_units)
dropoutLayer(dropout_rate)
fullyConnectedLayer(24) % Output layer with 24 units for 24-hour GHI forecasting
regressionLayer % Output layer for regression tasks
];
% Set options for training
options = trainingOptions(optimizer, …
‘MaxEpochs’, num_epochs, …
‘MiniBatchSize’, 64, …
‘InitialLearnRate’, learning_rate, …
‘LearnRateSchedule’, ‘piecewise’, …
‘LearnRateDropPeriod’, 50, …
‘LearnRateDropFactor’, 0.1, …
‘ValidationData’, {X_val_reshaped, y_val_reshaped}, …
‘ValidationFrequency’, 10, …
‘ValidationPatience’, 20, …
‘Verbose’, 0, …
‘Plots’, ‘training-progress’);
% Train the LSTM model
lstm_model = trainNetwork(X_train_reshaped, y_train_reshaped, layers, options);
% Predict GHI using the trained model on validation data
y_pred_val = predict(lstm_model, X_val_reshaped);
% Denormalize predictions
y_pred_denormalized = y_pred_val * (max(features.GHI) – min(features.GHI)) + min(features.GHI);
% Calculate evaluation metrics
rmse_val = calculate_rmse(y_val, y_pred_val_denormalized);
mae_val = calculate_mae(y_val, y_pred_val_denormalized);
end
Error using trainNetwork (line 184)
Invalid training data. Predictors must be a N-by-1 cell array of sequences, where N is the number of sequences. All
sequences must have the same feature dimension and at least one time step. hyperparameters fine-tune MATLAB Answers — New Questions