{
  "id": 355114,
  "title": "How to write competition metric in tensorflow",
  "url": "/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/355114",
  "author_name": "kunai",
  "post_date": "2022-09-25T13:25:19.558000",
  "votes": 2,
  "comment_count": 4,
  "views": 0,
  "content": "<p>I would like to create custom metrics in tensorflow to match the metrics of this competition as shown in the following discussion, but I don't know how to write the code. If anyone has written the code, I would appreciate it if you could let me know.<br>\n<a href=\"https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/341854\" target=\"_blank\">https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/341854</a></p>",
  "messages": [
    {
      "id": 1955048,
      "postDate": "2022-09-25T17:11:44.887Z",
      "content": "<p>You should only need to change the loss function. Try this:</p>\n<p><code>loss_fn = tf.losses.BinaryCrossEntropy(from_logits=True, reduction=tf.keras.losses.Reduction.NONE)</code></p>",
      "rawMarkdown": "You should only need to change the loss function. Try this:\n\n`loss_fn = tf.losses.BinaryCrossEntropy(from_logits=True, reduction=tf.keras.losses.Reduction.NONE)`\n",
      "votes": 1,
      "replies": [
        {
          "id": 1955620,
          "postDate": "2022-09-26T04:33:16.130Z",
          "content": "<p>Thank you for your reply.<br>\nI also tried to use BinaryCrossEntropy, but I felt that alone it would not take into account the weights specific to this competition, as shown below.</p>\n<p><code>competition_weights = {\n    '-' : torch.tensor([7, 1, 1, 1, 1, 1, 1, 1], dtype=torch.float, device=device),\n    '+' : torch.tensor([14, 2, 2, 2, 2, 2, 2, 2], dtype=torch.float, device=device),\n}</code></p>",
          "rawMarkdown": "Thank you for your reply.\nI also tried to use BinaryCrossEntropy, but I felt that alone it would not take into account the weights specific to this competition, as shown below.\n\n`competition_weights = {\n    '-' : torch.tensor([7, 1, 1, 1, 1, 1, 1, 1], dtype=torch.float, device=device),\n    '+' : torch.tensor([14, 2, 2, 2, 2, 2, 2, 2], dtype=torch.float, device=device),\n}`"
        }
      ]
    },
    {
      "id": 1954806,
      "postDate": "2022-09-25T13:25:19.557Z",
      "content": "<p>I would like to create custom metrics in tensorflow to match the metrics of this competition as shown in the following discussion, but I don't know how to write the code. If anyone has written the code, I would appreciate it if you could let me know.<br>\n<a href=\"https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/341854\" target=\"_blank\">https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/341854</a></p>",
      "rawMarkdown": "I would like to create custom metrics in tensorflow to match the metrics of this competition as shown in the following discussion, but I don't know how to write the code. If anyone has written the code, I would appreciate it if you could let me know.\nhttps://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/341854",
      "votes": 2
    },
    {
      "id": 1959972,
      "postDate": "2022-09-28T12:25:57.150Z",
      "content": "<p>Look at this notebook, the final function is the correct one, I believe<br>\n<a href=\"https://www.kaggle.com/code/solverworld/rsna2022-comp-metric\" target=\"_blank\">https://www.kaggle.com/code/solverworld/rsna2022-comp-metric</a></p>",
      "rawMarkdown": "Look at this notebook, the final function is the correct one, I believe\nhttps://www.kaggle.com/code/solverworld/rsna2022-comp-metric\n",
      "replies": [
        {
          "id": 1961897,
          "postDate": "2022-09-29T12:08:03.837Z",
          "content": "<p>Thanks for the reply.<br>\nI would like to know the metrics implemented in sensorflow, not in pytorch.</p>",
          "rawMarkdown": "Thanks for the reply.\nI would like to know the metrics implemented in sensorflow, not in pytorch."
        }
      ]
    }
  ],
  "comments": [
    {
      "id": 1955048,
      "author_name": "Samuel Cortinhas",
      "author_url": "",
      "post_date": "2022-09-25T17:11:44.887000",
      "content": "<p>You should only need to change the loss function. Try this:</p>\n<p><code>loss_fn = tf.losses.BinaryCrossEntropy(from_logits=True, reduction=tf.keras.losses.Reduction.NONE)</code></p>",
      "votes": 1,
      "replies": [
        {
          "id": 1955620,
          "author_name": "kunai",
          "author_url": "",
          "post_date": "2022-09-26T04:33:16.130000",
          "content": "<p>Thank you for your reply.<br>\nI also tried to use BinaryCrossEntropy, but I felt that alone it would not take into account the weights specific to this competition, as shown below.</p>\n<p><code>competition_weights = {\n    '-' : torch.tensor([7, 1, 1, 1, 1, 1, 1, 1], dtype=torch.float, device=device),\n    '+' : torch.tensor([14, 2, 2, 2, 2, 2, 2, 2], dtype=torch.float, device=device),\n}</code></p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 1959972,
      "author_name": "SolverWorld",
      "author_url": "",
      "post_date": "2022-09-28T12:25:57.150000",
      "content": "<p>Look at this notebook, the final function is the correct one, I believe<br>\n<a href=\"https://www.kaggle.com/code/solverworld/rsna2022-comp-metric\" target=\"_blank\">https://www.kaggle.com/code/solverworld/rsna2022-comp-metric</a></p>",
      "votes": 0,
      "replies": [
        {
          "id": 1961897,
          "author_name": "kunai",
          "author_url": "",
          "post_date": "2022-09-29T12:08:03.837000",
          "content": "<p>Thanks for the reply.<br>\nI would like to know the metrics implemented in sensorflow, not in pytorch.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1955048": "You should only need to change the loss function. Try this:\n\n`loss_fn = tf.losses.BinaryCrossEntropy(from_logits=True, reduction=tf.keras.losses.Reduction.NONE)`\n",
    "1954806": "I would like to create custom metrics in tensorflow to match the metrics of this competition as shown in the following discussion, but I don't know how to write the code. If anyone has written the code, I would appreciate it if you could let me know.\nhttps://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/341854",
    "1959972": "Look at this notebook, the final function is the correct one, I believe\nhttps://www.kaggle.com/code/solverworld/rsna2022-comp-metric\n"
  }
}