Support from FASE's Education Technology Office

Using Google Colab for a coding activity (Prof. Sinisa Colic)

Updated

We wish we'd thought of it, but we didn't! This technique was documented by Dr. Sinisa Colic and used as instructions that direct students to use Google Colab (see the two sample labs below). It details an introduction to Google Colab features and illustrates how students can use this tool to complete a coding activity.

Submitted by: Dr. Sinisa Colic
Contact Info:
[email protected]
Ask him about: Google Colab

What is Google Colab

Google Colab is a web-based iPython Notebook service for interactive coding. Google Colab offers free access to Nvidia K80 GPU, and it is becoming a popular tool for quick prototyping and visualization. Colab is easy to learn and it is very similar to Jupyter Notebook, another very popular tool for interactive coding.

1. Collab Basics

If you are viewing this file read-only (i.e. using the shared link), click ''OPEN IN PLAYGROUND", right below the header. Playground mode opens a copy of the read-only notebook in a temporary session that doesn't save the notebook. If you want to save any changes, you must save the notebook (this is covered in the Saving and Exporting Colab File section).

1.1. Colab File "Cells"

A Colab file is composed of 'cells' which are blocks of code that can be run independently. Cells can be added by clicking the '+' buttons on the top left-hand side.

You can select a cell by clicking on it. Below is an example of code cell.

2. Running and Organizing Cells

There are several options for running a cell or group of cells. These options are listed under the Runtime menu of the header. Alternatively, a single cell can be executed by the 'Run' button on the left side.

Try running the above example code cell. Then, run the code cell below.

Note that each cells can be run independently, and if there is a change, only the cells related to that change need to be run again.

Therefore, it is important to decompose your code into blocks of cells, instead of writing everything in a single cell. This modular approach makes it easy to change parts of the code without re-running everything.

3. Saving and Exporting Colab File

3.1. Saving a Colab File

As mentioned above, Playground only provides temporary session, so in order to save any changes, notebook files must be saved.

The easiest way to save Colab notebook is to save it in your Google Drive. Click File > Save a copy in Drive. Once you saved a copy of this tutorial, you can open it from your Google Drive or Colab dashboard.

3.2. Exporting a Colab File

Colab can be exported as a notebook file or Python file by File > Download .ipynb or File > Download .py. Exported notebook files can be reloaded into Google Colab or used in Jupyter Notebook.

To save Colab file as a PDF, File > Print and then save as PDF.

For example, in Google Chrome, click 'Open PDF in Preview'.

3.3. Sharing a Colab File

You can share Colab using shareable link. Click 'Share' button on the top right corner and click 'Get shareable link'.

4. Running Bash Commands and Installing Packages

4.1. Bash Commands

Bash commands can be run by prefixing the command with ''!''.

For example, below 'pwd' command prints out the pathway to the current working directory.

!pwd

'ls' command prints out a list of contents in current directory.

!ls

Bash command can be used to install packages or download files (e.g. datasets).

4.2. Installing Packages

Most common libraries (including PyTorch) are already installed on Colab.

To install Python packages, use:

!pip install [package name]

To install other packages available through Advanced Package Tool (apt):

!apt-get install [package name]

5. Using Google Drive

As mentioned above, Google Drive can be used to save Colab notebook files. However, this does not mean your Colab session will have access to your Google Drive. Sometimes you might want to load or save files from Colab session, and to do so, you need to mount Google Drive first.

Before mounting Google Drive, /content directory does not have gdrive directory, which is what we want to mount.

!ls /content

To mount Google Drive, run the below code and go to the link to retrieve the authorization code. Paste the authorization code in the box created by running the code and press enter.

from google.colab import drive

drive.mount('/content/gdrive')

Once mounted successfully, your entire Google Drive files should be accessible under /content/gdrive/My\ Drive/

!ls /content/gdrive/My\ Drive

After mounting Google Drive, you can download datasets into Google Drive and use it in Colab session, or save outputs of the session into your Google Drive.

6. Using Hardware Accelerator

To use GPU (or TPU), go to Runtime > Change runtime type and under Hardware accelerator, choose GPU or TPU.

After enabling GPU, the below statement should print true.

import torch

torch.cuda.is_available()

Previous Article Video demo of taking a ProctorU Auto Launch Assessment (Prof. Daniel Frances)
Next Article Screen/Audio Capture in Windows 10 (Prof. Bradley Saville)
Still have questions? Contact the FASE EdTech Office