I am Designging a multi-ouput ANN for regression and classification simultaneously but i face error
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Kaustab Pal
2024-8-21
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!