{
  "id": 187787,
  "title": "BCEWithLogitsLoss Label Smooth",
  "url": "/competitions/rsna-str-pulmonary-embolism-detection/discussion/187787",
  "author_name": "Manh Lab",
  "post_date": "2020-09-30T09:38:47.345000",
  "votes": 11,
  "comment_count": 2,
  "views": 0,
  "content": "<p>I  think the label smooth helpful in this competition. So I share my BCE with label smooth here:</p>\n<pre><code>import torch\nimport math\nimport torch.nn as nn\nimport torch.nn.functional as F\n\ndef binary_cross_entropy(inputs, target, weight=None, reduction='mean', smooth_eps=None, from_logits=False):\n    \"\"\"cross entropy loss, with support for label smoothing https://arxiv.org/abs/1512.00567\"\"\"\n    smooth_eps = smooth_eps or 0\n    if smooth_eps &gt; 0:\n        target = target.float()\n        target.add_(smooth_eps).div_(2.)\n    if from_logits:\n        return F.binary_cross_entropy_with_logits(inputs, target, weight=weight, reduction=reduction)\n    else:\n        return F.binary_cross_entropy(inputs, target, weight=weight, reduction=reduction)\n\n\ndef binary_cross_entropy_with_logits(inputs, target, weight=None, reduction='mean', smooth_eps=None, from_logits=True):\n    return binary_cross_entropy(inputs, target, weight, reduction, smooth_eps, from_logits)\n\n\nclass BCELoss(nn.BCELoss):\n    def __init__(self, weight=None, size_average=None, reduce=None, reduction='mean', smooth_eps=None, from_logits=False):\n        super(BCELoss, self).__init__(weight, size_average, reduce, reduction)\n        self.smooth_eps = smooth_eps\n        self.from_logits = from_logits\n\n    def forward(self, input, target):\n        return binary_cross_entropy(input, target,\n                                    weight=self.weight, reduction=self.reduction,\n                                    smooth_eps=self.smooth_eps, from_logits=self.from_logits)\n\n\nclass BCEWithLogitsLoss(BCELoss):\n    def __init__(self, weight=None, size_average=None, reduce=None, reduction='mean', smooth_eps=None, from_logits=True):\n        super(BCEWithLogitsLoss, self).__init__(weight, size_average,\n                                                reduce, reduction, smooth_eps=smooth_eps, from_logits=from_logits)\n</code></pre>",
  "messages": [
    {
      "id": 1032559,
      "postDate": "2020-09-30T09:38:47.347Z",
      "content": "<p>I  think the label smooth helpful in this competition. So I share my BCE with label smooth here:</p>\n<pre><code>import torch\nimport math\nimport torch.nn as nn\nimport torch.nn.functional as F\n\ndef binary_cross_entropy(inputs, target, weight=None, reduction='mean', smooth_eps=None, from_logits=False):\n    \"\"\"cross entropy loss, with support for label smoothing https://arxiv.org/abs/1512.00567\"\"\"\n    smooth_eps = smooth_eps or 0\n    if smooth_eps &gt; 0:\n        target = target.float()\n        target.add_(smooth_eps).div_(2.)\n    if from_logits:\n        return F.binary_cross_entropy_with_logits(inputs, target, weight=weight, reduction=reduction)\n    else:\n        return F.binary_cross_entropy(inputs, target, weight=weight, reduction=reduction)\n\n\ndef binary_cross_entropy_with_logits(inputs, target, weight=None, reduction='mean', smooth_eps=None, from_logits=True):\n    return binary_cross_entropy(inputs, target, weight, reduction, smooth_eps, from_logits)\n\n\nclass BCELoss(nn.BCELoss):\n    def __init__(self, weight=None, size_average=None, reduce=None, reduction='mean', smooth_eps=None, from_logits=False):\n        super(BCELoss, self).__init__(weight, size_average, reduce, reduction)\n        self.smooth_eps = smooth_eps\n        self.from_logits = from_logits\n\n    def forward(self, input, target):\n        return binary_cross_entropy(input, target,\n                                    weight=self.weight, reduction=self.reduction,\n                                    smooth_eps=self.smooth_eps, from_logits=self.from_logits)\n\n\nclass BCEWithLogitsLoss(BCELoss):\n    def __init__(self, weight=None, size_average=None, reduce=None, reduction='mean', smooth_eps=None, from_logits=True):\n        super(BCEWithLogitsLoss, self).__init__(weight, size_average,\n                                                reduce, reduction, smooth_eps=smooth_eps, from_logits=from_logits)\n</code></pre>",
      "rawMarkdown": "I  think the label smooth helpful in this competition. So I share my BCE with label smooth here:\n\n```\nimport torch\nimport math\nimport torch.nn as nn\nimport torch.nn.functional as F\n\ndef binary_cross_entropy(inputs, target, weight=None, reduction='mean', smooth_eps=None, from_logits=False):\n    \"\"\"cross entropy loss, with support for label smoothing https://arxiv.org/abs/1512.00567\"\"\"\n    smooth_eps = smooth_eps or 0\n    if smooth_eps > 0:\n        target = target.float()\n        target.add_(smooth_eps).div_(2.)\n    if from_logits:\n        return F.binary_cross_entropy_with_logits(inputs, target, weight=weight, reduction=reduction)\n    else:\n        return F.binary_cross_entropy(inputs, target, weight=weight, reduction=reduction)\n\n\ndef binary_cross_entropy_with_logits(inputs, target, weight=None, reduction='mean', smooth_eps=None, from_logits=True):\n    return binary_cross_entropy(inputs, target, weight, reduction, smooth_eps, from_logits)\n\n\nclass BCELoss(nn.BCELoss):\n    def __init__(self, weight=None, size_average=None, reduce=None, reduction='mean', smooth_eps=None, from_logits=False):\n        super(BCELoss, self).__init__(weight, size_average, reduce, reduction)\n        self.smooth_eps = smooth_eps\n        self.from_logits = from_logits\n\n    def forward(self, input, target):\n        return binary_cross_entropy(input, target,\n                                    weight=self.weight, reduction=self.reduction,\n                                    smooth_eps=self.smooth_eps, from_logits=self.from_logits)\n\n\nclass BCEWithLogitsLoss(BCELoss):\n    def __init__(self, weight=None, size_average=None, reduce=None, reduction='mean', smooth_eps=None, from_logits=True):\n        super(BCEWithLogitsLoss, self).__init__(weight, size_average,\n                                                reduce, reduction, smooth_eps=smooth_eps, from_logits=from_logits)\n```",
      "votes": 11
    },
    {
      "id": 1033701,
      "postDate": "2020-10-01T07:51:14.837Z",
      "content": "<p>Thanks for sharing! Indeed, label smoothing can be useful in such a setup to add a bit of regularization.</p>",
      "rawMarkdown": "Thanks for sharing! Indeed, label smoothing can be useful in such a setup to add a bit of regularization."
    },
    {
      "id": 1033178,
      "postDate": "2020-09-30T18:14:45.603Z",
      "content": "<p>Thank you for sharing!</p>",
      "rawMarkdown": "Thank you for sharing!"
    }
  ],
  "comments": [
    {
      "id": 1033701,
      "author_name": "Nikita Kozodoi",
      "author_url": "",
      "post_date": "2020-10-01T07:51:14.837000",
      "content": "<p>Thanks for sharing! Indeed, label smoothing can be useful in such a setup to add a bit of regularization.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 1033178,
      "author_name": "Ronaldo S.A. Batista",
      "author_url": "",
      "post_date": "2020-09-30T18:14:45.603000",
      "content": "<p>Thank you for sharing!</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1032559": "I  think the label smooth helpful in this competition. So I share my BCE with label smooth here:\n\n```\nimport torch\nimport math\nimport torch.nn as nn\nimport torch.nn.functional as F\n\ndef binary_cross_entropy(inputs, target, weight=None, reduction='mean', smooth_eps=None, from_logits=False):\n    \"\"\"cross entropy loss, with support for label smoothing https://arxiv.org/abs/1512.00567\"\"\"\n    smooth_eps = smooth_eps or 0\n    if smooth_eps > 0:\n        target = target.float()\n        target.add_(smooth_eps).div_(2.)\n    if from_logits:\n        return F.binary_cross_entropy_with_logits(inputs, target, weight=weight, reduction=reduction)\n    else:\n        return F.binary_cross_entropy(inputs, target, weight=weight, reduction=reduction)\n\n\ndef binary_cross_entropy_with_logits(inputs, target, weight=None, reduction='mean', smooth_eps=None, from_logits=True):\n    return binary_cross_entropy(inputs, target, weight, reduction, smooth_eps, from_logits)\n\n\nclass BCELoss(nn.BCELoss):\n    def __init__(self, weight=None, size_average=None, reduce=None, reduction='mean', smooth_eps=None, from_logits=False):\n        super(BCELoss, self).__init__(weight, size_average, reduce, reduction)\n        self.smooth_eps = smooth_eps\n        self.from_logits = from_logits\n\n    def forward(self, input, target):\n        return binary_cross_entropy(input, target,\n                                    weight=self.weight, reduction=self.reduction,\n                                    smooth_eps=self.smooth_eps, from_logits=self.from_logits)\n\n\nclass BCEWithLogitsLoss(BCELoss):\n    def __init__(self, weight=None, size_average=None, reduce=None, reduction='mean', smooth_eps=None, from_logits=True):\n        super(BCEWithLogitsLoss, self).__init__(weight, size_average,\n                                                reduce, reduction, smooth_eps=smooth_eps, from_logits=from_logits)\n```",
    "1033701": "Thanks for sharing! Indeed, label smoothing can be useful in such a setup to add a bit of regularization.",
    "1033178": "Thank you for sharing!"
  }
}