Clear Filters
Clear Filters

While training CNN, I am getting "Error: Invalid training data". How do I rectify this?

1 view (last 30 days)
I am trying for solar power prediction using cnn. It has 6 input features and 1 output. I have tried the following code. But i got the error during training as "Invalid training data. The output size ([1 1 1]) of the last layer does not match the response size ([1 1 1773])".
%% load data
% XTraining & YTraining
input_data = xlsread("forecast_data.xlsx", 1);
len_train = floor(0.8*length(input_data));
XTrain = input_data(1:len_train,1:6);
YTrain = input_data(1:len_train,7);
XTrainReshaped = reshape(XTrain, [len_train 6 1 1]);
YTrainReshaped = reshape(YTrain, [len_train 1 1 1]);
% create convolutional network
Conv1 = convolution2dLayer([1 1], 10, 'NumChannels',1, 'Stride',[1,1], 'Padding',[1 1 1 1], WeightLearnRateFactor=1, ...
BiasLearnRateFactor=1, name='Conv1')
Conv1.Weights = sqrt(2/(9*10)) * randn(1,1,1,10,'single');
Conv1.Bias = randn(1, 1, 10, 'single');
% construct layer
input_data2 = [imageInputLayer([6 1 1])]
layers = [input_data2; convolution2dLayer(1,32); reluLayer; ...
convolution2dLayer(1,64); reluLayer; convolution2dLayer(1,128); reluLayer; dropoutLayer(0.2); ...
fullyConnectedLayer(1); softmaxLayer; regressionLayer];
% training option
opts = trainingOptions('adam', 'MaxEpochs',10, 'InitialLearnRate',0.0001, ...
'GradientThreshold',0.1)
net = trainNetwork(XTrainReshaped', YTrainReshaped', layers, opts);
sample data is attached here. Please help me to rectify this error.
Thank you in advance.

Answers (1)

Aditya Srikar
Aditya Srikar on 28 Apr 2023
Hi Muhammad
In general, you cannot analytically calculate the number of layers or the number of nodes to use per layer in an artificial neural network to address a specific real-world problem. The number of layers and the number of nodes in each layer are model hyperparameters that you must specify.
The simplest method to choose number of neurons is probably to apply a k-fold cross validation.
You can design an automated search to test different network configurations. Some popular search strategies include:
  • Random: Try random configurations of layers and nodes per layer.
  • Grid: Try a systematic search across the number of layers and nodes per layer.
  • Heuristic: Try a directed search across configurations such as a genetic algorithm or Bayesian optimization.
  • Exhaustive: Try all combinations of layers and the number of nodes; it might be feasible for small networks and datasets
You can also determine the topology of the neural network using principal component analysis (PCA) and K-means clustering. PCA is used to determine the hidden layers number, while clustering is used to determine the hidden neurons number for corresponding hidden layer. The selected topology is then used for prediction. Then the performance of topology determination using PCA and clustering is then compared with several other methods.
Hope it helps !

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!