{
  "id": 111513,
  "title": "Official metric in pytorch",
  "url": "/competitions/rsna-intracranial-hemorrhage-detection/discussion/111513",
  "author_name": "Nicholas Lyu",
  "post_date": "2019-10-06T13:43:32.801000",
  "votes": 7,
  "comment_count": 5,
  "views": 0,
  "content": "<p>Been searching around kernels and discussion for the official metric implementation. I wonder if my version is correct. Any replies welcome:-)</p>\n\n<p>def criterion(pred, target):\n    bce = F.binary_cross_entropy_with_logits(pred, target, reduction='none').mean(0)\n    bce[5] = bce[5]*2\n    return bce.sum()/7</p>",
  "messages": [
    {
      "id": 642745,
      "postDate": "2019-10-06T15:20:04.387Z",
      "content": "<p>As <a href=\"/micpie\">@micpie</a> mentioned you can use weights option in pytorch's BCE. But also <a href=\"/yuval6967\">@yuval6967</a> already posted on the forum which works fine.</p>\n\n<p><code>\ndef my_loss(y_pred,y_true,weights):\n    return F.binary_cross_entropy_with_logits(y_pred,\n                                  y_true,\n                                  weights.repeat(y_pred.shape[0],1))\n</code></p>\n\n<p>make sure weights are float tensor, \n<code>[1.0, 1.0, 1.0, 1.0, 1.0, 2.0]</code> (2.0 is for class Any) </p>\n\n<p>here is he link: <a href=\"https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/111198#latest-642620\">https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/111198#latest-642620</a></p>",
      "rawMarkdown": "As @micpie mentioned you can use weights option in pytorch's BCE. But also @yuval6967 already posted on the forum which works fine.\n\n\n```\ndef my_loss(y_pred,y_true,weights):\n    return F.binary_cross_entropy_with_logits(y_pred,\n                                  y_true,\n                                  weights.repeat(y_pred.shape[0],1))\n```\n\nmake sure weights are float tensor, \n`[1.0, 1.0, 1.0, 1.0, 1.0, 2.0]` (2.0 is for class Any) \n\nhere is he link: https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/111198#latest-642620",
      "votes": 10,
      "replies": [
        {
          "id": 642754,
          "postDate": "2019-10-06T15:31:50.823Z",
          "content": "<p><a href=\"/drhabib\">@drhabib</a> 404 error. are you linking the right discussion post?</p>",
          "rawMarkdown": "@drhabib 404 error. are you linking the right discussion post?",
          "votes": 1
        },
        {
          "id": 642760,
          "postDate": "2019-10-06T15:40:12.550Z",
          "content": "<p>sorry I just corrected. </p>",
          "rawMarkdown": "sorry I just corrected. "
        },
        {
          "id": 643811,
          "postDate": "2019-10-08T00:31:32.263Z",
          "content": "<p>I used this implementation and I noticed that if I plug in y_pred=y_true I get a non-zero value. Is this correct? This doesn't make sense to me.</p>",
          "rawMarkdown": "I used this implementation and I noticed that if I plug in y_pred=y_true I get a non-zero value. Is this correct? This doesn't make sense to me."
        }
      ]
    },
    {
      "id": 642690,
      "postDate": "2019-10-06T13:43:32.800Z",
      "content": "<p>Been searching around kernels and discussion for the official metric implementation. I wonder if my version is correct. Any replies welcome:-)</p>\n\n<p>def criterion(pred, target):\n    bce = F.binary_cross_entropy_with_logits(pred, target, reduction='none').mean(0)\n    bce[5] = bce[5]*2\n    return bce.sum()/7</p>",
      "rawMarkdown": "Been searching around kernels and discussion for the official metric implementation. I wonder if my version is correct. Any replies welcome:-)\n\ndef criterion(pred, target):\n    bce = F.binary_cross_entropy_with_logits(pred, target, reduction='none').mean(0)\n    bce[5] = bce[5]*2\n    return bce.sum()/7",
      "votes": 7
    },
    {
      "id": 642740,
      "postDate": "2019-10-06T15:06:47.197Z",
      "content": "<p>When looking into the formulas from <a href=\"https://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html\">sklearn.metrics.log_loss</a> used in <a href=\"https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/110461#latest-641855\">another discussion thread</a>, a <a href=\"https://www.kaggle.com/dansbecker/what-is-log-loss\">notebook linked in the competition overview</a>, and an <a href=\"https://gombru.github.io/2018/05/23/cross_entropy_loss/\">interesting article about CE losses</a> this looks reasonable!</p>\n\n<p>According to the <a href=\"https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.binary_cross_entropy_with_logits\">pytorch documentation F.binary_cross_entropy_with_logits</a> can also take a weight as input (btw with backslash you can escape the italic markdown) .</p>\n\n<p>I am currently trying to get the metric working on my machine and will get back to you when I have tested it.</p>",
      "rawMarkdown": "When looking into the formulas from [sklearn.metrics.log_loss](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html) used in [another discussion thread](https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/110461#latest-641855), a [notebook linked in the competition overview](https://www.kaggle.com/dansbecker/what-is-log-loss), and an [interesting article about CE losses](https://gombru.github.io/2018/05/23/cross_entropy_loss/) this looks reasonable!\n\nAccording to the [pytorch documentation F.binary\\_cross\\_entropy\\_with\\_logits](https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.binary_cross_entropy_with_logits) can also take a weight as input (btw with backslash you can escape the italic markdown) .\n\nI am currently trying to get the metric working on my machine and will get back to you when I have tested it.",
      "votes": 1
    }
  ],
  "comments": [
    {
      "id": 642745,
      "author_name": "DrHB",
      "author_url": "",
      "post_date": "2019-10-06T15:20:04.387000",
      "content": "<p>As <a href=\"/micpie\">@micpie</a> mentioned you can use weights option in pytorch's BCE. But also <a href=\"/yuval6967\">@yuval6967</a> already posted on the forum which works fine.</p>\n\n<p><code>\ndef my_loss(y_pred,y_true,weights):\n    return F.binary_cross_entropy_with_logits(y_pred,\n                                  y_true,\n                                  weights.repeat(y_pred.shape[0],1))\n</code></p>\n\n<p>make sure weights are float tensor, \n<code>[1.0, 1.0, 1.0, 1.0, 1.0, 2.0]</code> (2.0 is for class Any) </p>\n\n<p>here is he link: <a href=\"https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/111198#latest-642620\">https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/111198#latest-642620</a></p>",
      "votes": 10,
      "replies": [
        {
          "id": 642754,
          "author_name": "Tim Yee",
          "author_url": "",
          "post_date": "2019-10-06T15:31:50.823000",
          "content": "<p><a href=\"/drhabib\">@drhabib</a> 404 error. are you linking the right discussion post?</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 642760,
          "author_name": "DrHB",
          "author_url": "",
          "post_date": "2019-10-06T15:40:12.550000",
          "content": "<p>sorry I just corrected. </p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 643811,
          "author_name": "ilovescience",
          "author_url": "",
          "post_date": "2019-10-08T00:31:32.263000",
          "content": "<p>I used this implementation and I noticed that if I plug in y_pred=y_true I get a non-zero value. Is this correct? This doesn't make sense to me.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 642740,
      "author_name": "Michael Pieler",
      "author_url": "",
      "post_date": "2019-10-06T15:06:47.197000",
      "content": "<p>When looking into the formulas from <a href=\"https://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html\">sklearn.metrics.log_loss</a> used in <a href=\"https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/110461#latest-641855\">another discussion thread</a>, a <a href=\"https://www.kaggle.com/dansbecker/what-is-log-loss\">notebook linked in the competition overview</a>, and an <a href=\"https://gombru.github.io/2018/05/23/cross_entropy_loss/\">interesting article about CE losses</a> this looks reasonable!</p>\n\n<p>According to the <a href=\"https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.binary_cross_entropy_with_logits\">pytorch documentation F.binary_cross_entropy_with_logits</a> can also take a weight as input (btw with backslash you can escape the italic markdown) .</p>\n\n<p>I am currently trying to get the metric working on my machine and will get back to you when I have tested it.</p>",
      "votes": 1,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "642745": "As @micpie mentioned you can use weights option in pytorch's BCE. But also @yuval6967 already posted on the forum which works fine.\n\n\n```\ndef my_loss(y_pred,y_true,weights):\n    return F.binary_cross_entropy_with_logits(y_pred,\n                                  y_true,\n                                  weights.repeat(y_pred.shape[0],1))\n```\n\nmake sure weights are float tensor, \n`[1.0, 1.0, 1.0, 1.0, 1.0, 2.0]` (2.0 is for class Any) \n\nhere is he link: https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/111198#latest-642620",
    "642690": "Been searching around kernels and discussion for the official metric implementation. I wonder if my version is correct. Any replies welcome:-)\n\ndef criterion(pred, target):\n    bce = F.binary_cross_entropy_with_logits(pred, target, reduction='none').mean(0)\n    bce[5] = bce[5]*2\n    return bce.sum()/7",
    "642740": "When looking into the formulas from [sklearn.metrics.log_loss](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html) used in [another discussion thread](https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/discussion/110461#latest-641855), a [notebook linked in the competition overview](https://www.kaggle.com/dansbecker/what-is-log-loss), and an [interesting article about CE losses](https://gombru.github.io/2018/05/23/cross_entropy_loss/) this looks reasonable!\n\nAccording to the [pytorch documentation F.binary\\_cross\\_entropy\\_with\\_logits](https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.binary_cross_entropy_with_logits) can also take a weight as input (btw with backslash you can escape the italic markdown) .\n\nI am currently trying to get the metric working on my machine and will get back to you when I have tested it."
  }
}