Deep Learning in a cloud. How to get started with Google Colab and why?

Contents Outline

Deep Learning in a cloud. How to get started with Google Colab and why?

May 07, 2020 5 minutes read

Image by FelixMittermeier from Pixabay



Introduction

In this article, I will introduce you to Google Colab which is a tool similar to Jupyter notebook that allows you to run the code in the cloud. I will walk you through:

What Google Colab is and why you should start using it?
How to install Google Colab?
Writing your first function in Google Colab.
Loading data file in Google Colab.
How to turn on GPU and TPU in Google Colab and how to use use it for deep learning.

After reading this article, you should be ready to start using Google Colab for your Data Science projects, take advantage of running your code in the cloud, and using free GPU /TPU that is offered by this tool.

Let’s get started!


What Google Colab is and why you should start using it?

Easy collaboration

As the name suggests Google Colab is a tool that was released by Google and similarly to other Google products it is available in the cloud and allows collaborative editing. This means that you can easily share the notebook with other people, allow them to view the code, or make them collaborators.

Preinstalled Data Science libraries

Google Colab allows you to write and run python code interactively the same way as Jupyter Notebook does. Additionally, it comes with most Data Science libraries preinstalled making it the ideal tool for beginners wanting to start on the Machine Leaning projects straight away. Libraries such as pandas, numpy, Tensorflow, Keras, OpenCV are already installed so there is no need for running ‘pip install’ and struggling with local environment installation.

Free GPU / TPU

Additionally, it offers free GPU /TPU! This means that training neural networks will be faster and can be accessed by people who do not have very powerful machines. Even if you have a powerful machine Google Colab will allow you to free your machine from heavy work and delegate it to a cloud.


How to install Google Colab?

In order to start with Google Colab follow these simple steps.

1. Navigate to your Google Drive Page.

2. While you are in Google Drive, click on the ‘+ New’ button.


3. From ‘+ New’ dropdown select ‘More’, and then from additional dropdown select ‘+ Connect more apps’.


4. Now you should be able to see a window with G Suite Marketplace. Type Colaboratory in the search box and press Enter to confirm the selection.


5. Now you should be able to see the Google Colab program as a possible selection.


6. Now select ‘Colaboratory’ by clicking in its icon and you will be prompted with the installation button. Follow the installation guidance appearing on-screen and you will be able to use Google Colab right after that.


Writing your first function in Google Colab

Once you have installed Google Colab it will appear on your dropdown when selecting the ‘+ New’ button from Google Drive.


By clicking ‘Google Colaboratory’ the system will open a Google Colab file in a new window. Now you are able to start using the file as a normal Jupyter notebook. You can test it with a simple hello world function.

def hello_world():
    print ('hello world')


Once tested you can start using in the same way as Jupyter notebook you are used to. You can import libraries using normal imports, run python code, and add markdown with text as it was a normal Jupyter notebook file.

Now I will show you how to get a data file loaded in Google Colab as it is slightly different from what you are used with Jupyter Notebook.


Loading data file in Google Colab

There are two options for loading a file from Google Colab. You can either load it from your local file system or add files to Google Drive and connect Google Colab to use the files stored in the drive. I suggest that you do the latter.

In order to connect Google Drives with Colab run this code in one of the Colab files.
from google.colab import drive
drive.mount('/content/drive')

You will be prompted to open the link where you will be asked to allow Google Drive File Stream.


Click the ‘Allow’ button and then you will be given the authorization code. Use this code and paste it Google Colab file where you have been mounting the drive. It will take a while and your Google Drive should be ready to be used. You should be able to see ‘Mounted at /content/drive’ message.


Now you can start using the files in your Google Colab notebook from your Drive. Let’s see an example. I have a file called ‘numbers’ in my Google Drive. I can load it to pandas data frame using the following code.

import pandas as pd
df = pd.read_csv('/content/drive/My Drive/numbers.csv')


In order to access any file in Google Drive you just need to use ‘/content/drive/My Drive/’ + filename as a path.


How to turn on GPU / TPU and use it for deep learning

Let’s finish this tutorial with what I think is the biggest advantage of the Google Colaboratory tool: using free GPU or TPU. Additionally, this could not be more simple than using another dropdown menu.

Navigate to ‘Runtime’ and select ‘Change runtime type.


Now you will be prompted with a box where you will have three options: None, TPU, and GPU. Select the one you would like to use for your neural network training.


Once you have selected the option just run your Keras or Tensorflow code as normal. If your ANN and data set are big enough you should see a significant decrease at each epoch training time while using GPU or TPU.


Summary

You are ready to start using Google Colab now for your Data Science projects. I hope you will enjoy its capabilities and take advantage of its collaborative nature and free GPU. If you are looking for some project ideas check the data resources I have listed in this article:
Join our private community in Discord

Keep up to date by participating in our global community of data scientists and AI enthusiasts. We discuss the latest developments in data science competitions, new techniques for solving complex challenges, AI and machine learning models, and much more!