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

2 次查看(过去 30 天)
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 个评论
Z
Z 2022-9-22
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.

请先登录,再进行评论。

回答(1 个)

Gagan Agarwal
Gagan Agarwal 2023-8-30
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 个评论
Z
Z 2023-8-30
编辑:Z 2023-8-30
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'.

请先登录,再进行评论。

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by