Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Colab: Can we restore all the data even after the runtime disconnects?

I am a new learner. I recently started learning Google Colab. Whenever I close my Colab and reopen it, all the code start executing from beginning. Is there any way to restore the local variable, code outputs and all the previous program data? It is really time-consuming to load the dataset every time.

like image 885
tamanna tasnim Avatar asked Jan 19 '20 05:01

tamanna tasnim


People also ask

How do you avoid runtime disconnection in Colab?

Ctrl+ Shift + i to open inspector view . Then goto console. It would keep on clicking the page and prevent it from disconnecting.

Can I leave colab running overnight?

Google Colab notebooks have an idle timeout of 90 minutes and absolute timeout of 12 hours. This means, if user does not interact with his Google Colab notebook for more than 90 minutes, its instance is automatically terminated. Also, maximum lifetime of a Colab instance is 12 hours.

How do I reconnect to a runtime on Google Colab?

In Colaboratory, click the "Connect" button and select "Connect to local runtime...". Enter the URL from the previous step in the dialog that appears and click the "Connect" button. After this, you should now be connected to your local runtime.


3 Answers

Unfortunately No (by this answer posted date), you cannot restore to previous runtime. Everything restarts on a new runtime session on a different virtual machine. Notebooks run by connecting to virtual machines that have maximum lifetimes that can be as much as 12 hours. And Colab Pro says to provide around 24hrs of runtime. This is necessary for Colab to be able to offer computational resources for free.

However you can apply good practices to help you work faster. Some of them are:

  • Save your datasets and trained models on your Google Drive; Mount it and use it as required. Only runtime local variables and program data for that session are destroyed.
  • Use pre-trained models to implement Transfer Learning to save training time.
  • Use "Connect to hosted runtime" and "Manage Sessions" to use the free resources effectively.
like image 68
sparsh Avatar answered Oct 17 '22 03:10

sparsh


Sadly, it's just part of the workflow with colab, but there are ways to make life easier. To persist data you'd want to connect to google drive and pull/save files from there:

from google.colab import drive
drive.mount('/content/drive')

Then follow instructions - click the link, copy/paste the auth token.

After connecting to google drive - copy files that are stored on the drive using command !cp. For example, these commands copy files stored on the drive to local notebook environment:

!cp "/content/drive/My Drive/Colab Notebooks/trainer.py" "trainer.py"
!cp "/content/drive/My Drive/Colab Notebooks/data.pkl" "data.pkl"

To copy files and folders from notebook environment to drive use the same !cp command:


!cp "model" "/content/drive/My Drive/Colab Notebooks/my-fancy-model"
like image 3
ego Avatar answered Oct 17 '22 02:10

ego


Assuming you want to see previous ouputs of the code. You could use File > Save and Pin Revision to save revision history including revision name. That way it will store previous outputs including code changes. Now going to File > Revision History, it will show difference between two version. Clicking on three dot on right side it will show option to restore version, open, or name it.

like image 2
B200011011 Avatar answered Oct 17 '22 04:10

B200011011