Google is quite aggressive in AI research. Over many years, Google developed an AI framework called TensorFlow and a development tool called Colaboratory. Today TensorFlow is open-sourced and since 2017, Google made Colaboratory free for public use. Colaboratory is now known as Google Colab or simply Colab.
Another attractive feature that Google offers to developers is the use of GPU. Colab supports GPU and it is totally free. The reasons for making it free for public could be to make its software a standard in academics for teaching machine learning and data science. It may also have a long-term perspective of building a customer base for Google Cloud APIs which are sold per-use basis.
Irrespective of the reasons, the introduction of Colab has eased the learning and development of machine learning applications. In this guide, we’ll cover a brief introduction to:
- Getting Started with Colab
- Documenting Your Work
- Saving and Sharing
- Advanced Code Execution
- Visualization and Graphics
- Developer Tools
- Interactive Forms
- Machine Learning Libraries
- GPU Acceleration
What is Google Colab?
If you’ve used Jupyter notebook previously, you’ll quickly learn to use Google Colab. To be precise, Colab is a free Jupyter notebook environment that runs entirely in the cloud. Most importantly, it doesn’t require any setup and the notebooks you create can be simultaneously edited by your team members, just the way you edit documents in Google Docs. Colab supports many popular machine learning libraries which can be easily loaded into your notebook.
What Colab Offers You
As a programmer, you can perform the following using Google Colab:
- Write and execute code in Python
- Document your code with support for mathematical equations
- Create, upload, and share notebooks
- Import and save notebooks from/to Google Drive
- Import and publish notebooks from GitHub
- Import external datasets (e.g., from Kaggle)
- Integrate PyTorch, TensorFlow, Keras, OpenCV
- Access free cloud service with free GPU
Creating Your First Colab Notebook
Let’s start by creating and executing your first notebook.
NOTEAs Colab implicitly uses Google Drive for storing your notebooks, ensure that you’re logged in to your Google Drive account before proceeding.
Step 1: Access Colab
Open the following URL in your browser: https://colab.research.google.com
You’ll see a welcome screen with various options for creating or opening notebooks.
Step 2: Create a New Notebook
Click on the NEW PYTHON 3 NOTEBOOK link at the bottom of the screen. A new notebook will open up with a code window where you can enter your Python code.
Setting the Notebook Name
By default, the notebook uses the naming convention UntitledXX.ipynb. To rename it, click on the name and type your desired name in the edit box. For example, you might name it “MyFirstColabNotebook.”
Entering and Executing Code
Let’s try a simple example. Enter the following Python statements in the code window:
import time
print(time.ctime())To execute the code, click on the arrow (play button) on the left side of the code cell. After a moment, you’ll see the output underneath the code window showing the current date and time.
Adding More Code Cells
To add more code to your notebook, you can either:
- Select Insert / Code Cell from the menu
- Hover your mouse at the bottom center of a code cell until CODE and TEXT buttons appear, then click CODE
Try adding a new cell with this code:
time.sleep(5)
print(time.ctime())Running All Cells
To run all the code in your notebook without interruption, select:
Runtime / Reset and run all…
This will execute all cells sequentially, and you’ll notice the time difference between outputs is exactly 5 seconds.
Managing Cells
Changing Cell Order: Use the UP CELL or DOWN CELL buttons to move cells up or down in your notebook.
Deleting Cells: Click on the vertical-dotted icon at the top right corner of a code cell and select “Delete cell.”
Documenting Your Code
While Python comments are useful, machine learning often requires more sophisticated documentation, including mathematical equations. Colab provides Text Cells for this purpose, which support Markdown and LaTeX.
Markdown Basics
In a Text cell, you can use Markdown syntax for formatting:
This is **bold**.
This is *italic*.
This is ~strikethrough~.Mathematical Equations
Colab supports LaTeX for mathematical representations. For inline equations, use single dollar signs:
$\sqrt{3x-1}+(1+x)^2$For block equations, use double dollar signs:
$$e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$$Here’s an example of more complex mathematical notation:
$$f(x_1, x_2) = 20 + e - 20exp(-0.2 \sqrt{\frac{1}{n}(x_1^2 + x_2^2)}) - exp(\frac{1}{n}(cos(2\pi x_1) + cos(2\pi x_2)))$$You can even create matrices:
$$A_{m,n} = \begin{pmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n}
\end{pmatrix}$$Saving Your Work
Colab provides multiple options for saving your notebooks.
Saving to Google Drive
Select: File / Save a copy in Drive…
This creates a copy of your notebook in your Google Drive, which you can rename later.
Saving to GitHub
You can also save directly to your GitHub repository:
Select: File / Save a copy in GitHub…
You’ll be prompted to log in to GitHub. If you don’t have a repository, you can create one and save your project there.
Sharing Your Notebook
There are several ways to share your work:
- Via Google Drive: Share the copy you saved in Google Drive with specific collaborators
- Via GitHub: Publish to your GitHub repository for public access
- Using the SHARE button: Click the SHARE link at the top right corner of your notebook
The Share dialog offers options to:
- Share with specific people via email
- Share with colleagues in your organization
- Share with anyone with the link
- Make it public on the web
You can also set different permission levels (can edit, can comment, can view) for each person or group.
Invoking System Commands
Jupyter includes shortcuts for many common system operations, and Colab supports this feature.
Simple Commands
You can use system commands by prefixing them with an exclamation mark:
message = 'A Great Tutorial on Colab by Tutorialspoint!'
greeting = !echo -e "$message\n$message"
greetingGetting Remote Data
You can download files from remote servers:
!wget https://wordpress.org/latest.zipCloning Git Repositories
You can clone entire GitHub repositories into Colab:
!git clone https://github.com/wxs/keras-mnist-tutorial.gitAfter cloning, you can right-click on any .ipynb file in the repository and select “Open With / Colaboratory” to open it in Colab.
System Aliases
To get a list of available system commands, execute:
!ls /binThis will show you all the available system utilities you can use in your notebook.
Executing External Python Files
If you have Python code stored in your Google Drive, you can load and execute it in Colab.
Mounting Your Drive
First, mount your Google Drive:
Select: Tools / Command palette, then search for and select “Mount Drive”
This will insert the following code:
from google.colab import drive
drive.mount('/content/drive')When you run this, you’ll be asked to authenticate and grant permissions. Once completed, your drive will be mounted at /content/drive.
Listing Drive Contents
You can now list files in your drive:
!ls "/content/drive/My Drive/Colab Notebooks"Running Python Code
To execute a Python file from your drive:
!python3 "/content/drive/My Drive/Colab Notebooks/hello.py"Creating Graphical Outputs
Colab supports rich outputs including charts and visualizations. Here’s an example using matplotlib:
import numpy as np
from matplotlib import pyplot as plt
y = np.random.randn(100)
x = [x for x in range(len(y))]
plt.plot(x, y, '-')
plt.fill_between(x, y, 200, where=(y > 195), facecolor='g', alpha=0.6)
plt.title("Sample Plot")
plt.show()The graphical output appears directly in the output section of the code cell.
Code Editing Help
Colab provides context-sensitive help to make coding easier.
Function Lists
When you type a module name followed by a dot, Colab can show you available functions. Simply hover over function names to get more information.
Function Documentation
To see documentation for any function or class, type the function name with an open parenthesis and hover over it:
Tensor = torch.cos()Hovering over cos will display its documentation, including parameters, return values, and examples.
Using Magics
Magics are special commands that provide a mini extensive command language in Colab.
Line Magics
Line magics are prepended with a single % character:
%ldir # Lists directory contents
%history # Shows command historyCell Magics
Cell magics are prepended with %% and affect the entire cell:
%%html
<marquee style='width: 50%; color: Green;'>
<b>Welcome to Tutorialspoint!</b>
</marquee>This renders HTML directly in your notebook.
You can also create SVG graphics:
%%html
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 400" width="400" height="400">
<rect x="10" y="0" width="300" height="100" style="fill:orange; stroke:black;" />
<rect x="10" y="100" width="300" height="100" style="fill:white; stroke:black;" />
<rect x="10" y="200" width="300" height="100" style="fill:green; stroke:black;" />
</svg>Getting a List of Magics
To see all available magics:
%lsmagicThis displays both line and cell magics available in your environment.
Adding Forms for User Input
Colab provides Forms, a powerful feature for accepting user inputs at runtime.
Creating a Form
Click on the Options menu (vertical dots) in a code cell and select “Add a form.” This adds a form to your cell with a default title.
Adding Form Fields
To add input fields:
- Click the Options menu again
- Select “Form” to reveal submenus
- Choose “Add a form field”
- Configure the field (variable name, type, default value)
Example of an integer input:
#@title Form
sleeptime = 0 #@param {type:"integer"}Form Field Types
Text Input:
name = 'Tutorialspoint' #@param {type:"string"}
print(name)Dropdown List:
color = 'green' #@param ["red", "green", "blue"]
print(color)Date Input:
#@title Date fields
date_input = '2019-06-03' #@param {type:"date"}
print(date_input)Using Form Values
Once you’ve created form fields, you can use the variables in your code:
import time
print(time.ctime())
time.sleep(sleeptime)
print(time.ctime())Installing Machine Learning Libraries
Colab supports most machine learning libraries. You can install them using !pip install or !apt-get install.
Keras
!pip install -q kerasPyTorch
!pip3 install torch torchvisionMxNet
!apt install libnvrtc8.0
!pip install mxnet-cu80OpenCV
!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-pythonXGBoost
!pip install -q xgboost==0.4a30GraphViz
!apt-get -qq install -y graphviz && pip install -q pydotUsing Free GPU
One of Colab’s most attractive features is free GPU access.
Enabling GPU
Select: Runtime / Change runtime type
In the dialog that appears, set “Hardware accelerator” to “GPU” and save.
Testing for GPU
To verify GPU is enabled:
import tensorflow as tf
tf.test.gpu_device_name()If GPU is enabled, this returns: '/device:GPU:0'
Listing Available Devices
To see all devices used during execution:
from tensorflow.python.client import device_lib
device_lib.list_local_devices()This shows details about CPUs, GPUs, and other accelerators available to your notebook.
Checking RAM
To see available memory:
!cat /proc/meminfoThis displays detailed information about your runtime’s memory resources.
Conclusion
Google Colab is a powerful platform for learning and quickly developing machine learning models in Python. It is based on Jupyter notebook and supports collaborative development. The team members can share and concurrently edit the notebooks, even remotely. The notebooks can also be published on GitHub and shared with the general public. Colab supports many popular ML libraries such as PyTorch, TensorFlow, Keras and OpenCV. The restriction as of today is that it does not support R or Scala yet. There is also a limitation to sessions and size. Considering the benefits, these are small sacrifices one needs to make.
