{
  "id": 149689,
  "title": "In TPU, can't I use 'class_weight' in 'model.fit()' ?",
  "url": "/competitions/alaska2-image-steganalysis/discussion/149689",
  "author_name": "",
  "post_date": "2020-05-09T13:18:41.116684400Z",
  "votes": 6,
  "comment_count": 4,
  "views": 0,
  "content": "<p>Hello everyone.\nI am trying TPU for the first time, but I stumbled.</p>\n\n<p>In TPU, can't I use <code>class_weight</code> in<code>model.fit()</code>?\nI get an error with the code below (I don't understand what the error is).</p>\n\n<p>It <strong>runs fine on the GPU instead of the TPU</strong>. It can also be <strong>run on TPU by commenting out</strong> <code>class_weight</code>.</p>\n\n<p><strong>Error  Code</strong>\n<code>\nhistory = model.fit(\n    train_dataset, \n    epochs = EPOCHS, \n    steps_per_epoch = y_train.shape[0] // BATCH_SIZE,\n    validation_data = valid_dataset,\n    class_weight = {0:1., 1:1./3}        # &lt;===== ?????\n)\n</code>\n<strong>Error message</strong>\n<code>\nInvalidArgumentError: {{function_node __inference_distributed_function_363200}} Compilation failure: Detected unsupported operations when trying to compile graph has_valid_nonscalar_shape_true_357243_const_0[] on XLA_TPU_JIT: DenseToDenseSetOperation (No registered 'DenseToDenseSetOperation' OpKernel for XLA_TPU_JIT devices compatible with node {{node has_invalid_dims/DenseToDenseSetOperation}}\n    .  Registered:  device='CPU'; T in [DT_INT8]\n  device='CPU'; T in [DT_INT16]\n  device='CPU'; T in [DT_INT32]\n  device='CPU'; T in [DT_INT64]\n  device='CPU'; T in [DT_UINT8]\n  device='CPU'; T in [DT_UINT16]\n  device='CPU'; T in [DT_STRING]\n){{node has_invalid_dims/DenseToDenseSetOperation}}\n     [[has_valid_nonscalar_shape]]\n     [[loss/dense_17_loss/weighted_loss/broadcast_weights/assert_broadcastable/is_valid_shape]]\n    TPU compilation failed\n     [[tpu_compile_succeeded_assert/_12217194444665189181/_6]]\n</code>\nPlease give me some advice. Thank you.</p>",
  "messages": [
    {
      "id": "839560",
      "postDate": "05/09/2020 13:18:41",
      "content": "<p>Hello everyone.\nI am trying TPU for the first time, but I stumbled.</p>\n\n<p>In TPU, can't I use <code>class_weight</code> in<code>model.fit()</code>?\nI get an error with the code below (I don't understand what the error is).</p>\n\n<p>It <strong>runs fine on the GPU instead of the TPU</strong>. It can also be <strong>run on TPU by commenting out</strong> <code>class_weight</code>.</p>\n\n<p><strong>Error  Code</strong>\n<code>\nhistory = model.fit(\n    train_dataset, \n    epochs = EPOCHS, \n    steps_per_epoch = y_train.shape[0] // BATCH_SIZE,\n    validation_data = valid_dataset,\n    class_weight = {0:1., 1:1./3}        # &lt;===== ?????\n)\n</code>\n<strong>Error message</strong>\n<code>\nInvalidArgumentError: {{function_node __inference_distributed_function_363200}} Compilation failure: Detected unsupported operations when trying to compile graph has_valid_nonscalar_shape_true_357243_const_0[] on XLA_TPU_JIT: DenseToDenseSetOperation (No registered 'DenseToDenseSetOperation' OpKernel for XLA_TPU_JIT devices compatible with node {{node has_invalid_dims/DenseToDenseSetOperation}}\n    .  Registered:  device='CPU'; T in [DT_INT8]\n  device='CPU'; T in [DT_INT16]\n  device='CPU'; T in [DT_INT32]\n  device='CPU'; T in [DT_INT64]\n  device='CPU'; T in [DT_UINT8]\n  device='CPU'; T in [DT_UINT16]\n  device='CPU'; T in [DT_STRING]\n){{node has_invalid_dims/DenseToDenseSetOperation}}\n     [[has_valid_nonscalar_shape]]\n     [[loss/dense_17_loss/weighted_loss/broadcast_weights/assert_broadcastable/is_valid_shape]]\n    TPU compilation failed\n     [[tpu_compile_succeeded_assert/_12217194444665189181/_6]]\n</code>\nPlease give me some advice. Thank you.</p>",
      "rawMarkdown": "Hello everyone.\nI am trying TPU for the first time, but I stumbled.\n\nIn TPU, can't I use `class_weight` in`model.fit()`?\nI get an error with the code below (I don't understand what the error is).\n\nIt **runs fine on the GPU instead of the TPU**. It can also be **run on TPU by commenting out** `class_weight`.\n\n**Error  Code**\n```\nhistory = model.fit(\n    train_dataset, \n    epochs = EPOCHS, \n    steps_per_epoch = y_train.shape[0] // BATCH_SIZE,\n    validation_data = valid_dataset,\n    class_weight = {0:1., 1:1./3}        # &lt;===== ?????\n)\n```\n**Error message**\n```\nInvalidArgumentError: {{function_node __inference_distributed_function_363200}} Compilation failure: Detected unsupported operations when trying to compile graph has_valid_nonscalar_shape_true_357243_const_0[] on XLA_TPU_JIT: DenseToDenseSetOperation (No registered 'DenseToDenseSetOperation' OpKernel for XLA_TPU_JIT devices compatible with node {{node has_invalid_dims/DenseToDenseSetOperation}}\n\t.  Registered:  device='CPU'; T in [DT_INT8]\n  device='CPU'; T in [DT_INT16]\n  device='CPU'; T in [DT_INT32]\n  device='CPU'; T in [DT_INT64]\n  device='CPU'; T in [DT_UINT8]\n  device='CPU'; T in [DT_UINT16]\n  device='CPU'; T in [DT_STRING]\n){{node has_invalid_dims/DenseToDenseSetOperation}}\n\t [[has_valid_nonscalar_shape]]\n\t [[loss/dense_17_loss/weighted_loss/broadcast_weights/assert_broadcastable/is_valid_shape]]\n\tTPU compilation failed\n\t [[tpu_compile_succeeded_assert/_12217194444665189181/_6]]\n```\nPlease give me some advice. Thank you.",
      "votes": null
    },
    {
      "id": "840257",
      "postDate": "05/09/2020 21:35:57",
      "content": "<p>I've run into this same issue on TPUs before. Unfortunately, class weighting doesn't work on TPUs. You'll need to create a weighted loss function.</p>\n\n<p>See helpful discussion here: <a href=\"https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272\">https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272</a></p>",
      "rawMarkdown": "I've run into this same issue on TPUs before. Unfortunately, class weighting doesn't work on TPUs. You'll need to create a weighted loss function.\n\nSee helpful discussion here: https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272",
      "votes": null
    },
    {
      "id": "840664",
      "postDate": "05/10/2020 08:57:15",
      "content": "<p>Hi Dave!\nThank you for the information.\nI implemented the weighted loss function with reference to the following, and it works without error on TPU.\n* <a href=\"https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272\">https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272</a>\n* <a href=\"https://github.com/keras-team/keras/issues/2115\">https://github.com/keras-team/keras/issues/2115</a></p>\n\n<p>I'm checking if it's working properly ...\nIn any case, thank you.</p>",
      "rawMarkdown": "Hi Dave!\nThank you for the information.\nI implemented the weighted loss function with reference to the following, and it works without error on TPU.\n* https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272\n* https://github.com/keras-team/keras/issues/2115\n\nI'm checking if it's working properly ...\nIn any case, thank you.",
      "votes": null
    },
    {
      "id": "844993",
      "postDate": "05/13/2020 01:49:33",
      "content": "<p>Thanks for posting this!  Found this helpful 👍 </p>",
      "rawMarkdown": "Thanks for posting this!  Found this helpful 👍",
      "votes": null
    },
    {
      "id": "847625",
      "postDate": "05/14/2020 14:27:32",
      "content": "<p>Hi Matt!\nIt was able to run without error. However, the expected effect has not been obtained. My model may not be good, but originally, the data of this competition has a poor identification rate, so I think it may be better to use the same number of data for each class.</p>",
      "rawMarkdown": "Hi Matt!\nIt was able to run without error. However, the expected effect has not been obtained. My model may not be good, but originally, the data of this competition has a poor identification rate, so I think it may be better to use the same number of data for each class.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 840257,
      "author_name": "davelo12",
      "author_url": "",
      "post_date": "05/09/2020 21:35:57",
      "content": "<p>I've run into this same issue on TPUs before. Unfortunately, class weighting doesn't work on TPUs. You'll need to create a weighted loss function.</p>\n\n<p>See helpful discussion here: <a href=\"https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272\">https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 840664,
          "author_name": "amanooo",
          "author_url": "",
          "post_date": "05/10/2020 08:57:15",
          "content": "<p>Hi Dave!\nThank you for the information.\nI implemented the weighted loss function with reference to the following, and it works without error on TPU.\n* <a href=\"https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272\">https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272</a>\n* <a href=\"https://github.com/keras-team/keras/issues/2115\">https://github.com/keras-team/keras/issues/2115</a></p>\n\n<p>I'm checking if it's working properly ...\nIn any case, thank you.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 844993,
      "author_name": "yeayates21",
      "author_url": "",
      "post_date": "05/13/2020 01:49:33",
      "content": "<p>Thanks for posting this!  Found this helpful 👍 </p>",
      "votes": null,
      "replies": [
        {
          "id": 847625,
          "author_name": "amanooo",
          "author_url": "",
          "post_date": "05/14/2020 14:27:32",
          "content": "<p>Hi Matt!\nIt was able to run without error. However, the expected effect has not been obtained. My model may not be good, but originally, the data of this competition has a poor identification rate, so I think it may be better to use the same number of data for each class.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "839560": "Hello everyone.\nI am trying TPU for the first time, but I stumbled.\n\nIn TPU, can't I use `class_weight` in`model.fit()`?\nI get an error with the code below (I don't understand what the error is).\n\nIt **runs fine on the GPU instead of the TPU**. It can also be **run on TPU by commenting out** `class_weight`.\n\n**Error  Code**\n```\nhistory = model.fit(\n    train_dataset, \n    epochs = EPOCHS, \n    steps_per_epoch = y_train.shape[0] // BATCH_SIZE,\n    validation_data = valid_dataset,\n    class_weight = {0:1., 1:1./3}        # &lt;===== ?????\n)\n```\n**Error message**\n```\nInvalidArgumentError: {{function_node __inference_distributed_function_363200}} Compilation failure: Detected unsupported operations when trying to compile graph has_valid_nonscalar_shape_true_357243_const_0[] on XLA_TPU_JIT: DenseToDenseSetOperation (No registered 'DenseToDenseSetOperation' OpKernel for XLA_TPU_JIT devices compatible with node {{node has_invalid_dims/DenseToDenseSetOperation}}\n\t.  Registered:  device='CPU'; T in [DT_INT8]\n  device='CPU'; T in [DT_INT16]\n  device='CPU'; T in [DT_INT32]\n  device='CPU'; T in [DT_INT64]\n  device='CPU'; T in [DT_UINT8]\n  device='CPU'; T in [DT_UINT16]\n  device='CPU'; T in [DT_STRING]\n){{node has_invalid_dims/DenseToDenseSetOperation}}\n\t [[has_valid_nonscalar_shape]]\n\t [[loss/dense_17_loss/weighted_loss/broadcast_weights/assert_broadcastable/is_valid_shape]]\n\tTPU compilation failed\n\t [[tpu_compile_succeeded_assert/_12217194444665189181/_6]]\n```\nPlease give me some advice. Thank you.",
    "840257": "I've run into this same issue on TPUs before. Unfortunately, class weighting doesn't work on TPUs. You'll need to create a weighted loss function.\n\nSee helpful discussion here: https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272",
    "840664": "Hi Dave!\nThank you for the information.\nI implemented the weighted loss function with reference to the following, and it works without error on TPU.\n* https://www.kaggle.com/c/flower-classification-with-tpus/discussion/130272\n* https://github.com/keras-team/keras/issues/2115\n\nI'm checking if it's working properly ...\nIn any case, thank you.",
    "844993": "Thanks for posting this!  Found this helpful 👍",
    "847625": "Hi Matt!\nIt was able to run without error. However, the expected effect has not been obtained. My model may not be good, but originally, the data of this competition has a poor identification rate, so I think it may be better to use the same number of data for each class."
  },
  "source": "meta"
}