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

查看:35
本文介绍了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 组的概率).在第二种情况下,每张图像只有 1 个输出值,即它的标签(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 激活函数来做分类任务,输出宽度为类别数.这意味着,如果您想将一个对象分为具有标签 ABC 的三个类别,则需要使Dense 层生成形状为 (None, 3) 的输出.然后就可以使用cross_entropyloss函数计算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 任务.您正在使用输出的值来表示类别.答案是正确的,但不像 classification 任务的一般解决方案那样执行.

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天全站免登陆