ResNet 家族分类层激活函数

ResNet family classification layer activation function

我正在使用 ResNet18 预训练模型,该模型将用于简单的二值图像分类任务。但是,包括 PyTorch 本身在内的所有教程都使用 nn.Linear(num_of_features, classes) 作为最终的全连接层。我不明白的是那个模块的激活函数在哪里?另外,如果我想使用 sigmoid/softmax 怎么办?

提前感谢您的帮助,我是 Pytorch 的新手

通常,最后一层不使用ReLU激活函数。 torch.nn.Linear 层的输出被馈送到交叉熵损失的 softmax 函数,例如,通过使用 torch.nn.CrossEntropyLoss。您可能正在寻找的是二元交叉熵损失 torch.nn.BCELoss.

在您在 Internet 上看到的教程中,人们大多进行多重 class class 化,为此他们使用 cross-entropy loss which doesn't require a user defined activation function at the output. It applies the softmax activation itself (actually applying an activation function before the cross-entropy is one of the most common mistakes in PyTorch). However, in your case you have a binary classification problem, for which you need to use binary cross-entropy loss,不应用任何激活函数本身不同于另一个。因此,您需要自己应用 sigmoid 激活(或任何一种将实数映射到范围 (0, 1) 的激活。

不,如果你的损失函数是CrossEntropyLoss,你不要在最后一层使用激活,因为pytorch CrossEntropyLoss损失结合了nn.LogSoftmax()nn.NLLLoss()在一个class.

他们会那样做吗?

您实际上需要 logits(sigmoid 的输出)来计算损失,因此不将其作为正向传递的一部分是正确的设计。更重要的是预测你不需要 logits 因为 argmax(linear(x)) == argmax(softmax(linear(x))softmax 不会改变顺序但只会改变幅度(压缩函数将任意值转换为 [0,1] 范围,但保留部分排序]

如果您想使用激活函数添加某种非线性,您通常会使用多层 NN 并在最后一层和其他层中使用激活函数。

最后,如果您使用其他损失函数,例如 NLLLossPoissonNLLLossBCELoss,那么您必须自己计算 sigmoid。同样,如果您使用 BCEWithLogitsLoss,则无需再次计算 sigmoid,因为此损失将 Sigmoid 层和 BCELoss 合并为一个 class.

查看 pytorch 文档以了解如何使用损失。