5 Amazing Google Colab Hacks You Should Try Today!

Abhishek Sharma Last Updated : 18 May, 2020
7 min read

Introduction

Google Colab is an amazing gift to the data science community from the fine folks at Google. Colab gives us the ability to build complex and heavy machine learning and deep learning models without having to expend our machine’s limited resources.

I can certainly appreciate this given how much I used to struggle on my machine! The ‘out of memory’ error is now quite infamous in the data science community – Google Colab provides us with the workaround and adds several cherries on top!

I love the free GPU and TPU support – it’s simply unparalleled and unrivalled in any other coding IDE. If you’re new to Google Colab, I highly recommend checking out my tutorial for beginners here.

google colab hacks

 

In this article, I have listed five awesome Google Colab hacks that I’ve personally found really useful in my data science journey. These hacks will not just save you a lot of time but will broaden your understanding of Colab as a whole.

So, let’s make you a Google Colab expert!

 

The 5 Google Colab Hacks We’ll Cover:

 

1. Increase Google Colab RAM

Update: Recently, I have noticed that this hack is not working for some users. So keep this in mind before using this.

Google Colab already gives us about 13 GB of RAM for free. That’s quite impressive, but there are times when even that isn’t enough when we’re building heavy deep learning models. Here’s how you can quickly increase the RAM in your Colab notebook.

It’s a very simple trick and it almost doubles your RAM capacity. For getting higher RAM, you need to run the following code in your Colab code cell and wait for Google Colab to crash:

i = []
while(True):
    i.append('a')

Yes, you read it right – wait for Google Colab to crash. As soon as it crashes due to a lack of RAM, it asks if you want more RAM:

get more ram

Now, click on Get more RAM  and you will see another popup:

choose high runtime

Just click on the YES button:

colab with 25 gb ram

And voila! You now have about 25 GB of RAM at your disposal!

 

2. Stop Google Colab From Disconnecting

Here’s a story I’m sure a lot of you will be familiar with. I was working on Colab the other day and left my laptop up and running for about an hour. When I came back, I saw that Google Colab disconnected my notebook and the model training had stopped. Imagine my immense disappointment!

Of course, I found out later that Google Colab disconnects our notebook if we leave it idle for more than 30 minutes. But here’s the good news -from today, you will never face this issue.

disconnected google colab

Just open your Chrome DevTools by pressing F12 or Ctrl+Shift+I on Linux and run the following JavaScript code in your console:

function KeepClicking(){
   console.log("Clicking");
   document.querySelector("colab-toolbar-button#connect").click()
}setInterval(KeepClicking,60000)

Here, the value 60,000 represents the milliseconds(equivalent to one minute). This small piece of code makes a click on the Colab screen after every 60 seconds. Thus, Colab thinks that the notebook is not idle and you don’t have to worry about being disconnected!

 

3. Snippets in Google Colab

Remembering code syntax is among the biggest issues almost every beginner faces in the starting phase of their data science journey. Due to this, people often find it very difficult to learn data science and quickly lose their motivation. We can’t be having that!
Therefore, I have a simple solution for you. Even if you don’t consider yourself a beginner, I am sure this trick will help you at some point in your data science journey.
The best way of tackling this situation is to create a snippet of the code that you most require, or you find it difficult to learn its syntax. A snippet is a reusable piece of code which you can use whenever you want.
In this way, you can save yourself from the headache of remembering code and focus on the concepts of data science. If you ask me about my favorite features of Colab then, my first answer is free GPUs and TPUs, and the second one is the ability to create custom snippets. Yes, you can create your own custom snippets inside Google Colab.
Let me show you how it’s done. First, you need you to create a notebook in Colab. Then, add a text cell and type your snippet name. Below that, add the code cells and type your code:
import snippet google colab
Here, I have created a very basic snippet for import statements – you can create a snippet of whatever you want. Next, its time to use this snippet in another notebook. Go to Tools and open Settings:
google colab settings
Now, you’ll see a popup like this one below. Just paste the link of a notebook containing snippets, click SAVE, and you are ready to go:
google colab snippet
You can see all your snippets here. Just search for your snippet by its snippet name and insert it into your code:

google colab snippets

4. Top Keyboard Shortcuts in Google Colab

Switching between your keyboard and mouse is a tedious and time taking task. Therefore, to speed things up, Google Colab provides us with certain really useful keyboard shortcuts. Honestly, you can’t master the Colab experience until you’ve used and become acquainted with these keyboard shortcuts.

Honestly, I feel these keyboard shortcuts increase our productivity as data scientists. I know it takes some time to learn these, but once you do get a hang of them, you can complete your work much faster.

I have created a list of top keyboard shortcuts that you should know when working with Google Colab. These key combinations are what I use, but you can modify them according to whatever is more comfortable:

google colab keyboard shortcuts

 

5. Modes in Colab

This hack is about different viewing modes offered by Google Colab and how you can switch between them. A lot of you will appreciate this switch and your eyes will thank you in the long term!

Apart from the default light mode, Google Colab also gives us a dark mode. Yes, similar to any other code editor out there, Google Colab also has a dark mode. Even Jupyter Notebooks don’t come with this feature by default.

To change your viewing mode to dark mode. Go to Tools and open Settings:

google colab settings

Here, you will see a popup like this one below. Just select the dark mode in theme, click SAVE, and you are ready to go:

google colab dark theme

As you can see, this mode is more relaxing for your eyes. You can use this mode when you are working in the dark because it saves you from eye strain:

google colab dark theme

We aren’t done yet! How about adding some fun to your Colab screen? Welcome to the Corgi and Kitty mode. In Corgi mode, you see dogs running on your colab screen similar to the one below:

corgi mode

You can easily guess what you get in Kitty mode. Yes, this one is for all the cat lovers out there. You can see your cats on the Colab screen too:

kitty mode

Or you can have both of them and have fun while coding:

kitty corgi mode google colab

Now, let’s see how you can get these modes. Go to Tools, open Settings and go to the Miscellaneous section. Here, you can select your Corgi and Kitty mode, click SAVE and there you have your cats and dogs strolling on your screen:

kitty corgi mode setting

 

End Notes

These hacks cover a broad range of things you can do in Google Colab. From efficiently running your code to having fun while designing deep learning models, these Colab hacks will enrich your coding experience.

If you want to know more tips and tricks related to data science, I recommend taking this free course.

Do share this article and comment below in case you have any queries or feedback. And don’t forget to sign up on Analytics Vidhya to read various exciting articles related to machine learning and data science!

He is a data science aficionado, who loves diving into data and generating insights from it. He is always ready for making machines to learn through code and writing technical blogs. His areas of interest include Machine Learning and Natural Language Processing still open for something new and exciting.

Responses From Readers

Clear

Sachchidanand Kumar
Sachchidanand Kumar

Nice Article Abhishek!! Adding to the above mentioned features. Google Colab runtime type can also be switched from one programming language to other like from Python 3 to R. SWIFT language will also be available soon.

Sachchidanand
Sachchidanand

Thank You RIZVI !!

Jojo
Jojo

"Your session crashed after using all available RAM. View Runtime Logs." This is what is displayed when I tried (1). I am not a coder so you might check what I am saying to be true / false.

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details