Dense(2)和Dense(1)之间的区别是二进制分类CNN的最后一层? [英] Difference between Dense(2) and Dense(1) as the final layer of a binary classification CNN?

查看:2868
本文介绍了Dense(2)和Dense(1)之间的区别是二进制分类CNN的最后一层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于图像的二进制分类的CNN中,输出的形状应该是(图像数量1)还是(图像数量2)?具体来说,这是CNN中的2种最后一层:

In a CNN for binary classification of images, should the shape of output be (number of images, 1) or (number of images, 2)? Specifically, here are 2 kinds of last layer in a CNN:

keras.layers.Dense(2, activation = 'softmax')(previousLayer)

keras.layers.Dense(1, activation = 'softmax')(previousLayer)

在第一种情况下,每个图像都有2个输出值(属于组1的概率和属于组2的概率)。在第二种情况下,每个图像只有一个输出值,即它的标签(0或1,label = 1表示它属于组1)。

In the first case, for every image there are 2 output values (probability of belonging to group 1 and probability of belonging to group 2). In the second case, each image has only 1 output value, which is its label (0 or 1, label=1 means it belongs to group 1).

其中一个是正确的?有内在的区别吗?我不想识别这些图像中的任何对象,只需将它们分成2组。

Which one is correct? Is there intrinsic difference? I don't want to recognize any object in those images, just divide them into 2 groups.

非常感谢!

推荐答案

第一个是正确的解决方案:

This first one is the correct solution:

keras.layers.Dense(2, activation = 'softmax')(previousLayer)

通常,我们使用 softmax 激活函数执行分类任务,并且输出宽度将为分类的数量。这意味着如果要将一个对象分为三个类别,它们的标签为 A B C ,则需要使 Dense 层生成形状为的输出(无,3 )。然后,您可以使用 cross_entropy 损失函数来计算 LOSS ,自动计算梯度并进行反向传播处理。

Usually, we use the softmax activation function to do classification tasks, and the output width will be the number of the categories. This means that if you want to classify one object into three categories with the labels A,B, or C, you would need to make the Dense layer generate an output with a shape of (None, 3). Then you can use the cross_entropyloss function to calculate the LOSS, automatically calculate the gradient, and do the back-propagation process.

如果只想在 Dense 层中生成一个值,则意味着您会得到一个具有张量的张量(None,1)的值-因此它会生成单个数值,例如 regression 任务。您正在使用输出的值来表示类别。答案是正确的,但表现却不如分类任务的常规解决方案。

If you want to only generate one value with the Dense layer, that means you get a tensor with a shape of (None, 1) - so it produces a single numeric value, like a regression task. You are using the value of the output to represent the category. The answer is correct, but does not perform like the general solution of the classification task.

这篇关于Dense(2)和Dense(1)之间的区别是二进制分类CNN的最后一层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆