{
  "id": 133857,
  "title": "For those using Colab (Pro): Preventing Session from disconnecting due to inactivity",
  "url": "/competitions/bengaliai-cv19/discussion/133857",
  "author_name": "Maxime Lenormand",
  "post_date": "2020-03-04T16:39:38.268000",
  "votes": 34,
  "comment_count": 32,
  "views": 0,
  "content": "<p>Hello!</p>\n\n<p>As someone without access to a decent GPU, and doing Kaggle only from my relatively low spec laptop, Colab Pro was great to give access to 24h long sessions of compute on a P100, great for this competition!</p>\n\n<p>But, one needs to stay active on the tab, otherwise your session is disconnected and you basically loose everything you were doing. Saving weights becomes <em>really</em> important, but that doesn't allow you to run your notebook all night, or all day if you're away form your computer.</p>\n\n<p>There are some hacks to do that, obviously.</p>\n\n<p>The first one I found was a Javascript script to run in the browser:</p>\n\n<p>Hit Ctrl + Shift + I and add this in the console:\n<code>function ClickConnect(){\nconsole.log(\"Working\"); \ndocument.querySelector(\"colab-toolbar-button#connect\").click() \n}\nsetInterval(ClickConnect,60000)</code></p>\n\n<p>This will basically click to reconnect the notebook every 6 seconds.</p>\n\n<p>However, after trying this, the notebook just adds random empty cells either in the middle or at the end of my notebook, like so:</p>\n\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2605845%2Feeafb8361dc24bc824caa5958f464bc9%2FScreenshot%20from%202020-03-04%2017-30-37.png?generation=1583339454155514&amp;alt=media\" alt=\"\"></p>\n\n<p>I also tried using a <a href=\"https://chrome.google.com/webstore/detail/colab-auto-reconnect/nbcihfbfamjlfiopdcemmohoojdecjid\">Chrome add-on</a> but that also just didn't prevent Colab from telling me it's gonna kick me out after 90 minutes, and doing so.</p>\n\n<p>Since then, I decided to take things in my own hands, and thought some of you might find this interesting and helpful. I made a small python 3 script that I just run that randomly moves the mouse back and forth and clicks, always on the same place, but that's enough to fool Colab into thinking I'm active on the notebook and not disconnect:</p>\n\n<p><code>import numpy as np</code>\n<code>import time</code>\n<code>import mouse</code></p>\n\n<p><code>while True:</code>\n    <code>random_row = np.random.random_sample()*100</code>\n    <code>random_col = np.random.random_sample()*10</code>\n    <code>random_time = np.random.random_sample()*np.random.random_sample() * 100</code>\n    <code>mouse.wheel(1000)</code>\n    <code>mouse.wheel(-1000)</code>\n    <code>mouse.move(random_row, random_col, absolute=False, duration=0.2)</code>\n    <code>mouse.move(-random_row, -random_col, absolute=False, duration = 0.2)</code>\n    <code>mouse.LEFT</code>\n    <code>time.sleep(random_time)</code></p>\n\n<p>You just need to run it as <code>sudo</code> (as it takes control of the mouse) and it should work, allowing you to take full advantage of Colab's 12h sessions (or 24h for the Pro tier).</p>\n\n<p>Hope that helps!</p>",
  "messages": [
    {
      "id": 763596,
      "postDate": "2020-03-04T16:39:38.270Z",
      "content": "<p>Hello!</p>\n\n<p>As someone without access to a decent GPU, and doing Kaggle only from my relatively low spec laptop, Colab Pro was great to give access to 24h long sessions of compute on a P100, great for this competition!</p>\n\n<p>But, one needs to stay active on the tab, otherwise your session is disconnected and you basically loose everything you were doing. Saving weights becomes <em>really</em> important, but that doesn't allow you to run your notebook all night, or all day if you're away form your computer.</p>\n\n<p>There are some hacks to do that, obviously.</p>\n\n<p>The first one I found was a Javascript script to run in the browser:</p>\n\n<p>Hit Ctrl + Shift + I and add this in the console:\n<code>function ClickConnect(){\nconsole.log(\"Working\"); \ndocument.querySelector(\"colab-toolbar-button#connect\").click() \n}\nsetInterval(ClickConnect,60000)</code></p>\n\n<p>This will basically click to reconnect the notebook every 6 seconds.</p>\n\n<p>However, after trying this, the notebook just adds random empty cells either in the middle or at the end of my notebook, like so:</p>\n\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2605845%2Feeafb8361dc24bc824caa5958f464bc9%2FScreenshot%20from%202020-03-04%2017-30-37.png?generation=1583339454155514&amp;alt=media\" alt=\"\"></p>\n\n<p>I also tried using a <a href=\"https://chrome.google.com/webstore/detail/colab-auto-reconnect/nbcihfbfamjlfiopdcemmohoojdecjid\">Chrome add-on</a> but that also just didn't prevent Colab from telling me it's gonna kick me out after 90 minutes, and doing so.</p>\n\n<p>Since then, I decided to take things in my own hands, and thought some of you might find this interesting and helpful. I made a small python 3 script that I just run that randomly moves the mouse back and forth and clicks, always on the same place, but that's enough to fool Colab into thinking I'm active on the notebook and not disconnect:</p>\n\n<p><code>import numpy as np</code>\n<code>import time</code>\n<code>import mouse</code></p>\n\n<p><code>while True:</code>\n    <code>random_row = np.random.random_sample()*100</code>\n    <code>random_col = np.random.random_sample()*10</code>\n    <code>random_time = np.random.random_sample()*np.random.random_sample() * 100</code>\n    <code>mouse.wheel(1000)</code>\n    <code>mouse.wheel(-1000)</code>\n    <code>mouse.move(random_row, random_col, absolute=False, duration=0.2)</code>\n    <code>mouse.move(-random_row, -random_col, absolute=False, duration = 0.2)</code>\n    <code>mouse.LEFT</code>\n    <code>time.sleep(random_time)</code></p>\n\n<p>You just need to run it as <code>sudo</code> (as it takes control of the mouse) and it should work, allowing you to take full advantage of Colab's 12h sessions (or 24h for the Pro tier).</p>\n\n<p>Hope that helps!</p>",
      "rawMarkdown": "Hello!\n\nAs someone without access to a decent GPU, and doing Kaggle only from my relatively low spec laptop, Colab Pro was great to give access to 24h long sessions of compute on a P100, great for this competition!\n\nBut, one needs to stay active on the tab, otherwise your session is disconnected and you basically loose everything you were doing. Saving weights becomes _really_ important, but that doesn't allow you to run your notebook all night, or all day if you're away form your computer.\n\nThere are some hacks to do that, obviously.\n\nThe first one I found was a Javascript script to run in the browser:\n\nHit Ctrl + Shift + I and add this in the console:\n`function ClickConnect(){\nconsole.log(\"Working\"); \ndocument.querySelector(\"colab-toolbar-button#connect\").click() \n}\nsetInterval(ClickConnect,60000)`\n\nThis will basically click to reconnect the notebook every 6 seconds.\n\nHowever, after trying this, the notebook just adds random empty cells either in the middle or at the end of my notebook, like so:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2605845%2Feeafb8361dc24bc824caa5958f464bc9%2FScreenshot%20from%202020-03-04%2017-30-37.png?generation=1583339454155514&amp;alt=media)\n\nI also tried using a [Chrome add-on](https://chrome.google.com/webstore/detail/colab-auto-reconnect/nbcihfbfamjlfiopdcemmohoojdecjid) but that also just didn't prevent Colab from telling me it's gonna kick me out after 90 minutes, and doing so.\n\nSince then, I decided to take things in my own hands, and thought some of you might find this interesting and helpful. I made a small python 3 script that I just run that randomly moves the mouse back and forth and clicks, always on the same place, but that's enough to fool Colab into thinking I'm active on the notebook and not disconnect:\n\n\n`import numpy as np`\n`import time`\n`import mouse`\n\n`while True:`\n\t`random_row = np.random.random_sample()*100`\n\t`random_col = np.random.random_sample()*10`\n\t`random_time = np.random.random_sample()*np.random.random_sample() * 100`\n\t`mouse.wheel(1000)`\n\t`mouse.wheel(-1000)`\n\t`mouse.move(random_row, random_col, absolute=False, duration=0.2)`\n\t`mouse.move(-random_row, -random_col, absolute=False, duration = 0.2)`\n\t`mouse.LEFT`\n\t`time.sleep(random_time)`\n\n\nYou just need to run it as `sudo` (as it takes control of the mouse) and it should work, allowing you to take full advantage of Colab's 12h sessions (or 24h for the Pro tier).\n\nHope that helps!",
      "votes": 34
    },
    {
      "id": 764692,
      "postDate": "2020-03-05T18:36:22.113Z",
      "content": "<p>This actually works (confirmed):</p>\n\n<blockquote>\n  <p>function ClickConnect(){\n           console.log(\"Clicking\"); \n           document.querySelector(\"colab-connect-button\").click() \n  }\n  setInterval(ClickConnect,60000)</p>\n</blockquote>",
      "rawMarkdown": "This actually works (confirmed):\n&gt; function ClickConnect(){\n         console.log(\"Clicking\"); \n         document.querySelector(\"colab-connect-button\").click() \n}\nsetInterval(ClickConnect,60000)",
      "votes": 9
    },
    {
      "id": 768369,
      "postDate": "2020-03-10T17:42:08.313Z",
      "content": "<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F1649622%2F9710aad78000e5f903c4b32b37f60895%2Fcolab.jpg?generation=1583862014401618&amp;alt=media\" alt=\"\">\nHaha my situation every night. Anyway i tried javascript approach didnt worked.Now i am gonna try your method <a href=\"/maxlenormand\">@maxlenormand</a> . Thank you 😃 </p>",
      "rawMarkdown": "![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F1649622%2F9710aad78000e5f903c4b32b37f60895%2Fcolab.jpg?generation=1583862014401618&amp;alt=media)\nHaha my situation every night. Anyway i tried javascript approach didnt worked.Now i am gonna try your method @maxlenormand . Thank you 😃 ",
      "votes": 7,
      "replies": [
        {
          "id": 768435,
          "postDate": "2020-03-10T19:39:56.607Z",
          "content": "<p>Oh yeah, I've had a few bad surprises in the morning when waking up and seeing that nothing had happened because of a disconnection.</p>\n\n<p>Hope it helps!</p>",
          "rawMarkdown": "Oh yeah, I've had a few bad surprises in the morning when waking up and seeing that nothing had happened because of a disconnection.\n\nHope it helps!",
          "votes": 2
        }
      ]
    },
    {
      "id": 769558,
      "postDate": "2020-03-12T02:24:09.850Z",
      "content": "<p>For Mac users, the mouse library will not work. You will have to try some other Mouse controller library like <a href=\"https://pypi.org/project/pynput/\">pynput</a> or <a href=\"http://pyautogui.readthedocs.io/en/latest/\">pyAutoGUI</a></p>",
      "rawMarkdown": "For Mac users, the mouse library will not work. You will have to try some other Mouse controller library like [pynput](https://pypi.org/project/pynput/) or [pyAutoGUI](http://pyautogui.readthedocs.io/en/latest/)",
      "votes": 1,
      "replies": [
        {
          "id": 769671,
          "postDate": "2020-03-12T06:14:25.357Z",
          "content": "<p>Oh, good to know! Thanks for pointing that out!</p>",
          "rawMarkdown": "Oh, good to know! Thanks for pointing that out!"
        }
      ]
    },
    {
      "id": 768442,
      "postDate": "2020-03-10T19:52:55.530Z",
      "content": "<p>btw, if you need more RAM you can fill the RAM and kernel crashes and asks you switch to a higher RAM notebook. (Works for non-pro as well)\nJust run\n<code>\na = []\nwhile(1):\n     a.append('-0.0001')\n</code></p>",
      "rawMarkdown": "btw, if you need more RAM you can fill the RAM and kernel crashes and asks you switch to a higher RAM notebook. (Works for non-pro as well)\nJust run\n```\na = []\nwhile(1):\n     a.append('-0.0001')\n```\n",
      "votes": 1,
      "replies": [
        {
          "id": 768445,
          "postDate": "2020-03-10T20:00:52.120Z",
          "content": "<p>I used to use this before the appearance of the pro tier, but does this still work though?</p>\n\n<p>Now in the pro tier, you can choose in the run-time options to have a high-RAM session. I'm thus guessing they stopped providing those to the free tier?</p>",
          "rawMarkdown": "I used to use this before the appearance of the pro tier, but does this still work though?\n\nNow in the pro tier, you can choose in the run-time options to have a high-RAM session. I'm thus guessing they stopped providing those to the free tier?"
        },
        {
          "id": 768771,
          "postDate": "2020-03-11T06:58:18.103Z",
          "content": "<p>Can confirm it works in the free tier. I couldn't load the dataset in the memory but after doing this, I can. </p>",
          "rawMarkdown": "Can confirm it works in the free tier. I couldn't load the dataset in the memory but after doing this, I can. ",
          "votes": 1
        }
      ]
    },
    {
      "id": 763691,
      "postDate": "2020-03-04T18:40:12.357Z",
      "content": "<p>That's a cool hack!</p>\n\n<p>&gt; your session is disconnected and you basically loose everything you were doing.</p>\n\n<p>Two things:\n1. If you don't want to lose everything you have been doing, connect your notebook to Google Drive and save your weights after each epoch. You can then load them and continue training in your next session.</p>\n\n<ol>\n<li>your session gets disconnected but not necessarily terminated. Even if you get disconnected, your session keeps running on the cloud until something else happens e.g. they run out of compute and terminate your session to allocate it to other users or you reach the end of your quota. </li>\n</ol>",
      "rawMarkdown": "That's a cool hack!\n\n&gt; your session is disconnected and you basically loose everything you were doing.\n\nTwo things:\n1. If you don't want to lose everything you have been doing, connect your notebook to Google Drive and save your weights after each epoch. You can then load them and continue training in your next session.\n\n2. your session gets disconnected but not necessarily terminated. Even if you get disconnected, your session keeps running on the cloud until something else happens e.g. they run out of compute and terminate your session to allocate it to other users or you reach the end of your quota. ",
      "votes": 2,
      "replies": [
        {
          "id": 763727,
          "postDate": "2020-03-04T19:35:27.043Z",
          "content": "<p>Yep, I already work around both! :-) I should probably have mentioned it a little bit more thoroughly in the post. But yes, the weights aren’t lost if you save them. I meant rather that everything opened and in RAM by the notebook is gone. But I wasn’t very clear about that indeed.</p>",
          "rawMarkdown": "Yep, I already work around both! :-) I should probably have mentioned it a little bit more thoroughly in the post. But yes, the weights aren’t lost if you save them. I meant rather that everything opened and in RAM by the notebook is gone. But I wasn’t very clear about that indeed."
        },
        {
          "id": 764449,
          "postDate": "2020-03-05T13:24:25.110Z",
          "content": "<p><a href=\"/mhviraf\">@mhviraf</a> storing weight into google drive is a pretty good solution. Thanks.</p>",
          "rawMarkdown": "@mhviraf storing weight into google drive is a pretty good solution. Thanks."
        }
      ]
    },
    {
      "id": 766815,
      "postDate": "2020-03-08T19:17:08.200Z",
      "content": "<p>Save the model weights to the drive.</p>",
      "rawMarkdown": "Save the model weights to the drive."
    },
    {
      "id": 766737,
      "postDate": "2020-03-08T16:13:01.387Z",
      "content": "<p>Thank you for your sharing, but how to use your script? \nIt seems like I need to run your script in my local computer?</p>",
      "rawMarkdown": "Thank you for your sharing, but how to use your script? \nIt seems like I need to run your script in my local computer?",
      "replies": [
        {
          "id": 766800,
          "postDate": "2020-03-08T18:35:13.550Z",
          "content": "<p>open chrome developer tools . Copy and paste it into the console.</p>",
          "rawMarkdown": "open chrome developer tools . Copy and paste it into the console.",
          "votes": 1
        },
        {
          "id": 767206,
          "postDate": "2020-03-09T10:00:21.060Z",
          "content": "<p>Sorry that might not have been very clear.</p>\n\n<p>The first script (in JavaScript) you should run in the console after hitting Ctrl + Shift + I.</p>\n\n<p>The one I posted, in Python, should just be run on your local machine yes. I'm on Linux, so it's pretty easy to run it from the command line: <code>sudo python3 script_to_prevent_colab_from_disconnecting.py</code> (as mentioned in the post, sudo is required as this script takes over your mouse movements). But you could just as well run this from an IDE I think.</p>\n\n<p>Hope that helps!</p>",
          "rawMarkdown": "Sorry that might not have been very clear.\n\nThe first script (in JavaScript) you should run in the console after hitting Ctrl + Shift + I.\n\nThe one I posted, in Python, should just be run on your local machine yes. I'm on Linux, so it's pretty easy to run it from the command line: `sudo python3 script_to_prevent_colab_from_disconnecting.py` (as mentioned in the post, sudo is required as this script takes over your mouse movements). But you could just as well run this from an IDE I think.\n\nHope that helps!",
          "votes": 4
        },
        {
          "id": 767283,
          "postDate": "2020-03-09T12:40:52.697Z",
          "content": "<p>Yes, the second script is what I want to asked.\nThank you for your reply.</p>",
          "rawMarkdown": "Yes, the second script is what I want to asked.\nThank you for your reply.\n "
        }
      ]
    },
    {
      "id": 764916,
      "postDate": "2020-03-06T04:35:46.100Z",
      "content": "<p>can colab pro get access to in China?</p>",
      "rawMarkdown": "can colab pro get access to in China?",
      "replies": [
        {
          "id": 765012,
          "postDate": "2020-03-06T07:21:43.473Z",
          "content": "<p>I'm using it in the Netherlands without any issues. But I couldn't tell for China, you're gonna have to try that one for yourself I guess</p>",
          "rawMarkdown": "I'm using it in the Netherlands without any issues. But I couldn't tell for China, you're gonna have to try that one for yourself I guess"
        },
        {
          "id": 765382,
          "postDate": "2020-03-06T14:59:01.580Z",
          "content": "<p>How do you use it from the Netherlands? It's only available in the US no?</p>",
          "rawMarkdown": "How do you use it from the Netherlands? It's only available in the US no?"
        }
      ]
    },
    {
      "id": 763940,
      "postDate": "2020-03-05T02:09:39.340Z",
      "content": "<p>I use <a href=\"https://chrome.google.com/webstore/detail/auto-refresh/ifooldnmmcmlbdennkpdnlnbgbmfalko?hl=en\">Auto Refresh</a></p>",
      "rawMarkdown": "I use [Auto Refresh](https://chrome.google.com/webstore/detail/auto-refresh/ifooldnmmcmlbdennkpdnlnbgbmfalko?hl=en)",
      "replies": [
        {
          "id": 764119,
          "postDate": "2020-03-05T06:41:21.563Z",
          "content": "<p>And that works well?\nSeems like an interesting add-on, thanks!</p>",
          "rawMarkdown": "And that works well?\nSeems like an interesting add-on, thanks!"
        },
        {
          "id": 764473,
          "postDate": "2020-03-05T13:55:50.520Z",
          "content": "<p>Yes, but it needs some techniques.\n1. Auto Refresh set around 60 min.\n2. Push reload button(F5 key), and select \"Cancel\" in dialog once.\n3. Push reload button again, and check \"Prevent this page from creating additional dialogs\", select \"Reload\".\n4. Repeat some times\n5. Then it will not come out dialogs !</p>",
          "rawMarkdown": "Yes, but it needs some techniques.\n1. Auto Refresh set around 60 min.\n2. Push reload button(F5 key), and select \"Cancel\" in dialog once.\n3. Push reload button again, and check \"Prevent this page from creating additional dialogs\", select \"Reload\".\n4. Repeat some times\n5. Then it will not come out dialogs !"
        }
      ]
    },
    {
      "id": 763790,
      "postDate": "2020-03-04T21:26:26.550Z",
      "content": "<p>Lol, I was thinking about this issue too. \nHere's my script:</p>\n\n<p>```\nfunction colabReconnect() {\n    var dialog = document.querySelector(\"colab-dialog.yes-no-dialog\");\n    var dialogTitle = dialog &amp;&amp; dialog.querySelector(\"div.content-area&gt;h2\");</p>\n\n<pre><code>if (dialogTitle &amp;&amp; dialogTitle.innerText == \"Runtime disconnected\") {\n    btn = dialog.querySelector(\"paper-button#ok\");\n    btn.click();\n    console.log(\"Reconnecting...\");\n} else {\n    console.log(\"Everything is ok.\");\n}\n</code></pre>\n\n<p>}</p>\n\n<p>timerId = setInterval(colabReconnect, 10000);\n```</p>",
      "rawMarkdown": "Lol, I was thinking about this issue too. \nHere's my script:\n\n```\nfunction colabReconnect() {\n    var dialog = document.querySelector(\"colab-dialog.yes-no-dialog\");\n    var dialogTitle = dialog &amp;&amp; dialog.querySelector(\"div.content-area&gt;h2\");\n\n    if (dialogTitle &amp;&amp; dialogTitle.innerText == \"Runtime disconnected\") {\n        btn = dialog.querySelector(\"paper-button#ok\");\n        btn.click();\n        console.log(\"Reconnecting...\");\n    } else {\n        console.log(\"Everything is ok.\");\n    }\n}\n\ntimerId = setInterval(colabReconnect, 10000);\n```\n",
      "replies": [
        {
          "id": 764120,
          "postDate": "2020-03-05T06:42:14.183Z",
          "content": "<p>I'm not really familiar with JavaScript, hence doing my own Python script, but seems like we all have our own hacky ways to deal with the issues ;)</p>",
          "rawMarkdown": "I'm not really familiar with JavaScript, hence doing my own Python script, but seems like we all have our own hacky ways to deal with the issues ;)"
        },
        {
          "id": 793416,
          "postDate": "2020-04-01T02:32:16.903Z",
          "content": "<p>Hello, how do I use this script , please? </p>",
          "rawMarkdown": "Hello, how do I use this script , please? "
        },
        {
          "id": 918160,
          "postDate": "2020-07-07T03:21:12.850Z",
          "rawMarkdown": "",
          "isDeleted": true
        }
      ]
    },
    {
      "id": 763713,
      "postDate": "2020-03-04T19:18:34.820Z",
      "content": "<p>I believe the right query is \"colab-connect-button\"</p>",
      "rawMarkdown": "I believe the right query is \"colab-connect-button\"",
      "replies": [
        {
          "id": 763729,
          "postDate": "2020-03-04T19:36:04.483Z",
          "content": "<p>Oh I’ll try that then! Maybe I just didn’t do it correctly and no need for this hacks way of doing it!</p>",
          "rawMarkdown": "Oh I’ll try that then! Maybe I just didn’t do it correctly and no need for this hacks way of doing it!"
        },
        {
          "id": 764393,
          "postDate": "2020-03-05T12:16:09.860Z",
          "content": "<p>From what I read on StackOverflow google changes the name of the button from time to time because they know people are doing it. But you can always inspect and see which button is it.</p>",
          "rawMarkdown": "From what I read on StackOverflow google changes the name of the button from time to time because they know people are doing it. But you can always inspect and see which button is it."
        },
        {
          "id": 764543,
          "postDate": "2020-03-05T15:23:45.680Z",
          "content": "<p>Oh that makes sense! I guess I can stick to my script for now then!</p>",
          "rawMarkdown": "Oh that makes sense! I guess I can stick to my script for now then!"
        }
      ]
    },
    {
      "id": 767216,
      "postDate": "2020-03-09T10:09:47.283Z",
      "rawMarkdown": "",
      "isDeleted": true
    },
    {
      "id": 767288,
      "postDate": "2020-03-09T12:44:15.293Z",
      "content": "<p>Useful trick. Thanks!</p>",
      "rawMarkdown": "Useful trick. Thanks!"
    }
  ],
  "comments": [
    {
      "id": 764692,
      "author_name": "Larry Schuster",
      "author_url": "",
      "post_date": "2020-03-05T18:36:22.113000",
      "content": "<p>This actually works (confirmed):</p>\n\n<blockquote>\n  <p>function ClickConnect(){\n           console.log(\"Clicking\"); \n           document.querySelector(\"colab-connect-button\").click() \n  }\n  setInterval(ClickConnect,60000)</p>\n</blockquote>",
      "votes": 9,
      "replies": []
    },
    {
      "id": 768369,
      "author_name": "ratan rohith",
      "author_url": "",
      "post_date": "2020-03-10T17:42:08.313000",
      "content": "<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F1649622%2F9710aad78000e5f903c4b32b37f60895%2Fcolab.jpg?generation=1583862014401618&amp;alt=media\" alt=\"\">\nHaha my situation every night. Anyway i tried javascript approach didnt worked.Now i am gonna try your method <a href=\"/maxlenormand\">@maxlenormand</a> . Thank you 😃 </p>",
      "votes": 7,
      "replies": [
        {
          "id": 768435,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-10T19:39:56.607000",
          "content": "<p>Oh yeah, I've had a few bad surprises in the morning when waking up and seeing that nothing had happened because of a disconnection.</p>\n\n<p>Hope it helps!</p>",
          "votes": 2,
          "replies": []
        }
      ]
    },
    {
      "id": 769558,
      "author_name": "Rajesh Kumar",
      "author_url": "",
      "post_date": "2020-03-12T02:24:09.850000",
      "content": "<p>For Mac users, the mouse library will not work. You will have to try some other Mouse controller library like <a href=\"https://pypi.org/project/pynput/\">pynput</a> or <a href=\"http://pyautogui.readthedocs.io/en/latest/\">pyAutoGUI</a></p>",
      "votes": 1,
      "replies": [
        {
          "id": 769671,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-12T06:14:25.357000",
          "content": "<p>Oh, good to know! Thanks for pointing that out!</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 768442,
      "author_name": "timetraveller",
      "author_url": "",
      "post_date": "2020-03-10T19:52:55.530000",
      "content": "<p>btw, if you need more RAM you can fill the RAM and kernel crashes and asks you switch to a higher RAM notebook. (Works for non-pro as well)\nJust run\n<code>\na = []\nwhile(1):\n     a.append('-0.0001')\n</code></p>",
      "votes": 1,
      "replies": [
        {
          "id": 768445,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-10T20:00:52.120000",
          "content": "<p>I used to use this before the appearance of the pro tier, but does this still work though?</p>\n\n<p>Now in the pro tier, you can choose in the run-time options to have a high-RAM session. I'm thus guessing they stopped providing those to the free tier?</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 768771,
          "author_name": "timetraveller",
          "author_url": "",
          "post_date": "2020-03-11T06:58:18.103000",
          "content": "<p>Can confirm it works in the free tier. I couldn't load the dataset in the memory but after doing this, I can. </p>",
          "votes": 1,
          "replies": []
        }
      ]
    },
    {
      "id": 763691,
      "author_name": "Massoud Hosseinali",
      "author_url": "",
      "post_date": "2020-03-04T18:40:12.357000",
      "content": "<p>That's a cool hack!</p>\n\n<p>&gt; your session is disconnected and you basically loose everything you were doing.</p>\n\n<p>Two things:\n1. If you don't want to lose everything you have been doing, connect your notebook to Google Drive and save your weights after each epoch. You can then load them and continue training in your next session.</p>\n\n<ol>\n<li>your session gets disconnected but not necessarily terminated. Even if you get disconnected, your session keeps running on the cloud until something else happens e.g. they run out of compute and terminate your session to allocate it to other users or you reach the end of your quota. </li>\n</ol>",
      "votes": 2,
      "replies": [
        {
          "id": 763727,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-04T19:35:27.043000",
          "content": "<p>Yep, I already work around both! :-) I should probably have mentioned it a little bit more thoroughly in the post. But yes, the weights aren’t lost if you save them. I meant rather that everything opened and in RAM by the notebook is gone. But I wasn’t very clear about that indeed.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 764449,
          "author_name": "Innat",
          "author_url": "",
          "post_date": "2020-03-05T13:24:25.110000",
          "content": "<p><a href=\"/mhviraf\">@mhviraf</a> storing weight into google drive is a pretty good solution. Thanks.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 766815,
      "author_name": "Mohammed Nadeem",
      "author_url": "",
      "post_date": "2020-03-08T19:17:08.200000",
      "content": "<p>Save the model weights to the drive.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 766737,
      "author_name": "YS",
      "author_url": "",
      "post_date": "2020-03-08T16:13:01.387000",
      "content": "<p>Thank you for your sharing, but how to use your script? \nIt seems like I need to run your script in my local computer?</p>",
      "votes": 0,
      "replies": [
        {
          "id": 766800,
          "author_name": "garyong",
          "author_url": "",
          "post_date": "2020-03-08T18:35:13.550000",
          "content": "<p>open chrome developer tools . Copy and paste it into the console.</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 767206,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-09T10:00:21.060000",
          "content": "<p>Sorry that might not have been very clear.</p>\n\n<p>The first script (in JavaScript) you should run in the console after hitting Ctrl + Shift + I.</p>\n\n<p>The one I posted, in Python, should just be run on your local machine yes. I'm on Linux, so it's pretty easy to run it from the command line: <code>sudo python3 script_to_prevent_colab_from_disconnecting.py</code> (as mentioned in the post, sudo is required as this script takes over your mouse movements). But you could just as well run this from an IDE I think.</p>\n\n<p>Hope that helps!</p>",
          "votes": 4,
          "replies": []
        },
        {
          "id": 767283,
          "author_name": "YS",
          "author_url": "",
          "post_date": "2020-03-09T12:40:52.697000",
          "content": "<p>Yes, the second script is what I want to asked.\nThank you for your reply.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 764916,
      "author_name": "yy2020",
      "author_url": "",
      "post_date": "2020-03-06T04:35:46.100000",
      "content": "<p>can colab pro get access to in China?</p>",
      "votes": 0,
      "replies": [
        {
          "id": 765012,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-06T07:21:43.473000",
          "content": "<p>I'm using it in the Netherlands without any issues. But I couldn't tell for China, you're gonna have to try that one for yourself I guess</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 765382,
          "author_name": "Oik Dim",
          "author_url": "",
          "post_date": "2020-03-06T14:59:01.580000",
          "content": "<p>How do you use it from the Netherlands? It's only available in the US no?</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 763940,
      "author_name": "sukekiyo",
      "author_url": "",
      "post_date": "2020-03-05T02:09:39.340000",
      "content": "<p>I use <a href=\"https://chrome.google.com/webstore/detail/auto-refresh/ifooldnmmcmlbdennkpdnlnbgbmfalko?hl=en\">Auto Refresh</a></p>",
      "votes": 0,
      "replies": [
        {
          "id": 764119,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-05T06:41:21.563000",
          "content": "<p>And that works well?\nSeems like an interesting add-on, thanks!</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 764473,
          "author_name": "sukekiyo",
          "author_url": "",
          "post_date": "2020-03-05T13:55:50.520000",
          "content": "<p>Yes, but it needs some techniques.\n1. Auto Refresh set around 60 min.\n2. Push reload button(F5 key), and select \"Cancel\" in dialog once.\n3. Push reload button again, and check \"Prevent this page from creating additional dialogs\", select \"Reload\".\n4. Repeat some times\n5. Then it will not come out dialogs !</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 763790,
      "author_name": "artur.k.space",
      "author_url": "",
      "post_date": "2020-03-04T21:26:26.550000",
      "content": "<p>Lol, I was thinking about this issue too. \nHere's my script:</p>\n\n<p>```\nfunction colabReconnect() {\n    var dialog = document.querySelector(\"colab-dialog.yes-no-dialog\");\n    var dialogTitle = dialog &amp;&amp; dialog.querySelector(\"div.content-area&gt;h2\");</p>\n\n<pre><code>if (dialogTitle &amp;&amp; dialogTitle.innerText == \"Runtime disconnected\") {\n    btn = dialog.querySelector(\"paper-button#ok\");\n    btn.click();\n    console.log(\"Reconnecting...\");\n} else {\n    console.log(\"Everything is ok.\");\n}\n</code></pre>\n\n<p>}</p>\n\n<p>timerId = setInterval(colabReconnect, 10000);\n```</p>",
      "votes": 0,
      "replies": [
        {
          "id": 764120,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-05T06:42:14.183000",
          "content": "<p>I'm not really familiar with JavaScript, hence doing my own Python script, but seems like we all have our own hacky ways to deal with the issues ;)</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 793416,
          "author_name": "coot54joe",
          "author_url": "",
          "post_date": "2020-04-01T02:32:16.903000",
          "content": "<p>Hello, how do I use this script , please? </p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 918160,
          "author_name": "",
          "author_url": "",
          "post_date": "2020-07-07T03:21:12.850000",
          "content": "",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 763713,
      "author_name": "garyong",
      "author_url": "",
      "post_date": "2020-03-04T19:18:34.820000",
      "content": "<p>I believe the right query is \"colab-connect-button\"</p>",
      "votes": 0,
      "replies": [
        {
          "id": 763729,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-04T19:36:04.483000",
          "content": "<p>Oh I’ll try that then! Maybe I just didn’t do it correctly and no need for this hacks way of doing it!</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 764393,
          "author_name": "garyong",
          "author_url": "",
          "post_date": "2020-03-05T12:16:09.860000",
          "content": "<p>From what I read on StackOverflow google changes the name of the button from time to time because they know people are doing it. But you can always inspect and see which button is it.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 764543,
          "author_name": "Maxime Lenormand",
          "author_url": "",
          "post_date": "2020-03-05T15:23:45.680000",
          "content": "<p>Oh that makes sense! I guess I can stick to my script for now then!</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 767216,
      "author_name": "",
      "author_url": "",
      "post_date": "2020-03-09T10:09:47.283000",
      "content": "",
      "votes": 0,
      "replies": []
    },
    {
      "id": 767288,
      "author_name": "CSD",
      "author_url": "",
      "post_date": "2020-03-09T12:44:15.293000",
      "content": "<p>Useful trick. Thanks!</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "763596": "Hello!\n\nAs someone without access to a decent GPU, and doing Kaggle only from my relatively low spec laptop, Colab Pro was great to give access to 24h long sessions of compute on a P100, great for this competition!\n\nBut, one needs to stay active on the tab, otherwise your session is disconnected and you basically loose everything you were doing. Saving weights becomes _really_ important, but that doesn't allow you to run your notebook all night, or all day if you're away form your computer.\n\nThere are some hacks to do that, obviously.\n\nThe first one I found was a Javascript script to run in the browser:\n\nHit Ctrl + Shift + I and add this in the console:\n`function ClickConnect(){\nconsole.log(\"Working\"); \ndocument.querySelector(\"colab-toolbar-button#connect\").click() \n}\nsetInterval(ClickConnect,60000)`\n\nThis will basically click to reconnect the notebook every 6 seconds.\n\nHowever, after trying this, the notebook just adds random empty cells either in the middle or at the end of my notebook, like so:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F2605845%2Feeafb8361dc24bc824caa5958f464bc9%2FScreenshot%20from%202020-03-04%2017-30-37.png?generation=1583339454155514&amp;alt=media)\n\nI also tried using a [Chrome add-on](https://chrome.google.com/webstore/detail/colab-auto-reconnect/nbcihfbfamjlfiopdcemmohoojdecjid) but that also just didn't prevent Colab from telling me it's gonna kick me out after 90 minutes, and doing so.\n\nSince then, I decided to take things in my own hands, and thought some of you might find this interesting and helpful. I made a small python 3 script that I just run that randomly moves the mouse back and forth and clicks, always on the same place, but that's enough to fool Colab into thinking I'm active on the notebook and not disconnect:\n\n\n`import numpy as np`\n`import time`\n`import mouse`\n\n`while True:`\n\t`random_row = np.random.random_sample()*100`\n\t`random_col = np.random.random_sample()*10`\n\t`random_time = np.random.random_sample()*np.random.random_sample() * 100`\n\t`mouse.wheel(1000)`\n\t`mouse.wheel(-1000)`\n\t`mouse.move(random_row, random_col, absolute=False, duration=0.2)`\n\t`mouse.move(-random_row, -random_col, absolute=False, duration = 0.2)`\n\t`mouse.LEFT`\n\t`time.sleep(random_time)`\n\n\nYou just need to run it as `sudo` (as it takes control of the mouse) and it should work, allowing you to take full advantage of Colab's 12h sessions (or 24h for the Pro tier).\n\nHope that helps!",
    "764692": "This actually works (confirmed):\n&gt; function ClickConnect(){\n         console.log(\"Clicking\"); \n         document.querySelector(\"colab-connect-button\").click() \n}\nsetInterval(ClickConnect,60000)",
    "768369": "![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F1649622%2F9710aad78000e5f903c4b32b37f60895%2Fcolab.jpg?generation=1583862014401618&amp;alt=media)\nHaha my situation every night. Anyway i tried javascript approach didnt worked.Now i am gonna try your method @maxlenormand . Thank you 😃 ",
    "769558": "For Mac users, the mouse library will not work. You will have to try some other Mouse controller library like [pynput](https://pypi.org/project/pynput/) or [pyAutoGUI](http://pyautogui.readthedocs.io/en/latest/)",
    "768442": "btw, if you need more RAM you can fill the RAM and kernel crashes and asks you switch to a higher RAM notebook. (Works for non-pro as well)\nJust run\n```\na = []\nwhile(1):\n     a.append('-0.0001')\n```\n",
    "763691": "That's a cool hack!\n\n&gt; your session is disconnected and you basically loose everything you were doing.\n\nTwo things:\n1. If you don't want to lose everything you have been doing, connect your notebook to Google Drive and save your weights after each epoch. You can then load them and continue training in your next session.\n\n2. your session gets disconnected but not necessarily terminated. Even if you get disconnected, your session keeps running on the cloud until something else happens e.g. they run out of compute and terminate your session to allocate it to other users or you reach the end of your quota. ",
    "766815": "Save the model weights to the drive.",
    "766737": "Thank you for your sharing, but how to use your script? \nIt seems like I need to run your script in my local computer?",
    "764916": "can colab pro get access to in China?",
    "763940": "I use [Auto Refresh](https://chrome.google.com/webstore/detail/auto-refresh/ifooldnmmcmlbdennkpdnlnbgbmfalko?hl=en)",
    "763790": "Lol, I was thinking about this issue too. \nHere's my script:\n\n```\nfunction colabReconnect() {\n    var dialog = document.querySelector(\"colab-dialog.yes-no-dialog\");\n    var dialogTitle = dialog &amp;&amp; dialog.querySelector(\"div.content-area&gt;h2\");\n\n    if (dialogTitle &amp;&amp; dialogTitle.innerText == \"Runtime disconnected\") {\n        btn = dialog.querySelector(\"paper-button#ok\");\n        btn.click();\n        console.log(\"Reconnecting...\");\n    } else {\n        console.log(\"Everything is ok.\");\n    }\n}\n\ntimerId = setInterval(colabReconnect, 10000);\n```\n",
    "763713": "I believe the right query is \"colab-connect-button\"",
    "767216": "",
    "767288": "Useful trick. Thanks!"
  }
}