How to use Google Colab

How to use Google Colab
What is Google Colab?
Google Colab is an interactive, all-cloud, easy-to-use, and colaboratory programming environment for those working on artificial intelligence and deep learning projects. It is free of cost and no software installation is required. If you are familiar with Jupyter notebook, you can think of Google Colab as a powered version of Jupyter notebook with multiple useful features hosted on Google’s cloud servers.
How to use Google Colab?
You can click here to start using Google Colab. When you click on the link, a window like the one below will open. We will call our workspace notebook.
Before moving on to the notebook, let’s get acquainted with the simple settings. The settings button is next to our profile picture. From here we can control the settings of our Colab. Let’s start with the theme of our page first.
In terms of theme, Google Colab gives you 3 options:
- light – work on a white sheet.
- dark – a black work.
- adaptive – adjusting the colour of the page according to the time of day.
When you click the settings button, you can customise many settings such as font size, indentation, showing line numbers, connecting to Github, etc., apart from the theme, from the selections in the small window that opens.
Now let’s move on to the Colab Notebook. To create a new notebook, click File, then New notebook.
Note:
- New notebook – to create a new Interactive Python notebook,
- Upload notebook – to open an existing .ipynb file in Colab,
- Open notebook – to view and open a notebook already existing on the drive.
We see a page like the one below and by default the notebook named Untitled0.ipynb opens. We can change the name of the notebook as we wish.
Let’s create our first code block on Google Colab:
Now let’s explore the Google Colab toolbar more closely:
- File – Changing the location or name of the notebook, saving, opening/creating/loading a new notebook, etc.,
- Edit – Selecting, copying, deleting, pasting, finding and replacing cells, etc.
- View – Check the metadata related to the notebook, view the code history, hide the code or output in the selected cell, etc.
- Insert – Adding a new code cell, text cell or heading, etc.
- Runtime – Run all or only selected cells, stop execution, change hardware accelerator (GPU, TPU), etc.
- Tools – Like going to shortcuts, settings and commands etc.
- Help – Provides options for reading documentation, giving feedback, etc.
Now let’s see how we can comment on someone else’s or our own notebook or share our code with others:
Library installations
We can use the Linux terminal commands by having “!” character before the command. For example, we can load the libraries that are not installed by default by typing the following command in the code cell:
!pip install [library_name] |
Default useful shortcuts:
Shortcut settings with the help of Control (or Command) + M + H. Top 10 useful shortcuts:
Run the selected cell | Ctrl / Cmd + Enter |
Run the selected cell and switch to the new cell | Shift + Enter |
Adding code cell above | Ctrl / Cmd + M + A |
Add code cell below | Ctrl / Cmd + M + B |
Delete the selected cell | Ctrl / Cmd + M + D |
Stop code execution | Ctrl / Cmd + M + I |
Convert selected cell to code cell | Ctrl / Cmd + M + Y |
Convert selected cell to text cell | Ctrl / Cmd + M + M |
Convert selected lines to comments | Ctrl / Cmd + / |
Find and replace | Ctrl / Cmd + M |
Making the above shortcuts a habit will definitely speed up your work. At the same time, you can customise all the shortcuts according to your wishes from the shortcut settings!
Google Colab tricks that save lives
-
“Snippets”!
A “snippet” is a reusable piece of code that you can use whenever you want. If you’ve done exploratory data analysis in python at least once, you probably remember the following code:
import numpy as np import pandas as pd import matplotlib.pyplot as plt |
We can save these or more libraries so that we don’t have to rewrite the import process or any heavily used code every time. To do this, Tools -> Settings -> Paste custom snippet notebook URL! It is sufficient to perform the operations. Afterwards, you can make your work easier by accessing this or a ready-made “snippet” for a while.
Let’s choose one of the random snippets and click on insert:
That’s it!
2. To stop the disconnection
You decide to take a break from working at Google Colab, but when you return, your computer is disconnected from Colab! In fact, Google Colab will automatically disconnect if the computer is left idle for more than 30 minutes. If we run the code below in the console, which is opened by clicking F12, a click is made on the colab screen every 60 seconds. Thus, Colab assumes that the computer is not idle and your connection is not interrupted.
function KeepClicking(){ console.log(“Clicking”); document.querySelector(“colab-toolbar-button#connect”).click() }setInterval(KeepClicking, 60000) |
Google Colab advantages and disadvantages
Advantages:
- Pre-installed libraries: Google Colab includes predefined libraries for machine learning and deep learning and requires no installation for them.
- Collaboration: Another great feature that Google Colab offers is the collaboration feature. If you’re working on a project with more than one friend, Google Colab is an ideal choice.
- Access from any device: You can access your codes from any device with a simple Google sign-in process. All your Google Colab notebooks are saved in your Google Drive account, just like your Google Docs and Google Sheets files.
Disadvantages:
- Repeating the same operations every time: Except for connecting to the local runtime, you have to reinstall libraries that are not included in the standard Colab package each time you use it.
- Working at the same time: Sharing the code you wrote with your friend allows you to collaborate. However, the inability of two people to write or edit code at the same time slows down the project.
- Storage issue: Since Google Colab does not provide permanent storage, the uploaded files are removed when the session is restarted. Therefore, data is lost when the device is turned off and it is necessary to reload it each time.
- Limited space: Google Colab files are stored in 15GB free space in Google Drive; however, working on larger datasets requires more space.
- Limited time: Google Colab allows users to run their notebooks for a maximum of 12 hours a day, but to work longer, users need access to the paid version, Colab Pro, which allows programmers to stay connected around the clock.
What is Google Colab Pro / Colab Pro +?
Apart from the plan offered to us for free, there are also Colab Pro and Colab Pro + plans. Let’s take a look at the differences between them:
colab | Colab Pro | Colab Pro + | |
GPU | K80 | K80, T4, and T100 | K80, T4, and T100 |
RAM | 16 GB | 32GB | 52GB |
Operating environment | 12 hours | 24 hours | 24 hours |
Running in the background | No | No | Yup |
Price | Free | $9.99/month | $49.99/month |
The free version is useful for people who are at the beginning of their careers in this field and working on machine learning and deep learning projects that do not require very large memory, Colab Pro is useful for people who have been working in this field for one or more years, and Colab Pro + is useful for people who do projects that require heavy and very large memory.
You’ve learned all the basics you need to start coding at Colab, and more. No setup required, so you can start building your machine learning models from your phone, tablet, or laptop as long as you have an internet connection!
If you want to learn Python, don’t hesitate to sign up and begin your educational journey with Global AI Hub.
references
[2] 5 Amazing Google Colab Hacks You Should Try Today!
[3] Choose the Colab plan that’s right for you
[4] Google Colab Pro+: Is it worth $49.99?