{
  "id": 215479,
  "title": "Writing a custom learning rate scheduler in TF",
  "url": "/competitions/cassava-leaf-disease-classification/discussion/215479",
  "author_name": "",
  "post_date": "2021-01-30T04:47:31.167222900Z",
  "votes": 3,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Hi everyone, I would like to implement ReduceLRonPlateau in my custom training loop in TF. I know how to implement it in model.fit(), but it's a bit tricky when implementing it in a custom tf.data pipeline. </p>\n<p>I'm not sure how to create a feedback loop that reads my validation accuracy and then adjusts the learning rate after, given that the learning rate is predefined in the optimizer and if I try to pass a schedule with the variable that defines my validation accuracy, it doesn't work since the validation accuracy has not yet been called. So I'm stuck in sort of a catch-22. </p>\n<p>Any help would be greatly appreciated, thanks!</p>",
  "messages": [
    {
      "id": "1177129",
      "postDate": "01/30/2021 04:47:31",
      "content": "<p>Hi everyone, I would like to implement ReduceLRonPlateau in my custom training loop in TF. I know how to implement it in model.fit(), but it's a bit tricky when implementing it in a custom tf.data pipeline. </p>\n<p>I'm not sure how to create a feedback loop that reads my validation accuracy and then adjusts the learning rate after, given that the learning rate is predefined in the optimizer and if I try to pass a schedule with the variable that defines my validation accuracy, it doesn't work since the validation accuracy has not yet been called. So I'm stuck in sort of a catch-22. </p>\n<p>Any help would be greatly appreciated, thanks!</p>",
      "rawMarkdown": "Hi everyone, I would like to implement ReduceLRonPlateau in my custom training loop in TF. I know how to implement it in model.fit(), but it's a bit tricky when implementing it in a custom tf.data pipeline. \n\nI'm not sure how to create a feedback loop that reads my validation accuracy and then adjusts the learning rate after, given that the learning rate is predefined in the optimizer and if I try to pass a schedule with the variable that defines my validation accuracy, it doesn't work since the validation accuracy has not yet been called. So I'm stuck in sort of a catch-22. \n\nAny help would be greatly appreciated, thanks!",
      "votes": null
    },
    {
      "id": "1177466",
      "postDate": "01/30/2021 09:54:31",
      "content": "<p>Hello!</p>\n<p>I'm not pretty sure about the exact implementation, but after a peer at the Keras <strong><a href=\"https://github.com/keras-team/keras/blob/master/keras/callbacks.py\" target=\"_blank\">callbacks.py</a></strong> I'd suggest the following:<br>\n<code>train_steps(train_dataset)</code><br>\n<code>validation_steps(validation_dataset)</code><br>\n<code># evaluating model -&gt; score -&gt; patience_count</code><br>\n<code>if patience &gt;= max_patience: tf.keras.backend.set_value(model.optimizer.lr, new_lr)</code><br>\nAlso consider looking through this <strong><a href=\"https://www.kaggle.com/dimitreoliveira/cassava-leaf-disease-training-with-tpu-v2-pods\" target=\"_blank\">notebook</a></strong> with a custom TensorFlow loop and the EarlyStopping Callback hard-coded in pretty the same way.</p>",
      "rawMarkdown": "Hello!\n\nI'm not pretty sure about the exact implementation, but after a peer at the Keras **[callbacks.py](https://github.com/keras-team/keras/blob/master/keras/callbacks.py)** I'd suggest the following:\n`train_steps(train_dataset)`\n`validation_steps(validation_dataset)`\n`# evaluating model -> score -> patience_count`\n`if patience >= max_patience: tf.keras.backend.set_value(model.optimizer.lr, new_lr)`\nAlso consider looking through this **[notebook](https://www.kaggle.com/dimitreoliveira/cassava-leaf-disease-training-with-tpu-v2-pods)** with a custom TensorFlow loop and the EarlyStopping Callback hard-coded in pretty the same way.",
      "votes": null
    },
    {
      "id": "1177580",
      "postDate": "01/30/2021 11:48:06",
      "content": "<p>Thanks! I'll give it a try.</p>",
      "rawMarkdown": "Thanks! I'll give it a try.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1177466,
      "author_name": "nickuzmenkov",
      "author_url": "",
      "post_date": "01/30/2021 09:54:31",
      "content": "<p>Hello!</p>\n<p>I'm not pretty sure about the exact implementation, but after a peer at the Keras <strong><a href=\"https://github.com/keras-team/keras/blob/master/keras/callbacks.py\" target=\"_blank\">callbacks.py</a></strong> I'd suggest the following:<br>\n<code>train_steps(train_dataset)</code><br>\n<code>validation_steps(validation_dataset)</code><br>\n<code># evaluating model -&gt; score -&gt; patience_count</code><br>\n<code>if patience &gt;= max_patience: tf.keras.backend.set_value(model.optimizer.lr, new_lr)</code><br>\nAlso consider looking through this <strong><a href=\"https://www.kaggle.com/dimitreoliveira/cassava-leaf-disease-training-with-tpu-v2-pods\" target=\"_blank\">notebook</a></strong> with a custom TensorFlow loop and the EarlyStopping Callback hard-coded in pretty the same way.</p>",
      "votes": null,
      "replies": [
        {
          "id": 1177580,
          "author_name": "junyingsg",
          "author_url": "",
          "post_date": "01/30/2021 11:48:06",
          "content": "<p>Thanks! I'll give it a try.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1177129": "Hi everyone, I would like to implement ReduceLRonPlateau in my custom training loop in TF. I know how to implement it in model.fit(), but it's a bit tricky when implementing it in a custom tf.data pipeline. \n\nI'm not sure how to create a feedback loop that reads my validation accuracy and then adjusts the learning rate after, given that the learning rate is predefined in the optimizer and if I try to pass a schedule with the variable that defines my validation accuracy, it doesn't work since the validation accuracy has not yet been called. So I'm stuck in sort of a catch-22. \n\nAny help would be greatly appreciated, thanks!",
    "1177466": "Hello!\n\nI'm not pretty sure about the exact implementation, but after a peer at the Keras **[callbacks.py](https://github.com/keras-team/keras/blob/master/keras/callbacks.py)** I'd suggest the following:\n`train_steps(train_dataset)`\n`validation_steps(validation_dataset)`\n`# evaluating model -> score -> patience_count`\n`if patience >= max_patience: tf.keras.backend.set_value(model.optimizer.lr, new_lr)`\nAlso consider looking through this **[notebook](https://www.kaggle.com/dimitreoliveira/cassava-leaf-disease-training-with-tpu-v2-pods)** with a custom TensorFlow loop and the EarlyStopping Callback hard-coded in pretty the same way.",
    "1177580": "Thanks! I'll give it a try."
  },
  "source": "meta"
}