Tutorial 6 - Google Colab

View notebook on Github Open In Collab

Google Colab (or Colaboratary) is a cloud-based platform created by Google that offers an environment for sharing, running, and writing Python code within Google Drive. Colab runs Jupyter notebook files, and it comes with pre-installed popular Data Science and Machine Learning libraries and frameworks, such as TensorFlow, PyTorch, NumPy, pandas, and others.

Google Colab provides CPU, TPU, and GPU support. It enables real-time collaborative editing on a single notebook, much like the collaborative text editing functionality provided by Google Docs.

This is the official Colab webpage.

Welcome Page of Colab

The welcome page offers basic tutorials about working with Jupyter notebooks, data science, and machine learning.

It provides:

  • Overview of Colab basic features, such as Code Cells and Markdown Cells

  • Loading data: Drive, Sheets, and Google Cloud Storage Link

  • Data Visualization Link

  • Machine Learning introduction course Link

Top Menus

On the top menu section, Colab provides very similar features to the original Jupyter Notebook interface.

  • File: create/rename/upload/move/save notebook files

  • Edit: move/copy/past cells, notebook settings

  • Insert: code/text/section header cells/code snippets/add a form field

  • Runtime: run/interrupt/restart cells/runtime type change (GPU<->CPU)

  • Tools: command palette/settings/keyboard shortcuts, etc.

2258767cf65c4091b91bac0551b4c15b

Upload Files and Mount the Google Drive

  1. Login into Google Drive with your Gmail account (Link).

  2. Create a folder to store your data files.

58f6b41566034f46a087ae484cb14772

  1. Create a new Jupyter notebook file. The file can be accessed in the “Colab Notebooks” folder in Google Drive.

4f26af9337654e809f98ed9f43055b6d

  1. Use the following code to mount your Google Drive in the Jupyter notebook.

[6]:
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
  1. Click Connect to Google Drive to permit the notebook to access Google Drive.

c034ab5a9f4b455a97d650d8c2b77991

  1. After you mount the Google Drive, you can load files from the drive. For example, load a csv file. Note that the path to the file needs to start with drive/My Drive/....

[7]:
import pandas as pd
df_IMDb = pd.read_csv('drive/My Drive/data_file/IMDb_movies.csv')
df_IMDb
[7]:
Unnamed: 0 Movie Name Year of Release Watch Time Movie Rating Metascore of movie Gross Votes Description
0 0 The Shawshank Redemption 1994 142 9.3 82.0 28.34 27,77,378 Over the course of several years, two convicts...
1 1 The Godfather 1972 175 9.2 100.0 134.97 19,33,588 Don Vito Corleone, head of a mafia family, dec...
2 2 The Dark Knight 2008 152 9.0 84.0 534.86 27,54,087 When the menace known as the Joker wreaks havo...
3 3 Schindler's List 1993 195 9.0 95.0 96.9 13,97,886 In German-occupied Poland during World War II,...
4 4 12 Angry Men 1957 96 9.0 97.0 4.36 8,24,211 The jury in a New York City murder trial is fr...
... ... ... ... ... ... ... ... ... ...
995 995 Philomena 2013 98 7.6 77.0 37.71 1,02,336 A world-weary political journalist picks up th...
996 996 Un long dimanche de fiançailles 2004 133 7.6 76.0 6.17 75,004 Tells the story of a young woman's relentless ...
997 997 Shine 1996 105 7.6 87.0 35.81 55,589 Pianist David Helfgott, driven by his father a...
998 998 The Invisible Man 1933 71 7.6 87.0 NaN 37,822 A scientist finds a way of becoming invisible,...
999 999 Celda 211 2009 113 7.6 NaN NaN 69,464 The story of two men on different sides of a p...

1000 rows × 9 columns

Enable GPU

Colab has built-in features that allow users to switch between CPU and GPU for working with Data Science/Machine Learning models.

Method1:

  • Click Edit Notebook Settings.

  • Choose an available GPU from the Hardware Accelerator, and click Save.

6fb057503ff34e38b39af1a559768236

c00521ec63454616b2771f91e91a1136

Method2:

  • Click Runtime Change runtime type.

  • Choose an available GPU from the Hardware Accelerator, and click Save.

703aa4cd62314e82aaa53de683d23ea2

Using GPU

Use the following code to load the GPU in your script. When you are connected to GPU, the code will print out “Found GPU at: /device:GPU:0”

[4]:
import tensorflow as tf
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
    raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))

Monitor Your Hardware Resources

To monitor available and used resources, click the Connect button in the upper right corner of your screen, and if your GPU is connected, it will show the hardware stats.

f5fb8fb15327403b81bd516ad2cb7701

Colab Subscription Plans

Paid Colab plans provide better hardware, i.e. access to more powerful GPU, and more VRAM and RAM. They also have longer timeout sessions. For instance, Colab Pro+ allows you to run the script for up to 24 hours with the web browser closed.

The free Colab version offers 16GB of GPU RAM, while the paid versions can have up to 48GB of RAM. Larger VRAM and RAM may be required to train some large language models (LLM).

For the free version, only T4 GPU are available, but sometimes there would be no GPU available. The free version has at most 12 hours sessions before timeout. In practice, for the free version, your session could be timed out and your script could be interrupted at any time. If you reconnect within the next 1-2 minutes, the session can be resumed, and otherwise, you will have to re-run your script.

6ced3ff9d68b47a88068a4af01c70d9d

BACK TO TOP