{
  "id": 196115,
  "title": "Using LRFinder with the AgentDataset",
  "url": "/competitions/lyft-motion-prediction-autonomous-vehicles/discussion/196115",
  "author_name": "",
  "post_date": "2020-11-09T12:56:57.618617100Z",
  "votes": null,
  "comment_count": 4,
  "views": 0,
  "content": "<p>I am trying to use the <code>LRFinder</code> function to determine the <code>lr_scheduler</code> optimal range with this code:</p>\n<pre><code>from torch_lr_finder import LRFinder\n\ncriterion = pytorch_neg_multi_log_likelihood_batch\noptimizer = optim.AdamW(model.parameters())\nlr_finder = LRFinder(model, optimizer, forward, device)\nlr_finder.range_test(train_loader, end_lr=10, num_iter=1000)\n</code></pre>\n<p>And I got this error:</p>\n<pre><code>---------------------------------------------------------------------------\nValueError                                Traceback (most recent call last)\n&lt;ipython-input-22-d6535fcb2010&gt; in &lt;module&gt;\n----&gt; 1 lr_finder.range_test(train_loader_for_LRF, end_lr=10, num_iter=1000)\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in range_test(self, train_loader, val_loader, start_lr, end_lr, num_iter, step_mode, smooth_f, diverge_th, accumulation_steps, non_blocking_transfer)\n    315         for iteration in tqdm(range(num_iter)):\n    316             # Train on batch and retrieve loss\n--&gt; 317             loss = self._train_batch(\n    318                 train_iter,\n    319                 accumulation_steps,\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in _train_batch(self, train_iter, accumulation_steps, non_blocking_transfer)\n    369         self.optimizer.zero_grad()\n    370         for i in range(accumulation_steps):\n--&gt; 371             inputs, labels = next(train_iter)\n    372             inputs, labels = self._move_to_device(\n    373                 inputs, labels, non_blocking=non_blocking_transfer\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in __next__(self)\n     57         try:\n     58             batch = next(self._iterator)\n---&gt; 59             inputs, labels = self.inputs_labels_from_batch(batch)\n     60         except StopIteration:\n     61             if not self.auto_reset:\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in inputs_labels_from_batch(self, batch_data)\n     31     def inputs_labels_from_batch(self, batch_data):\n     32         if not isinstance(batch_data, list) and not isinstance(batch_data, tuple):\n---&gt; 33             raise ValueError(\n     34                 \"Your batch type is not supported: {}. Please inherit from \"\n     35                 \"`TrainDataLoaderIter` or `ValDataLoaderIter` and override the \"\n\nValueError: Your batch type is not supported: &lt;class 'dict'&gt;. Please inherit from `TrainDataLoaderIter` or `ValDataLoaderIter` and override the `inputs_labels_from_batch` method.\n</code></pre>\n<p>The <code>pytorch_neg_multi_log_likelihood_batch</code> and the <code>train_loader</code> are taken from the <a href=\"https://www.kaggle.com/huanvo/lyft-complete-train-and-prediction-pipeline\" target=\"_blank\">Lyft: Complete train and prediction pipeline notebook</a></p>\n<p>So in short <code>Your batch type is not supported: &lt;class 'dict'&gt;.</code> which make sense, but how can I convert the <code>AgentDataset</code> output, that I could use the <code>LRFinder</code>?</p>\n<p>Thanks for help!</p>",
  "messages": [
    {
      "id": "1073348",
      "postDate": "11/09/2020 12:56:57",
      "content": "<p>I am trying to use the <code>LRFinder</code> function to determine the <code>lr_scheduler</code> optimal range with this code:</p>\n<pre><code>from torch_lr_finder import LRFinder\n\ncriterion = pytorch_neg_multi_log_likelihood_batch\noptimizer = optim.AdamW(model.parameters())\nlr_finder = LRFinder(model, optimizer, forward, device)\nlr_finder.range_test(train_loader, end_lr=10, num_iter=1000)\n</code></pre>\n<p>And I got this error:</p>\n<pre><code>---------------------------------------------------------------------------\nValueError                                Traceback (most recent call last)\n&lt;ipython-input-22-d6535fcb2010&gt; in &lt;module&gt;\n----&gt; 1 lr_finder.range_test(train_loader_for_LRF, end_lr=10, num_iter=1000)\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in range_test(self, train_loader, val_loader, start_lr, end_lr, num_iter, step_mode, smooth_f, diverge_th, accumulation_steps, non_blocking_transfer)\n    315         for iteration in tqdm(range(num_iter)):\n    316             # Train on batch and retrieve loss\n--&gt; 317             loss = self._train_batch(\n    318                 train_iter,\n    319                 accumulation_steps,\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in _train_batch(self, train_iter, accumulation_steps, non_blocking_transfer)\n    369         self.optimizer.zero_grad()\n    370         for i in range(accumulation_steps):\n--&gt; 371             inputs, labels = next(train_iter)\n    372             inputs, labels = self._move_to_device(\n    373                 inputs, labels, non_blocking=non_blocking_transfer\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in __next__(self)\n     57         try:\n     58             batch = next(self._iterator)\n---&gt; 59             inputs, labels = self.inputs_labels_from_batch(batch)\n     60         except StopIteration:\n     61             if not self.auto_reset:\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in inputs_labels_from_batch(self, batch_data)\n     31     def inputs_labels_from_batch(self, batch_data):\n     32         if not isinstance(batch_data, list) and not isinstance(batch_data, tuple):\n---&gt; 33             raise ValueError(\n     34                 \"Your batch type is not supported: {}. Please inherit from \"\n     35                 \"`TrainDataLoaderIter` or `ValDataLoaderIter` and override the \"\n\nValueError: Your batch type is not supported: &lt;class 'dict'&gt;. Please inherit from `TrainDataLoaderIter` or `ValDataLoaderIter` and override the `inputs_labels_from_batch` method.\n</code></pre>\n<p>The <code>pytorch_neg_multi_log_likelihood_batch</code> and the <code>train_loader</code> are taken from the <a href=\"https://www.kaggle.com/huanvo/lyft-complete-train-and-prediction-pipeline\" target=\"_blank\">Lyft: Complete train and prediction pipeline notebook</a></p>\n<p>So in short <code>Your batch type is not supported: &lt;class 'dict'&gt;.</code> which make sense, but how can I convert the <code>AgentDataset</code> output, that I could use the <code>LRFinder</code>?</p>\n<p>Thanks for help!</p>",
      "rawMarkdown": "I am trying to use the `LRFinder` function to determine the `lr_scheduler` optimal range with this code:\n```\nfrom torch_lr_finder import LRFinder\n\ncriterion = pytorch_neg_multi_log_likelihood_batch\noptimizer = optim.AdamW(model.parameters())\nlr_finder = LRFinder(model, optimizer, forward, device)\nlr_finder.range_test(train_loader, end_lr=10, num_iter=1000)\n```\n\nAnd I got this error:\n```\n---------------------------------------------------------------------------\nValueError                                Traceback (most recent call last)\n<ipython-input-22-d6535fcb2010> in <module>\n----> 1 lr_finder.range_test(train_loader_for_LRF, end_lr=10, num_iter=1000)\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in range_test(self, train_loader, val_loader, start_lr, end_lr, num_iter, step_mode, smooth_f, diverge_th, accumulation_steps, non_blocking_transfer)\n    315         for iteration in tqdm(range(num_iter)):\n    316             # Train on batch and retrieve loss\n--> 317             loss = self._train_batch(\n    318                 train_iter,\n    319                 accumulation_steps,\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in _train_batch(self, train_iter, accumulation_steps, non_blocking_transfer)\n    369         self.optimizer.zero_grad()\n    370         for i in range(accumulation_steps):\n--> 371             inputs, labels = next(train_iter)\n    372             inputs, labels = self._move_to_device(\n    373                 inputs, labels, non_blocking=non_blocking_transfer\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in __next__(self)\n     57         try:\n     58             batch = next(self._iterator)\n---> 59             inputs, labels = self.inputs_labels_from_batch(batch)\n     60         except StopIteration:\n     61             if not self.auto_reset:\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in inputs_labels_from_batch(self, batch_data)\n     31     def inputs_labels_from_batch(self, batch_data):\n     32         if not isinstance(batch_data, list) and not isinstance(batch_data, tuple):\n---> 33             raise ValueError(\n     34                 \"Your batch type is not supported: {}. Please inherit from \"\n     35                 \"`TrainDataLoaderIter` or `ValDataLoaderIter` and override the \"\n\nValueError: Your batch type is not supported: <class 'dict'>. Please inherit from `TrainDataLoaderIter` or `ValDataLoaderIter` and override the `inputs_labels_from_batch` method.\n```\nThe `pytorch_neg_multi_log_likelihood_batch` and the `train_loader` are taken from the [Lyft: Complete train and prediction pipeline notebook](https://www.kaggle.com/huanvo/lyft-complete-train-and-prediction-pipeline)\n\nSo in short ` Your batch type is not supported: <class 'dict'>.` which make sense, but how can I convert the `AgentDataset` output, that I could use the `LRFinder`?\n\nThanks for help!",
      "votes": null
    },
    {
      "id": "1073519",
      "postDate": "11/09/2020 15:34:25",
      "content": "<p>I'd say the best way would be to inherit from it and replace the getitem such that instead of a dict it gives you something else (I guess a list is what this function wants). You will likely have to change the model also if you're using the one we provide.</p>",
      "rawMarkdown": "I'd say the best way would be to inherit from it and replace the getitem such that instead of a dict it gives you something else (I guess a list is what this function wants). You will likely have to change the model also if you're using the one we provide.",
      "votes": null
    },
    {
      "id": "1074077",
      "postDate": "11/10/2020 08:03:24",
      "content": "<p>There is TrainDataLoaderIter class in the lr_finder package, which can be used to modify the output of your dataset just for LRFinder. </p>\n<p>In my case (I have a bit different pipeline than one in the public notebook) it looks like that. </p>\n<pre><code>from torch_lr_finder import LRFinder, TrainDataLoaderIter\n[...]\ntrain_dataloader = [...] # initialize your dataloader\nclass TrainIter(TrainDataLoaderIter):\n    def inputs_labels_from_batch(self, batch_data):\n        agents = batch_data\n        image = batch_data[\"image\"]\n        agent_state = batch_get_agent_state(batch_data)\n\n        targets = batch_data['target_positions']\n        avails = batch_data['target_availabilities']\n        return [(image, agent_state), (targets, avails)] # returns [input, targets]\n\ntrain_data_iter = TrainIter(train_dataloader)\n[...] # model initialization, optimizer, LRFinder etc.\n</code></pre>\n<p>However, I found out that LRFinder overstates the best learning rate and it does yield a pretty plot only at the beginning of the training. In later stages, it's useless for me. </p>",
      "rawMarkdown": "There is TrainDataLoaderIter class in the lr_finder package, which can be used to modify the output of your dataset just for LRFinder. \n\nIn my case (I have a bit different pipeline than one in the public notebook) it looks like that. \n\n```\nfrom torch_lr_finder import LRFinder, TrainDataLoaderIter\n[...]\ntrain_dataloader = [...] # initialize your dataloader\nclass TrainIter(TrainDataLoaderIter):\n    def inputs_labels_from_batch(self, batch_data):\n        agents = batch_data\n        image = batch_data[\"image\"]\n        agent_state = batch_get_agent_state(batch_data)\n\n        targets = batch_data['target_positions']\n        avails = batch_data['target_availabilities']\n        return [(image, agent_state), (targets, avails)] # returns [input, targets]\n\ntrain_data_iter = TrainIter(train_dataloader)\n[...] # model initialization, optimizer, LRFinder etc.\n```\n\nHowever, I found out that LRFinder overstates the best learning rate and it does yield a pretty plot only at the beginning of the training. In later stages, it's useless for me.",
      "votes": null
    },
    {
      "id": "1074093",
      "postDate": "11/10/2020 08:35:26",
      "content": "<p>Thank you <a href=\"https://www.kaggle.com/szacho\" target=\"_blank\">@szacho</a>, this is really helpful. What is the <code>batch_get_agent_state()</code> content?</p>",
      "rawMarkdown": "Thank you @szacho, this is really helpful. What is the `batch_get_agent_state()` content?",
      "votes": null
    },
    {
      "id": "1074101",
      "postDate": "11/10/2020 08:47:08",
      "content": "<p>That's just my custom function that takes some additional input for the model, for the public notebook you only need an image</p>",
      "rawMarkdown": "That's just my custom function that takes some additional input for the model, for the public notebook you only need an image",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1073519,
      "author_name": "lucabergamini",
      "author_url": "",
      "post_date": "11/09/2020 15:34:25",
      "content": "<p>I'd say the best way would be to inherit from it and replace the getitem such that instead of a dict it gives you something else (I guess a list is what this function wants). You will likely have to change the model also if you're using the one we provide.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1074077,
      "author_name": "szacho",
      "author_url": "",
      "post_date": "11/10/2020 08:03:24",
      "content": "<p>There is TrainDataLoaderIter class in the lr_finder package, which can be used to modify the output of your dataset just for LRFinder. </p>\n<p>In my case (I have a bit different pipeline than one in the public notebook) it looks like that. </p>\n<pre><code>from torch_lr_finder import LRFinder, TrainDataLoaderIter\n[...]\ntrain_dataloader = [...] # initialize your dataloader\nclass TrainIter(TrainDataLoaderIter):\n    def inputs_labels_from_batch(self, batch_data):\n        agents = batch_data\n        image = batch_data[\"image\"]\n        agent_state = batch_get_agent_state(batch_data)\n\n        targets = batch_data['target_positions']\n        avails = batch_data['target_availabilities']\n        return [(image, agent_state), (targets, avails)] # returns [input, targets]\n\ntrain_data_iter = TrainIter(train_dataloader)\n[...] # model initialization, optimizer, LRFinder etc.\n</code></pre>\n<p>However, I found out that LRFinder overstates the best learning rate and it does yield a pretty plot only at the beginning of the training. In later stages, it's useless for me. </p>",
      "votes": null,
      "replies": [
        {
          "id": 1074093,
          "author_name": "bessenyeiszilrd",
          "author_url": "",
          "post_date": "11/10/2020 08:35:26",
          "content": "<p>Thank you <a href=\"https://www.kaggle.com/szacho\" target=\"_blank\">@szacho</a>, this is really helpful. What is the <code>batch_get_agent_state()</code> content?</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1074101,
          "author_name": "szacho",
          "author_url": "",
          "post_date": "11/10/2020 08:47:08",
          "content": "<p>That's just my custom function that takes some additional input for the model, for the public notebook you only need an image</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1073348": "I am trying to use the `LRFinder` function to determine the `lr_scheduler` optimal range with this code:\n```\nfrom torch_lr_finder import LRFinder\n\ncriterion = pytorch_neg_multi_log_likelihood_batch\noptimizer = optim.AdamW(model.parameters())\nlr_finder = LRFinder(model, optimizer, forward, device)\nlr_finder.range_test(train_loader, end_lr=10, num_iter=1000)\n```\n\nAnd I got this error:\n```\n---------------------------------------------------------------------------\nValueError                                Traceback (most recent call last)\n<ipython-input-22-d6535fcb2010> in <module>\n----> 1 lr_finder.range_test(train_loader_for_LRF, end_lr=10, num_iter=1000)\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in range_test(self, train_loader, val_loader, start_lr, end_lr, num_iter, step_mode, smooth_f, diverge_th, accumulation_steps, non_blocking_transfer)\n    315         for iteration in tqdm(range(num_iter)):\n    316             # Train on batch and retrieve loss\n--> 317             loss = self._train_batch(\n    318                 train_iter,\n    319                 accumulation_steps,\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in _train_batch(self, train_iter, accumulation_steps, non_blocking_transfer)\n    369         self.optimizer.zero_grad()\n    370         for i in range(accumulation_steps):\n--> 371             inputs, labels = next(train_iter)\n    372             inputs, labels = self._move_to_device(\n    373                 inputs, labels, non_blocking=non_blocking_transfer\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in __next__(self)\n     57         try:\n     58             batch = next(self._iterator)\n---> 59             inputs, labels = self.inputs_labels_from_batch(batch)\n     60         except StopIteration:\n     61             if not self.auto_reset:\n\n~/anaconda3/envs/lyft_env/lib/python3.8/site-packages/torch_lr_finder/lr_finder.py in inputs_labels_from_batch(self, batch_data)\n     31     def inputs_labels_from_batch(self, batch_data):\n     32         if not isinstance(batch_data, list) and not isinstance(batch_data, tuple):\n---> 33             raise ValueError(\n     34                 \"Your batch type is not supported: {}. Please inherit from \"\n     35                 \"`TrainDataLoaderIter` or `ValDataLoaderIter` and override the \"\n\nValueError: Your batch type is not supported: <class 'dict'>. Please inherit from `TrainDataLoaderIter` or `ValDataLoaderIter` and override the `inputs_labels_from_batch` method.\n```\nThe `pytorch_neg_multi_log_likelihood_batch` and the `train_loader` are taken from the [Lyft: Complete train and prediction pipeline notebook](https://www.kaggle.com/huanvo/lyft-complete-train-and-prediction-pipeline)\n\nSo in short ` Your batch type is not supported: <class 'dict'>.` which make sense, but how can I convert the `AgentDataset` output, that I could use the `LRFinder`?\n\nThanks for help!",
    "1073519": "I'd say the best way would be to inherit from it and replace the getitem such that instead of a dict it gives you something else (I guess a list is what this function wants). You will likely have to change the model also if you're using the one we provide.",
    "1074077": "There is TrainDataLoaderIter class in the lr_finder package, which can be used to modify the output of your dataset just for LRFinder. \n\nIn my case (I have a bit different pipeline than one in the public notebook) it looks like that. \n\n```\nfrom torch_lr_finder import LRFinder, TrainDataLoaderIter\n[...]\ntrain_dataloader = [...] # initialize your dataloader\nclass TrainIter(TrainDataLoaderIter):\n    def inputs_labels_from_batch(self, batch_data):\n        agents = batch_data\n        image = batch_data[\"image\"]\n        agent_state = batch_get_agent_state(batch_data)\n\n        targets = batch_data['target_positions']\n        avails = batch_data['target_availabilities']\n        return [(image, agent_state), (targets, avails)] # returns [input, targets]\n\ntrain_data_iter = TrainIter(train_dataloader)\n[...] # model initialization, optimizer, LRFinder etc.\n```\n\nHowever, I found out that LRFinder overstates the best learning rate and it does yield a pretty plot only at the beginning of the training. In later stages, it's useless for me.",
    "1074093": "Thank you @szacho, this is really helpful. What is the `batch_get_agent_state()` content?",
    "1074101": "That's just my custom function that takes some additional input for the model, for the public notebook you only need an image"
  },
  "source": "meta"
}