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. In this article, we'll learn how to use google colab.
What is Google Colab?
Google Colab, short for Colaboratory, is a free cloud-based platform provided by Google that allows users to write and execute Python code collaboratively in a Jupyter Notebook environment. Google Collaboratory notebook, is designed to facilitate machine learning (ML) and data science tasks by providing a virtual environment, Google colab python with access to free GPU resources.
Benefits of Google Colab
Google Colab offers several benefits that make it a popular choice among data scientists, researchers, and machine learning practitioners. Key features of Google Collaboratory notebook include:
- Free Access to GPUs: Colab offers free GPU access, which is particularly useful for training machine learning models that require significant computational power.
- No Setup Required : Colab runs in the cloud, eliminating the need for users to set up and configure their own development environment. This makes it convenient for quick coding and collaboration.
- Collaborative Editing: Multiple users can work on the same Colab notebook simultaneously, making it a useful tool for collaborative projects.
- Integration with Google Drive : Colab is integrated with Google Drive, allowing users to save their work directly to their Google Drive account. This enables easy sharing and access to notebooks from different devices.
- Support for Popular Libraries :Colab comes pre-installed with many popular Python libraries for machine learning, data analysis, and visualization, such as TensorFlow, PyTorch, Matplotlib, and more.
- Easy Sharing :Colab notebooks can be easily shared just like Google Docs or Sheets. Users can provide a link to the notebook, and others can view or edit the code in real-time.
Getting Started With Google Colab
To start working with Google Collaboratory Notebook you first need to log in to your Google account, then go to this link https://colab.research.google.com .
Open Collaboratory Notebook
On opening the website you will see a pop-up containing the following tabs -
Google Collaboratory Notebook
- 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.
Create Collaboratory Notebook
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
Google Collaboratory Notebook
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 to the details in Getting Started with Jupyter Notebook .
Let's talk about what is different here:
Change Runtime Environment: Click the "Runtime" dropdown menu. Select "Change runtime type" . Select python2 or 3 from the "Runtime type" dropdown menu.
Runtime setting in Google colab
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.
GPU and TPU in Google Colab
Select python in colab
Verify GPU in Colab
Python
import tensorflow as tf
tf.test.gpu_device_name()
If GPU is connected it will output the following -
'/device:GPU:0'
Otherwise, it will output following
''
Verify TPU
Python
import os
if 'COLAB_TPU_ADDR' not in os.environ:
print('Not connected to TPU')
else:
print("Connected to TPU")
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:
Python
Clone GitHub repos in Google Colab
Use the git clone command. For example:
Python
! git clone https://github.com/souvik3333/Testing-and-Debugging-Tools
Upload File on Google Colab
Python
from google.colab import files
uploaded = files.upload()
Select "Choose file" and upload the file you want. Enable third-party cookies if they are disabled.
.webp)
Then you can save it in a dataframe.
Python
import io
df2 = pd.read_csv(io.BytesIO(uploaded['file_name.csv']))
Upload File By Mounting Google Drive
To mount your drive inside the "mntDrive" folder execute the following -
Python
from google.colab import drive
drive.mount('/mntDrive')
Then you’ll see a link, click on the link, then allow access, copy the code that pops up, and paste it at "Enter your authorization code:". Now to see all data in your google drive you need to execute the following:
Python
! ls '/mntDrive/My Drive"'
Uploading files on google colab
File Hierarchy In Google Colab
You can also see the file hierarchy by clicking ">" at the top left below the control buttons (CODE, TEXT, CELL).


Download Files from Google Colab
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:
Python
cp file_name.csv "/mntDrive/My Drive/data/renamed_file_name.csv"
The file will be saved in the "data" folder with the "renamed_file_name.csv" name. Now you can directly download from there, Or, you can just open the file hierarchy and right-clicking will give a download option. Download Jupyter Notebook: Click the "File" dropdown menu at the top left corner. Choose "download .ipynb" or "download .py"
Downloading files from Google colab
Share Jupyter Notebook: You can share your notebook by adding others' email addresses or by creating a shareable link.
Share jupyter notebook in google colab
Share Google colab Notebook
Conclusion
In conclusion, Google Colab stands out as a versatile and accessible platform for Python coding.
Similar Reads
How to run Python code on Google Colaboratory
Prerequisite: How to use Google Colab Google provides Jupyter Notebook like interface to run Python code on online Virtual Machines. In this article, we will see how to run simple Python code on Google Colab. Step #1: Open https://colab.research.google.com/ Step #2: Select New Python3 Notebook Step
2 min read
How to Scrape Web Data from Google using Python?
Prerequisites: Python Requests, Implementing Web Scraping in Python with BeautifulSoup Web scraping is a technique to fetch data from websites. While surfing on the web, many websites donât allow the user to save data for personal use. One way is to manually copy-paste the data, which both tedious a
2 min read
Download Anything to Google Drive using Google colab
When we download/upload something from a cloud server, it gives more transfer rate as compared to a normal server. We can use Google Drive for storage as well as fast speed download. The problem is how to upload something to G-Drive direct from Internet. So, Here we will see a solution to upload any
2 min read
Python | Automate Google Search using Selenium
Google search can be automated using Python script in just 2 minutes. This can be done using selenium (a browser automation tool). Selenium is a portable framework for testing web applications. It can automatically perform the same interactions that any you need to perform manually and this is a sma
3 min read
How to use gpu in google colab?
Google colab is a service provided by Google for a lot of researchers and developers around the globe. It is a Jupyter Notebook-like environment in one single place without any prerequisites. It is free to use with a limited number of computer resources and engines including free access to GPUs i.e.
3 min read
How to use R with Google Colab?
Google Colab is a cloud based coding platform providing free access to limited resources like GPU or TPU. It comprises cells where one can write codes and execute them. It also provides the facility of sharing in real time. Some of the features are:It allows users to share notebooks in real time.It
3 min read
How to upload folders to Google Colab?
Google Colab is a service provided by Google for a lot of researchers and developers around the globe. It is a Jupyter Notebook-like environment in one single place without any prerequisites. It is free to use with a limited number of computer resources and engines including free access to GPUs i.e.
4 min read
How to Open web camera in 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. In this article, we will see how we can open a camera in Google Colab. What is Google Colab?Google offers the Google Colab service to numerous researcher
3 min read
How to run Flask App on Google Colab?
Flask is a web framework in python language. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a machine learning webapp. Googl
2 min read
How to update Google Colab's Python version?
The importance of having a current version will be covered in this article, especially for activities involving machine learning and data science. Python versions that are out-of-date or extremely old may experience a number of issues, one of which is incompatibility with recent packages. The usage
7 min read