A Beginner’s Guide To Using Google Colab

COLAB

We are all familiar with the pop-up alerts of ‘memory-error’ while trying to work with a large dataset of machine learning (ML) or deep learning algorithms on Jupyter notebooks. On top of that, owning a decent GPU from an existing cloud provider has remained out of bounds due to the financial investment it entails. The machines at our disposal, unfortunately, do not have unlimited computational ability. But the wait is finally over as we can now build large ML models without selling our properties.

The credit goes to Google for launching the Colab – an online platform that allows anyone to train models with large datasets, absolutely free. From working with complex models to sharing the work with others in a simple manner, Colab is what the data science community has been longing for years. 

Colab should be the ideal choice for anyone looking forward to improving their Python coding skills with deep learning libraries such as PyTorch, Keras, TensorFlow, and OpenCV. One can create, upload, store and share notebooks in Colab, mount the Google Drive, import directories, and more. Since it is a whole new set of features put together, navigating could be a challenge. In this article, we will provide a simple and quick tutorial on how to go about Google Colab.

Setting Up Drive

To begin with, one can click on the link to get started with Colab. As the picture displays, one can create a new notebook with just one click, and then can upload a local notebook to Colab. Through using an authentication process, a user can import a notebook from Google drive or GitHub as well.

In case a user is already working on Google drive, he or she can directly add a new Colab notebook by clicking on ‘new’ and dropping the menu down to ‘more’ and selecting ‘Colaboratory’. One can also easily rename the notebook. 

Choosing GPU Or TPU

What makes Google Colab popular is the flexibility users get to change the runtime of their notebook. As shown in the picture, one can click the runtime menu and change its type. A user will get two options after clicking on ‘Change runtime type’. A box will appear on the screen with the option ‘hardware accelerator’. Users can either select TPU or GPU to work on their notebook. 

One can even make it better by mounting their Google drive. To do so, one needs to use: 

from google.colab import drive

drive.mount(‘/content/gdrive’)

It is advisable to shut the notebook since the step will allow others to use it as a valuable resource, and they can further share it with others. To terminate the notebook, one can follow these steps:

Runtime > Factory Reset Runtime

Using Terminal Codes

Google Colab allows a user to run terminal codes, and most of the popular libraries are added as default on the platform. Libraries like Python’s Pandas, NumPy and Scikit-learn come pre-installed with Colab, and running them is a straightforward job. In case a user wants to run a different Python library, follow the step below:

!pip install library_name

The only catch is the usage of an exclamation mark (!) that must be put before entering each command. 

Colab also allows the user to import any library by running import like any other notebook. 

Although PyTorch is now supported on Colab, one can further use the code given below in case they face some complexities:

# http://pytorch.org/

from os.path import exists

from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag

platform = ‘{}{}-{}’.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())

cuda_output = !ldconfig -p|grep cudart.so|sed -e ‘s/.*\.\([0-9]*\)\.\([0-9]*\)$/cu\1\2/’

accelerator = cuda_output[0] if exists(‘/dev/nvidia0’) else ‘cpu’

!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.1-{platform}-linux_x86_64.whl torchvision

import torch

Importing From GitHub

As mentioned above, one can import any file from Google Drive and GitHub. The importing function from GitHub is straightforward, as is seen in the picture. One can look for any organization or user to find the file. Once the file is found, they can enter the link to import the files. In case a user is not able to find the exact material, they have the option to go through the repository drop-down menu as shown in the picture below.

Saving

The most important part is saving on time. One can use the unconventional ‘command-s’ or drop the ‘file’ menu down to save. A user can further create a copy of the notebook by dropping ‘File’ -> ‘Save a Copy in Drive.’ One can also download the notebook  by going from ‘File’ -> ‘download .ipyb’ or ‘download .py.
We hope this article will enable readers to navigate Google Colab seamlessly and take advantage of the free GPU environment. One can also read our article on how Google Colaboratory Can Be Your Free GPU For Deep Learning for more information.

Download our Mobile App

Subscribe to our newsletter

Join our editors every weekday evening as they steer you through the most significant news of the day.
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.

Our Recent Stories

Our Upcoming Events

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox
MOST POPULAR