How to use Google Colab

If you want to create a machine learning model but say you don’t have a computer that can take the workload, Google Colab is the platform for you. Even if you have a GPU or a good computer creating a local environment with anaconda and installing packages and resolving installation issues are a hassle.
Colaboratory is a free Jupyter notebook environment provided by Google where you can use free GPUs and TPUs which can solve all these issues.

Getting Started

To start working with Colab you first need to log in to your google account, then go to this link https://colab.research.google.com.

Opening Jupyter Notebook:
On opening the website you will see a pop-up containing following tabs –



EXAMPLES: Contain a number of Jupyter notebooks of various examples.
RECENT: Jupyter notebook you have recently worked with.
GOOGLE DRIVE: Jupyter notebook in your google drive.
GITHUB: You can add Jupyter notebook from your GitHub but you first need to connect Colab with GitHub.
UPLOAD: Upload from your local directory.

Else you can create a new Jupyter notebook by clicking New Python3 Notebook or New Python2 Notebook at the bottom right corner.

Notebook’s Description:

On creating a new notebook, it will create a Jupyter notebook with Untitled0.ipynb and save it to your google drive in a folder named Colab Notebooks. Now as it is essentially a Jupyter notebook, all commands of Jupyter notebooks will work here. Though, you can refer the details in Getting started with Jupyter Notebook.

Let’s talk about what different here.

Change Runtime Environment:
Click the “Runtime” dropdown menu. Select “Change runtime type”. Select python2 or 3 from “Runtime type” dropdown menu.

Use GPU and TPU:
Click the “Runtime” dropdown menu. Select “Change runtime type”. Now select anything(GPU, CPU, None) you want in the “Hardware accelerator” dropdown menu.

Verify GPU:

filter_none

edit
close

play_arrow

link
brightness_4
code

import tensorflow as tf
tf.test.gpu_device_name()

chevron_right


If gpu is connected it will output following –

'/device:GPU:0'

Otherwise, it will output following

''

Verify TPU:



filter_none

edit
close

play_arrow

link
brightness_4
code

import os
  
if 'COLAB_TPU_ADDR' not in os.environ:
  print('Not connected to TPU')
else:
  print("Connected to TPU")

chevron_right


If gpu is connected it will output following

Connected to TPU

Otherwise, it will output following

Not connected to TPU

 
Install Python packages –
Use can use pip to install any package. For example:

filter_none

edit
close

play_arrow

link
brightness_4
code

! pip install pandas

chevron_right


 
Clone GitHub repos:
Use git clone command. For example:

filter_none

edit
close

play_arrow

link
brightness_4
code

! git clone https://github.com/souvik3333/Testing-and-Debugging-Tools

chevron_right


 
Upload File:

filter_none

edit
close

play_arrow

link
brightness_4
code

from google.colab import files
uploaded = files.upload()

chevron_right


Select “Choose file” and upload the file you want. Enable third-party cookies if they are disabled.

Then you can save it in a dataframe.

filter_none

edit
close

play_arrow

link
brightness_4
code

import io
df2 = pd.read_csv(io.BytesIO(uploaded['file_name.csv']))

chevron_right


Upload File By Mounting Google Drive:
To mount your drive inside “mntDrive” folder execute following –

filter_none

edit
close

play_arrow

link
brightness_4
code

from google.colab import drive
drive.mount('/mntDrive')

chevron_right


Then you’ll see a link, click on link, then allow access, copy the code that pops up, paste it at “Enter your authorization code:”.

Now to see all data in your google drive you need to execute following:



filter_none

edit
close

play_arrow

link
brightness_4
code

! ls "/mntDrive/My Drive"

chevron_right


File Hierarchy:
You can also see file hierarchy by clicking “>” at top left below the control buttons (CODE, TEXT, CELL).

Download Files:
Let’s say you want to download “file_name.csv”. You can copy the file to your google drive (In “data” folder, you need to create the “data” folder in google drive) by executing this:

filter_none

edit
close

play_arrow

link
brightness_4
code

cp file_name.csv "/mntDrive/My Drive/data/renamed_file_name.csv"

chevron_right


The file will be saved at “data” folder with “renamed_file_name.csv” name. Now you can directly download from there, Or, you can just open file hierarchy and right clicking will give download option.

Download Jupyter Notebook:
Click “File” dropdown menu at top left corner. Choose “download .ipynb” or “download .py”

Share Jupyter Notebook:
You can share your notebook by adding others email address or by creating a shareable link.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.