An Absolute Beginner’s Guide To Google Colaboratory

Google Colab is a free cloud service that, in it’s own words, “allows you to share Jupyter notebooks with others without having to download, install, or run anything on your own computer other than a browser!” Now you may be asking,

What Are Jupyter Notebooks?

Jupyter Notebook is an open-source web application that gives you the power to create and share documents that have live code, equations, visualizations, and narrative text. Jupyter Notebook is a interactive notebook that has many applications. It supports over 40 programming languages including Python, R, Julia, Scala, and even more (meaning that it can run code written in all those languages) It’s even been used for machine learning! Your Jupyter Notebooks are stored in somewhere aptly named as “my binder” which holds all those notebooks.

Where Do I Start?

To start using Google Colab, you need to make sure you have the latest version of Chrome or Firefox, since Colaboratory has been thoroughly tested with those desktop versions. You can install Chrome straight from Google here and you can install Firefox from Mozilla here. Once you have that done, you can head over to Colaboratory, where you will be greeted with this welcome page in your notebook.

Colaboratory Welcome Page (Google)

You will most likely not have the “Overview of Colaboratory Features” on there yet, since that is a recently viewed notebook that I opened, which you will open too!

Open up the “Hello, Collaboratory” Notebook. Don’t worry about not understanding everything that it says, especially any of the TensorFlow stuff, as that is most likely not going to help you on using Notebooks but is for those who create Notebooks. Either way, you can run lots of languages on Notebooks, like I said before, and TensorFlow is not the only language that Jupyter Notebooks supports :)

Running Code in Notebooks

Before we just into learning more about using Colaboratory, you should know how to get around. You can access different sections using the Table of Contents to the left of your page, as seen below.

Table of Contents on Colaboratory (Google Colaboratory)

Head over to the “TensorFlow execution” section and select the code you see on the main part of the Notebook page. As you can see below, you can hit the run button, and code will run within the Notebook, giving you a live demo!

Google Colaboratory can also show live visualizations, save a copy of your notebook to Github, allow you to install new libraries to use in the code on your notebook, and even create forms right inside the notebook! Checkout the “Hello, Collaboratory”’s “Getting Started” links to learn more about each feature.

GPU/TPI Runtime Acceleration Support

Google Colaboratory supports GPU/TPU runtime acceleration support! TPU stands for “Tensor Processing Unit” which is a chip designed for Google’s math library (TensorFlow) which is used for machine learning (like neural networks). GPU stands for Graphics Processing Unit and is a card that your computer uses for graphics. GPU/TPU acceleration is using the GPU and CPU (Central Processing Unit) in your computer for tasks that use up a lot of power, like deep learning, analytics, and engineering applications. Google Colab supports the use of GPU/TPU in the applications in your Notebook, so you can run neural networks all without leaving your browser (Since you’ll be using Google’s TPU’s and CPU’s to run those applications, there’s a time limit to how long you can use them, which is around 12 hours) Google Colab also supports connecting to a Jupyter runtime on your local machine. For more info, check out Google Colab’s documentation.

Google Colab and Google Drive

Google Colab supports the use of Google Drive to load datasets, and even upload or download models to and from the notebooks you use and your local machine. All you have to do is link your Google Drive account to Google Colab by running this snippet of code:

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

(taken from http://tiny.cc/uvmc1y)

A link will appear. Click the link, select your google account, and copy the verification code given to you. Go back to your notebook where you ran the code snippet above, paste the verification code into the empty box that appears, and hit enter. You will have to do this twice.

Next, make a directory on your notebook for all your drive files:

!mkdir -p drive
!google-drive-ocamlfuse drive

After that, to access a file already in your Google Drive, just replace the /FILE part below to the filename you want to access. Note: if you want to access a file in a folder in your drive, place the folder name after drive with a / then the filename. For example, here I’m accessing the file graph.csv that’s in a folder called Projects.

import pandas as pd
pd.read_csv("drive/Projects/graph.csv")

Using Colab with Github

You can load notebooks to and from Github! To load a notebook from Colab to Github, you first create a repository on Github, open the Notebook and select File → Save a copy to Github… and that’s all!

Other Cool Features

There’s a Open in Colab extension you can download to open files in Google Colaboratory. Install it here. You can also use Markdown in your Notebooks. Here’s a Markdown Cheatsheet. Google Colaboratory has a free machine learning crash course which you can find here.

Now go out there and make the notebooks you wish you had back in school!