deep-learning - 如何计算 PyTorch 中的自举交叉熵损失?

标签 deep-learning neural-network pytorch loss-function

我读过一些论文,它们使用称为“Bootstrapped Cross Entropy Loss”的东西来训练他们的分割网络。这个想法是只关注最难的 k%(比如 15%)的像素来提高学习性能,尤其是当简单的像素占主导地位时。
目前,我正在使用标准交叉熵:

loss = F.binary_cross_entropy(mask, gt)
如何在 PyTorch 中有效地将其转换为引导版本?

最佳答案

通常我们还会为损失添加一个“热身”期,这样网络就可以学习首先适应容易的区域,然后过渡到较难的区域。
此实现从 k=100 开始并继续 20000 次迭代,然后线性衰减到 k=15再进行 50000 次迭代。

class BootstrappedCE(nn.Module):
    def __init__(self, start_warm=20000, end_warm=70000, top_p=0.15):
        super().__init__()

        self.start_warm = start_warm
        self.end_warm = end_warm
        self.top_p = top_p

    def forward(self, input, target, it):
        if it < self.start_warm:
            return F.cross_entropy(input, target), 1.0

        raw_loss = F.cross_entropy(input, target, reduction='none').view(-1)
        num_pixels = raw_loss.numel()

        if it > self.end_warm:
            this_p = self.top_p
        else:
            this_p = self.top_p + (1-self.top_p)*((self.end_warm-it)/(self.end_warm-self.start_warm))
        loss, _ = torch.topk(raw_loss, int(num_pixels * this_p), sorted=False)
        return loss.mean(), this_p

关于deep-learning - 如何计算 PyTorch 中的自举交叉熵损失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63735255/

相关文章:

algorithm - 元素拾取竞赛的AI算法

parallel-processing - 如何在pytorch中进行并行处理

python - Torch 在 PyTorch 上分配零 GPU 内存

machine-learning - 在 GPU 上运行 TensorFlow Textsum 模型

python - 恢复训练好的 tensorflow 模型,编辑与节点关联的值,然后保存

python - 为什么我的简单NN在Tensorflow中的损失是nan?

python - Keras 一个操作在 train_on_batch 时对梯度有 None

python - 怎么把Keras的后台换成Theano?

python - 属性错误: 'Series' object has no attribute 'label'

pytorch - 预训练变压器模型的配置更改