{
  "id": 144575,
  "title": "TF - Best Macro Focal Loss and F1-Metric",
  "url": "/competitions/flower-classification-with-tpus/discussion/144575",
  "author_name": "",
  "post_date": "2020-04-19T16:44:04.250676900Z",
  "votes": 12,
  "comment_count": 10,
  "views": 0,
  "content": "<p><strong>Update TensorFlow and TensorFlow-Addons to these versions</strong>\n```\n!pip install tensorflow-addons==0.9.1</p>\n\n<p>import tensorflow as tf\nimport tensorflow_addons as tfa\nfrom tensorflow.keras import backend as K\n```</p>\n\n<p><strong>Focal - Loss</strong>\nIn <strong>model.compile()</strong> specify <strong>loss=FocalLoss</strong>,\n```\ndef FocalLoss(target, input):\n    gamma = 2. # Hyperparameter that can be tuned.</p>\n\n<pre><code>input = tf.cast(input, tf.float32)    \nmax_val = K.clip(-input, 0, 1)\nloss = input - input * target + max_val + K.log(K.exp(-max_val) + K.exp(-input - max_val))\ninvprobs = tf.math.log_sigmoid(-input * (target * 2.0 - 1.0))\nloss = K.exp(invprobs * gamma) * loss    \nreturn K.mean(K.sum(loss, axis=1))\n</code></pre>\n\n<p>```</p>\n\n<p><strong>F1 - Metric</strong>\nTensorrflow - addons provides F1 metric which works for both multi-class and multi-label classification. Dont forget to one-hot encode the labels and specify the number of labels.</p>\n\n<p>To use it, specify in model.compile(),\n<code>\nmetrics = [tfa.metrics.f_scores.F1Score(num_classes=104,average=\"macro\")] \n</code></p>",
  "messages": [
    {
      "id": "813417",
      "postDate": "04/19/2020 16:44:04",
      "content": "<p><strong>Update TensorFlow and TensorFlow-Addons to these versions</strong>\n```\n!pip install tensorflow-addons==0.9.1</p>\n\n<p>import tensorflow as tf\nimport tensorflow_addons as tfa\nfrom tensorflow.keras import backend as K\n```</p>\n\n<p><strong>Focal - Loss</strong>\nIn <strong>model.compile()</strong> specify <strong>loss=FocalLoss</strong>,\n```\ndef FocalLoss(target, input):\n    gamma = 2. # Hyperparameter that can be tuned.</p>\n\n<pre><code>input = tf.cast(input, tf.float32)    \nmax_val = K.clip(-input, 0, 1)\nloss = input - input * target + max_val + K.log(K.exp(-max_val) + K.exp(-input - max_val))\ninvprobs = tf.math.log_sigmoid(-input * (target * 2.0 - 1.0))\nloss = K.exp(invprobs * gamma) * loss    \nreturn K.mean(K.sum(loss, axis=1))\n</code></pre>\n\n<p>```</p>\n\n<p><strong>F1 - Metric</strong>\nTensorrflow - addons provides F1 metric which works for both multi-class and multi-label classification. Dont forget to one-hot encode the labels and specify the number of labels.</p>\n\n<p>To use it, specify in model.compile(),\n<code>\nmetrics = [tfa.metrics.f_scores.F1Score(num_classes=104,average=\"macro\")] \n</code></p>",
      "rawMarkdown": "**Update TensorFlow and TensorFlow-Addons to these versions**\n```\n!pip install tensorflow-addons==0.9.1\n\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nfrom tensorflow.keras import backend as K\n```\n\n**Focal - Loss**\nIn **model.compile()** specify **loss=FocalLoss**,\n```\ndef FocalLoss(target, input):\n    gamma = 2. # Hyperparameter that can be tuned.\n\n    input = tf.cast(input, tf.float32)    \n    max_val = K.clip(-input, 0, 1)\n    loss = input - input * target + max_val + K.log(K.exp(-max_val) + K.exp(-input - max_val))\n    invprobs = tf.math.log_sigmoid(-input * (target * 2.0 - 1.0))\n    loss = K.exp(invprobs * gamma) * loss    \n    return K.mean(K.sum(loss, axis=1))\n```\n\n**F1 - Metric**\nTensorrflow - addons provides F1 metric which works for both multi-class and multi-label classification. Dont forget to one-hot encode the labels and specify the number of labels.\n\nTo use it, specify in model.compile(),\n```\nmetrics = [tfa.metrics.f_scores.F1Score(num_classes=104,average=\"macro\")] \n```",
      "votes": null
    },
    {
      "id": "813442",
      "postDate": "04/19/2020 17:06:36",
      "content": "<p>thanks man :)</p>",
      "rawMarkdown": "thanks man :)",
      "votes": null
    },
    {
      "id": "821583",
      "postDate": "04/26/2020 09:12:18",
      "content": "<p>hello, I use it in my Kernel, but get something wrong as follow : NotFoundError: 'ParallelInterleaveDatasetV3' is neither a type of a primitive operation nor a name of a function registered in binary running .......</p>",
      "rawMarkdown": "hello, I use it in my Kernel, but get something wrong as follow : NotFoundError: 'ParallelInterleaveDatasetV3' is neither a type of a primitive operation nor a name of a function registered in binary running .......",
      "votes": null
    },
    {
      "id": "821647",
      "postDate": "04/26/2020 10:21:01",
      "content": "<p>I forgot to edit this, </p>\n\n<p>I was getting the same error when i was trying to use TensorFlow-2.2.0 versions. This happens because of mismatch of TensorFlow versions in TPU as it never gets updated to TensorFlow-2.2.0 on Kaggle but, the same thing works on Colab. </p>\n\n<p>Which version of TensorFlow are you using? Try the same using TensorFlow-2.1.0 that is provided in Kaggle already. </p>\n\n<p>Plus, don't forget to one_hot encode the targets for both training and validation data.</p>",
      "rawMarkdown": "I forgot to edit this, \n\nI was getting the same error when i was trying to use TensorFlow-2.2.0 versions. This happens because of mismatch of TensorFlow versions in TPU as it never gets updated to TensorFlow-2.2.0 on Kaggle but, the same thing works on Colab. \n\nWhich version of TensorFlow are you using? Try the same using TensorFlow-2.1.0 that is provided in Kaggle already. \n\nPlus, don't forget to one_hot encode the targets for both training and validation data.",
      "votes": null
    },
    {
      "id": "821698",
      "postDate": "04/26/2020 10:56:10",
      "content": "<p>yeah, it works, thanks very much👍 </p>",
      "rawMarkdown": "yeah, it works, thanks very much👍",
      "votes": null
    },
    {
      "id": "825088",
      "postDate": "04/28/2020 19:13:45",
      "content": "<p>Seems like tfa.metrics.f_scores.F1score is not the best metric to use on my experimentation. Has anyone else felt the same?</p>",
      "rawMarkdown": "Seems like tfa.metrics.f_scores.F1score is not the best metric to use on my experimentation. Has anyone else felt the same?",
      "votes": null
    },
    {
      "id": "848613",
      "postDate": "05/15/2020 05:34:46",
      "content": "<p>Thanks.</p>",
      "rawMarkdown": "Thanks.",
      "votes": null
    },
    {
      "id": "964677",
      "postDate": "08/10/2020 05:06:36",
      "content": "<p><code>\nmy be is too late for this but just use it like this \nf1 = tfa.metrics.F1Score(num_classes=2,average=\"macro\")\n</code></p>",
      "rawMarkdown": "```\nmy be is too late for this but just use it like this \nf1 = tfa.metrics.F1Score(num_classes=2,average=\"macro\")\n```",
      "votes": null
    },
    {
      "id": "967472",
      "postDate": "08/12/2020 09:14:28",
      "content": "<p>this focal loss are only for the multi-class ???</p>",
      "rawMarkdown": "this focal loss are only for the multi-class ???",
      "votes": null
    },
    {
      "id": "981489",
      "postDate": "08/22/2020 13:39:00",
      "content": "<blockquote>\n  <p>this focal loss are only for the multi-class </p>\n</blockquote>",
      "rawMarkdown": "> this focal loss are only for the multi-class",
      "votes": null
    },
    {
      "id": "996935",
      "postDate": "09/03/2020 16:40:23",
      "content": "<p>amazing thanks</p>",
      "rawMarkdown": "amazing thanks",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 967472,
      "author_name": "itsmekhey",
      "author_url": "",
      "post_date": "08/12/2020 09:14:28",
      "content": "<p>this focal loss are only for the multi-class ???</p>",
      "votes": null,
      "replies": [
        {
          "id": 981489,
          "author_name": "tikoboss",
          "author_url": "",
          "post_date": "08/22/2020 13:39:00",
          "content": "<blockquote>\n  <p>this focal loss are only for the multi-class </p>\n</blockquote>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 996935,
      "author_name": "zsinghrahulk",
      "author_url": "",
      "post_date": "09/03/2020 16:40:23",
      "content": "<p>amazing thanks</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 813442,
      "author_name": "albeffe",
      "author_url": "",
      "post_date": "04/19/2020 17:06:36",
      "content": "<p>thanks man :)</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 821583,
      "author_name": "freshmen",
      "author_url": "",
      "post_date": "04/26/2020 09:12:18",
      "content": "<p>hello, I use it in my Kernel, but get something wrong as follow : NotFoundError: 'ParallelInterleaveDatasetV3' is neither a type of a primitive operation nor a name of a function registered in binary running .......</p>",
      "votes": null,
      "replies": [
        {
          "id": 821647,
          "author_name": "rhtsingh",
          "author_url": "",
          "post_date": "04/26/2020 10:21:01",
          "content": "<p>I forgot to edit this, </p>\n\n<p>I was getting the same error when i was trying to use TensorFlow-2.2.0 versions. This happens because of mismatch of TensorFlow versions in TPU as it never gets updated to TensorFlow-2.2.0 on Kaggle but, the same thing works on Colab. </p>\n\n<p>Which version of TensorFlow are you using? Try the same using TensorFlow-2.1.0 that is provided in Kaggle already. </p>\n\n<p>Plus, don't forget to one_hot encode the targets for both training and validation data.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 821698,
          "author_name": "freshmen",
          "author_url": "",
          "post_date": "04/26/2020 10:56:10",
          "content": "<p>yeah, it works, thanks very much👍 </p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 964677,
          "author_name": "itsmekhey",
          "author_url": "",
          "post_date": "08/10/2020 05:06:36",
          "content": "<p><code>\nmy be is too late for this but just use it like this \nf1 = tfa.metrics.F1Score(num_classes=2,average=\"macro\")\n</code></p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 825088,
      "author_name": "kurianbenoy",
      "author_url": "",
      "post_date": "04/28/2020 19:13:45",
      "content": "<p>Seems like tfa.metrics.f_scores.F1score is not the best metric to use on my experimentation. Has anyone else felt the same?</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 848613,
      "author_name": "zhangyingkk",
      "author_url": "",
      "post_date": "05/15/2020 05:34:46",
      "content": "<p>Thanks.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "813417": "**Update TensorFlow and TensorFlow-Addons to these versions**\n```\n!pip install tensorflow-addons==0.9.1\n\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nfrom tensorflow.keras import backend as K\n```\n\n**Focal - Loss**\nIn **model.compile()** specify **loss=FocalLoss**,\n```\ndef FocalLoss(target, input):\n    gamma = 2. # Hyperparameter that can be tuned.\n\n    input = tf.cast(input, tf.float32)    \n    max_val = K.clip(-input, 0, 1)\n    loss = input - input * target + max_val + K.log(K.exp(-max_val) + K.exp(-input - max_val))\n    invprobs = tf.math.log_sigmoid(-input * (target * 2.0 - 1.0))\n    loss = K.exp(invprobs * gamma) * loss    \n    return K.mean(K.sum(loss, axis=1))\n```\n\n**F1 - Metric**\nTensorrflow - addons provides F1 metric which works for both multi-class and multi-label classification. Dont forget to one-hot encode the labels and specify the number of labels.\n\nTo use it, specify in model.compile(),\n```\nmetrics = [tfa.metrics.f_scores.F1Score(num_classes=104,average=\"macro\")] \n```",
    "813442": "thanks man :)",
    "821583": "hello, I use it in my Kernel, but get something wrong as follow : NotFoundError: 'ParallelInterleaveDatasetV3' is neither a type of a primitive operation nor a name of a function registered in binary running .......",
    "821647": "I forgot to edit this, \n\nI was getting the same error when i was trying to use TensorFlow-2.2.0 versions. This happens because of mismatch of TensorFlow versions in TPU as it never gets updated to TensorFlow-2.2.0 on Kaggle but, the same thing works on Colab. \n\nWhich version of TensorFlow are you using? Try the same using TensorFlow-2.1.0 that is provided in Kaggle already. \n\nPlus, don't forget to one_hot encode the targets for both training and validation data.",
    "821698": "yeah, it works, thanks very much👍",
    "825088": "Seems like tfa.metrics.f_scores.F1score is not the best metric to use on my experimentation. Has anyone else felt the same?",
    "848613": "Thanks.",
    "964677": "```\nmy be is too late for this but just use it like this \nf1 = tfa.metrics.F1Score(num_classes=2,average=\"macro\")\n```",
    "967472": "this focal loss are only for the multi-class ???",
    "981489": "> this focal loss are only for the multi-class",
    "996935": "amazing thanks"
  },
  "source": "meta"
}