10 tricks for a better Google Colab experience | by Cyprien NIELLY | Towards Data Science

Colab is a great tool for coding. I use it very often, for a large set of tasks, from traditional Machine Learning to Deep Learning applications using PyTorch, TensorFlow or OpenCV. With Google Colab, creating and sharing notebooks is intuitive and simple 😃
Here are 10 tips and tricks I gathered over time that will help you to get the most out of Google Colab.
Table of content:
Save time with keyboard shortcutsMount your Google Drive to ColabRun bash commandsUpload / download filesActivate GPU and TPULink Colab to GitHubStop Colab from disconnectingDisplay dataframes as interactive tablesUse TensorBoard with ColabChange display mode
Save time with keyboard shortcuts
Mount your Google Drive to Colab
Run bash commands
Upload / download files
Activate GPU and TPU
Link Colab to GitHub
Stop Colab from disconnecting
Display dataframes as interactive tables
Use TensorBoard with Colab
Change display mode
You can access all the shortcuts selecting “Tools” → “Keyboard Shortcuts”.
But here is a selection of my top 5:
Undo last action (inside a cell): ctrl + m + z
Find and replace: ctrl + m + h
Insert code cell above: ctrl + m + a
Insert code cell below: ctrl + m + b
Delete cell: ctrl + m + d
Be sure to check out other shortcuts and customize your favourite ones!
This feature is useful to get access to files that are stored on your Google Drive. It is also the perfect way to save your models and data. To do so, just enter the following code snippet:
from google.colab import drivedrive.mount('/content/gdrive')
The cell will return the following:
Click on the link, give Google Colab access to your Drive, and retrieve the authorization code. Paste it, and you’re done!
You are now able to access your Google Drive files under: /content/gdrive/My Drive/Use the batch comand !cd or the “Files” panel on the left.
Bash commands can be run by prefixing the command with ‘!’.Example:
Download dataset from the web with !wget <ENTER URL>
Install libraries with !pip install <LIBRARY>or !apt-get install <LIBRARY>
Run an existing .py script with !python script.py
Clone a git repository with !git clone <REPOSITORY URL>
To upload a file (or several) from your computer, run:
from google.colab import filesfiles.upload()
To download a file, run:
from google.colab import filesfiles.download('path/to/your/file')
Another way to do this is to use the file explorer on the left panel, with drags & drops.
⚠️ Be aware the files will disapear as soon as you leave Google Colab.
The default hardware of Google Colab is CPU. However you can enable GPU (and even TPU) support for more computationally demanding tasks like Deep Learning.
Click on: “Runtime” → “Change runtime type” → “Hardware accelerator”. Then select the desired hardware.
You can easily check if the GPU is enabled by executing the following code:
import tensorflow as tftf.test.gpu_device_name()
To open a notebook hosted on GitHub, there are 3 options:
Go to https://colab.research.google.com switch to the GitHub tab and enter the URL of the notebook
The notebook carries an “Open in Colab” badge, which allows you to open it directly on Google Colab. The markdown code to insert into the README.md file is:
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/googlecolab/colabtools/blob/master/notebooks/colab-github-demo.ipynb)
Use “Open in Colab” Chrome extension. After installation, whenever you find a Jupyter notebook in GitHub, click the icon of the plugin and it will open in Colab.
Follow: “File” → “Save a copy in GitHub”. Once that is clicked it will ask for the authorization of your git account. Finally, you will be able to save the collaboratory notebook into the repository of your choice.
This is a big drawback of Google Colab, and I’m sure a lot of you encountered this issue at least once. You decide to take a break, but when you’re back your notebook is disconnected!
Actually, Google Colab automatically disconnects the notebook if we leave it idle for more than 30 minutes. 🕑
Open your Chrome DevTools by pressing F12 or ctrl+shift+i on Linux and enter the following JavaScript snippet in your console:
function KeepClicking(){console.log("Clicking");document.querySelector("colab-connect-button").click()}setInterval(KeepClicking,60000)
This function makes a click on the connect-button every 60 seconds. Thus, Colab thinks that the notebook is not idle and you don’t have to worry about being disconnected!
First, be aware that when you connect to a GPU, you are given a maximum of 12 hours at a time on the Cloud Machine.
Sometimes it happens that you’re being disconnected, even within this 12-hour time lapse. As explained in Colab’s FAQ: “Colaboratory is intended for interactive use. Long-running background computations, particularly on GPUs, may be stopped.” 😕
Colab includes an extension that renders pandas dataframes into interactive tables that can be filtered, sorted, and explored dynamically.
Enable this extension with %load_ext google.colab.data_table and disable it with %unload_ext google.colab.data_table
TensorBoard is a tool for providing the measurements and visualizations needed during a Deep Learning workflow. It can be used directly within Colab. 📉 📈
Start by loading the TensorBoard notebook extension:
%load_ext tensorboard
Once your model is created, start TensorBoard within the notebook using:
%tensorboard --logdir logs
For more details, please check the Tensorflow tutorial on that topic.
Dark mode: a lot of people prefer this mode because is more relaxing for their eyes. You can activate it following “Tools” → “Settings” → “Theme”.
For cats & dogs lovers: yes, there is a Kitty and a Corgi mode. Go to “Tools” → “Settings” → “Miscellaneous”. Here, you can select the mode, and you will see your cats and dogs strolling on your screen! 😹 🐶
I hope this article has been helpful and that you will keep using this wonderful and free tool. Please share your extra tips in the comments below. Happy coding!
For further reading, you can have a look at this very exhaustive blog post: