Google Colab, or Colaboratory, is a doppelganger of Jupyter Notebook running on the cloud. Due to its zero-cost, pre-configured Python development environment, Colab has become a favorite among data science enthusiasts for running experiments in recent years.
Colab makes a perfect fit for anyone looking for a quick and affordable environment for training ML models. With the free access to a GPU or TPU, it’s hard to find a better alternative to Colab on the market today.
If you’re already familiar with Jupyter Notebook, you’ll have an easier time figuring out how Colab works. But if that’s not the case, we’ll cover the essentials of getting started with Google Colab in this post.
Colab is basically a cloud-based Python development environment created by Google Research. It gives you free but limited access to Google’s computing resources to run experiments without burdening your local system.
The Colab environment is specially configured for data science-related tasks. It comes with many popular data science libraries—like Pandas, Numpy, and Tensorflow—pre-installed. Most of the time, you only have to visit Colab from your browser and start building models from the get-go.
Colab allows you to create notebooks with blocks of executable code and text. This format is very similar to Jupyter Notebook. It isn’t surprising considering how Colab is based on the open-source Jupyter project.
Colab also allows you to share notebooks with others and collaborate on projects. It has become one of the biggest reasons people choose Colab over their local environments.
Another draw is the free access to GPUs and TPUs. It sounds like a deal too good to be true, considering the cost of using GPUs and TPUs on popular cloud platforms. But Colab’s offer is indeed true, even though it comes with certain limitations that we’ll discuss in later sections.
Basically, anyone can use Google Colab for Python development purposes. It doesn’t have to be data science related. But its data science-tailored environment attracts the majority of people from this field. Several of its popular use cases include data analysis, data visualization, and machine learning model development and training.
Let’s take a look at working with some basic Colab features in this section.
You can visit this Google Colab link to create your first notebook. It brings you to a “Welcome To Colaboratory” notebook and a popup window that allows you to create a new notebook or open an existing one.
If you choose the
New Notebook option, Colab gives you a new notebook in
Additionally, Colab’s compatibility with Jupyter allows you to open an existing Jupyter notebook through the Upload tab in the previous popup window.
Colab saves all new notebooks in your Google Drive.
You can add a new code cell to Colab using
Ctrl+M B keyboard shortcut. If you want to convert a code cell to a text cell, use
Ctrl+M M. The interface also provides different methods to add new code or text cells.
You can use code cells to write the usual Python code. Text cells, on the other hand, are used to organize the notebook and give more context to your coding decisions.
Text cells primarily support markdown for adding content. But they also accept other formats like HTML and latex. You can also add images, links, emojis, and other styling elements to the text.
Colab provides a few different options for running a code cell.
- Run the current cell: Use the arrow icon on the cell’s left side or
- Run the current cell and add a new cell below:
- Run the current cell and go to the next cell:
- Run all cells:
Runtime option in the notebook’s top menu shows more ways to control how the code execution happens.
The first time you use this run operation on your notebook is when Colab allocates you a virtual machine to execute the program. You can have access to it for a maximum of 12 hours. After the time limit, Colab disconnects and deletes your VM.
If you leave the notebook idle for too long, Colab automatically disconnects you even though the time hasn’t run out.
In this section, we’ll look at some special features in Colab that you may find useful in different situations.
We already know how Colab assigns a virtual machine to run your program. But it doesn’t disclose the precise memory and disk space bounds of these VMs. In fact, their values are rarely identical across VMs. Usually, you can expect to have close to 12GB of memory and 108GB of disk space.
If you run out of that 12GB memory allocation, Colab allows you to switch to a high-RAM runtime of 25GB.
Colab provides two scaled graphs to give you an idea about your resource usage. They plot your overtime RAM and disk utilization to help you figure out if you’re close to exceeding the limits.
If you’re running particularly computationally heavy, time-consuming experiments on Colab, you can select GPUs or TPUs to accelerate the execution.
You will find this “Change runtime type” option under the “Runtime” menu. It leads you to a popup window to select the hardware accelerator type. By default, this value is set to none. If you want to use a GPU or a TPU for your work, select the appropriate option from the dropdown and save the changes.
Colab’s GPU and TPU access comes with dynamically changing limits. Colab doesn’t disclose the lifetimes of the allocated GPUs or idle timeouts. But you can expect the maximum lifetime to last about 12 hours like the VMs.
The most commonly assigned GPU in Colab is K80. Faster GPUs like T4 are more difficult to see and often follow restricted quotas.
Colab also employs a mechanism to prioritize GPU and TPU resources for users who have used fewer resources in the near past. Users who often run long computations are more likely to face tighter limitations in this scenario.
Colab allows you to upload files from your local device to use in your experiments. The following files.upload method prompts you to select the necessary files and handle the operation.
from google.colab import files uploaded = files.upload()
Once it completes, you’ll be able to see the newly added files in the Files tab on the left pane.
You can mount your Google Drive on the allocated VM using the drive.mount method.
from google.colab import drive drive.mount('/content/drive')
This code opens a popup window that prompts you to authorize the mounting operation.
After giving permission, you’ll be able to view and access your Drive file system through the Files tab.
As we mentioned before, you can add other collaborators to a notebook as editors, commenters, or viewers. It gives them access to all code, text, and outputs that appear in your notebook.
But it doesn’t share any resources or content you’re using with the others. They won’t be able to access your VM environment, including the local file system or new libraries you’ve installed. Collaborators have to set up such content manually before running the notebook.
Colab allows you to run terminal commands on a code cell by using ! as a prefix. This method can list the files in the current directory, install new Python libraries, clone GitHub repositories, or execute any other terminal-bound task.
Despite the many benefits of Colab’s free tier, it comes with limited resources that aren’t enough in certain situations. Colab Pro and Colab Pro+ provide a paid solution to overcome this problem.
Colab Pro and Pro+ give you access to longer runtimes, faster GPUs, and more memory than the free tier. Colab Pro+ has the additional benefit of executing notebooks in the background, even when you close the browser tab.
Both paid plans come with the promise of minor fluctuations in resource allocations from one VM to another.
The following table shows a brief comparison between Colab Pro and Pro+.
|Colab Pro||Colab Pro+|
|Maximum runtime||24 hours||24 hours|
|Maximum RAM||32GB||52GB (with high-RAM option)|
|GPU types||K80, T4, P100, or V100||K80, T4, P100, or V100|
Colab is an incredibly valuable tool, particularly for hobby data scientists, for conducting their experiments. It gives you access to computing resources, an easy-to-use development environment, and a collaborative working space at zero cost on your end.
Although not free, Colab Pro and Pro+ come with higher resource guarantees at an affordable price to more regular and professional users.
These three plans combined have made Google Colab a formidable opponent for challenging more expensive data science platforms in the market today. If you, too, are looking for a place to practice and perfect your skills in this field, Colab is sure to become a perfect fit for you.
Thanks for reading!