MITB Banner

A Beginner’s Guide To Using Google Colab

Share

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.

Share
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.