i have data.mat file and I am working on load forecasting for solar power with CNN, I am facing issue in data preparation.

9 次查看(过去 30 天)
i have data.mat file and I am working on load forecasting for solar power. there are 23 features in my array with each feature has sample size of 4348. This data file is represented as 4348*24, where 24 th column is represented as output. i am want to write CNN code for it. This is my code but I am receving this error.
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.
Error in CNN (line 15)
X_reshaped = reshape(X_padded, [num_padded_rows/144, 8, 3, 3, ceil(num_padded_cols/72)]);
% Load data
load('data.mat')
% Extract features and labels
X = data(:, 1:end-1);
Y = data(:, end);
Y_str = cellstr(num2str(Y));
categories = unique(Y);
Y = categorical(Y_str, unique(Y_str));
[num_samples, num_features] = size(X);
num_padded_rows = ceil(num_samples/144)*144;
num_padded_cols = ceil(num_features/72)*72;
X_padded = [X, zeros(num_samples, num_padded_cols - num_features)];
X_reshaped = reshape(X_padded, [num_padded_rows/144, 8, 3, 3, ceil(num_padded_cols/72)]);
% Define CNN layers
layers = [ imageInputLayer([8, 3, 3, 2])
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,64,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(length(categories))
softmaxLayer
classificationLayer];
% Define training options
options = trainingOptions('adam', ...
'InitialLearnRate',1e-4, ...
'MaxEpochs',20, ...
'MiniBatchSize',32, ...
'Shuffle','every-epoch', ...
'Verbose',false, ...
'Plots','training-progress');
% Train the CNN
net = trainNetwork(X_reshaped, Y, layers, options);

回答(1 个)

Govind KM
Govind KM 2024-9-24,10:16
The reshape function requires the total number of elements in the input array to be preserved in the output array. Hence, the product of the specified dimensions must equal the total number of elements in the input.
In the provided code:
X = rand(4348,23); %Sample data with same size
[num_samples, num_features] = size(X);
num_padded_rows = ceil(num_samples/144)*144;
num_padded_cols = ceil(num_features/72)*72;
X_padded = [X, zeros(num_samples, num_padded_cols - num_features)];
numel(X_padded)
ans = 313056
%X_reshaped = reshape(X_padded, [num_padded_rows/144, 8, 3, 3, ceil(num_padded_cols/72)]);
prod([num_padded_rows/144, 8, 3, 3, ceil(num_padded_cols/72)])
ans = 2232
The total number of elements are unequal, leading to an error.
One way to resolve this is to pass "[]" as the first dimension to the reshape function, maintaining the remaining dimensions as [8,3,3,2] for the CNN layer. This lets the function calculate the necessary size automatically, ensuring the element count remains unchanged.
X_reshaped = reshape(X_padded, [], 8, 3, 3, 2);
size(X_reshaped)
ans = 1×5
2174 8 3 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
More details on the reshape function can be found in the following documentation:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Sequence and Numeric Feature Data Workflows 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by