I am Designging a multi-ouput ANN for regression and classification simultaneously but i face error

1 view (last 30 days)
clc;clear all;
%% Read file
filename = "CorrectDS.csv";
tbl = readtable(filename,'TextType','String');
head(tbl)
X=table2array(tbl);
training=X(:,1:23);
classLabel=categorical(X(:,24));
regLabel=X(:,25);
num_features = size(X, 2)-2;
num_classes = 2;
num_neurons_hidden = 20;
% Define the neural network architecture
layers = [
featureInputLayer(num_features, 'Normalization', 'none')
fullyConnectedLayer(num_neurons_hidden)
reluLayer
fullyConnectedLayer(num_neurons_hidden)
reluLayer
fullyConnectedLayer(num_neurons_hidden) % Additional hidden layer for classification
reluLayer
fullyConnectedLayer(num_classes) % Classification output
softmaxLayer
fullyConnectedLayer(1) % Regression output
regressionLayer
];
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'MiniBatchSize', 16, ...
'Verbose', true, ...
'Plots', 'training-progress');
lab={classLabel,regLabel};
net = trainNetwork(training,lab,layers, options);
and I got this error
Error using trainNetwork
Invalid training data. For classification tasks, responses must be a vector of categorical responses. For regression tasks, responses must be a vector, a matrix,
or a 4-D array of numeric responses which must not contain NaNs.
Error in MAin (line 40)
net = trainNetwork(training,lab,layers, options);
Please, Someone help me to tackle this error
  8 Comments

Sign in to comment.

Answers (2)

Kaustab Pal
Kaustab Pal on 21 Aug 2024
To create a Neural Network capable of both classification and regression, you should design the network with two distinct branches: one dedicated to classification and the other to regression. You can utiize the "dlnetwork" function to achieve this. Below is a sample code to illustrate how you can implement this:
num_classes = 3;
num_neurons_hidden = 20;
% Define the neural network architecture
net = dlnetwork();
tmp_layers = [
featureInputLayer(10, 'Normalization', 'none', "Name","input_layer")
fullyConnectedLayer(num_neurons_hidden,"Name","fc1")
reluLayer("Name","relu_1")
fullyConnectedLayer(num_neurons_hidden, "Name","fc2")
reluLayer("Name","relu_2")
fullyConnectedLayer(num_neurons_hidden, "Name","fc3") % Additional hidden layer for classification
reluLayer("Name","relu_3")
fullyConnectedLayer(num_classes, "Name","fc4") % Classification output
softmaxLayer("Name","classifier_output")
];
net = addLayers(net,tmp_layers);
tempLayers = [
fullyConnectedLayer(1, "Name","regression_output")
];
net = addLayers(net,tempLayers);
net = connectLayers(net,"fc4","regression_output");
analyzeNetwork(net)
The output of the analyzeNetwork will look something like this:
You can now incorporate the appropriate loss functions for each branch to train the network on your dataset effectively.
To know more about dlnetwork, you can refer the official documentation here: https://www.mathworks.com/help/deeplearning/ref/dlnetwork.html
I hope this helps!

Ameer HAmza
Ameer HAmza on 21 Aug 2024
Thank you brother

Community Treasure Hunt

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

Start Hunting!