For binary classification using a neural network, I want to enforce one output node instead of two output nodes

11 views (last 30 days)
I have a dataset consisting of a positive class and a negative class which I want to classifiy using a neural network (e.g. cancerous tumor or benign). I want to set the classification layer to a single node, either this class or that class. During training, MATLAB detects that the data contains two labels, and insists that I put two nodes at the output. How can I enforce one node at the output?
  1 Comment
Z
Z on 22 Sep 2022
I learned that I needed to use a sigmoid instead of a softmax before the output layer. Now the issue is that I cannot use the built-in classification layer, and need to create a custom layer which uses the binary cross-entropy loss.

Sign in to comment.

Answers (1)

Gagan Agarwal
Gagan Agarwal on 30 Aug 2023
Hi Z
In MATLAB, the default assumption is that your dataset contains multiple classes, and as a result, the output layer is automatically configured accordingly with the desired no of nodes to accommodate the corresponding classes.
However, if you specifically want to ensure that the output layer has only one node, you can achieve this by explicitly setting the size of the output layer to 1 using the following command:
net.layers{end}.size = 1;
Here, the variable 'net' represents your neural network.
  1 Comment
Z
Z on 30 Aug 2023
Edited: Z on 30 Aug 2023
Hello Gagan,
This command doesn't work.
For example, if I do
net=googlenet;
net.layers{end}.size = 1;
I get: Unrecognized method, property, or field 'layers' for class 'DAGNetwork'.

Sign in to comment.

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!