30

Google Colab — Your Python workspace on cloud

 4 years ago
source link: https://www.tuicool.com/articles/NneAR36
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Google Colab — Your Python workspace on cloud

Learn about Google Colab

i6beUvM.jpg!web

Photo by Kaitlyn Baker on Unsplash

Today, I switched to a new machine for my development. While there is a learning curve attached to get aligned with the new machine, I can still continue to work on my projects and pursue my online courses through Google Colab. It’s practically as simple as logging into my Google account and I’m all set.

Thus, I decided to share my insights on why I find Google Colab so useful and the commands that have helped me use it more effectively without much effort.

What is Google Colab?

ZvAVBfZ.png!web

Google Colab introductory notebook

Google Colab, also know as Colaboratory, is a Jupyter environment provided and supported by Google with the option to work with CPUs, GPUs and even TPUs. It’s like any other Jupyter notebook where we can code in Python and write descriptions as markdown along with all the other Jupyter features and a lot more.

The top section includes all the controls to save notebooks, share notebooks to GitHub repositories, change runtime environments, run cells and a lot more. The bottom section is divided into a left panel which includes the table of contents and files, and has the complete functioning notebook on the right.

Why Google Colab?

neyMr2Y.jpg!web

Photo by freestocks.org on Unsplash

While there are many reasons why one might prefer to use a service like Google Colab for their work, following are reasons it works for me:

  1. GitHub integration: While the notebooks are saved to our Google drive, I prefer to also share some of the notebooks as part of their own GitHub repository. Google Colab allows a direct share to any repository after linking our GitHub profile.
  2. Zero set up time: To get started with Google Colab, all you need to do is go to https://colab.research.google.com/ and select NEW PYTHON 3 NOTEBOOK from the popup menu.
  3. Platform independence: As the Jupyter notebooks can be accessed directly from a browser, we can have any machine, Mac, Windows, Linux etc. and it’ll work exactly the same.
  4. Free resource availability: Training models for deep learning require a lot of power and hence, not all laptops and desktops are equipped for it. Google Colab provides free access to GPUs which we can leverage for our deep learning tasks.

Exploring Google Colab

Now that we understand what Google Colab is and how many benefits it brings to the table for everyone, let’s explore a few key important aspects. These have surely helped me in increasing my productivity and making my work more accessible.

Quick keyboard shortcuts

Moving across cells is easy. You just need to use the UP and DOWN arrow keys on the keyboard. To start editing a cell, simply press Enter . To escape from editing a cell, simply press Esc .

There are already some predefined keyboard shortcuts that you can use. Some of the most basic include Shift + Enter to run a cell, Ctrl + M Y to convert a cell to code cell, Ctrl + M M to convert a cell to text cell and more. You can get the complete list by going to Tools > Keyboard shortcuts... You can customize and change the shortcuts as you like.

Apart from the predefined ones, I customized two more shortcuts according to my need. As the shortcuts that deal with cells start with the combination of Ctrl + M , I decided that I’d keep the same while configuring my own shortcuts. For creating a new code cell, I created the shortcut Ctrl + M C . For creating a new text cell, I created the shortcut Ctrl + M T .

Similar shortcuts exist and can be configured for Mac as well using the Command Key .

Load datasets

Google Colab allows us to get access to our datasets via a number of ways.

Upload dataset

On the left panel, select the Files heading. Select the option Upload and select the dataset file.

7fimqqq.png!web

dataset.csv uploaded and being read through pandas

However, the files are removed at runtime refresh so if somehow the runtime crashes without completing its task, you’ll have to upload them again.

Load dataset from Google drive

Secondly, we can import the datasets that are already saved in Google drive. This is beneficial as we do not need to upload the dataset again if the notebook crashes.

To mount the Google drive, we use the following code:

On running this code, a link is provided which allows us to login into our account and presents an authorization token which needs to be entered in the space provided for it to work. This enables us to access the contents of our Google drive using the My Drive folder inside /content/drive .

nARbArN.png!web

Loading dataset from Google drive

Save to GitHub repository

One of the most fascinating and useful features for me has been the integration between Google Colab and GitHub. I prefer to work on Jupyter notebooks and save my work to specific GitHub repositories in my account. Google Colab makes the whole process hassle-free and easy to use.

From the File menu, we select the option to Save a copy in GitHub... . It loads up a screen to link our GitHub account, which is a one-time process after which, we are greeted with another popup. We can select the repository, the branch, the name of the file and the commit message. It’s that simple!!

ANB7z2J.png!web

Save notebook to GitHub

We can also add a link to this notebook in the GitHub repository, so anyone viewing the notebook on GitHub can quickly jump right into working.

R363emI.png!web

Open in Colab option in the notebook saved via Google Colab

Changing runtime type

If you’ve ever worked with deep learning models, you would understand the role a GPU can play in increasing the speed of training many-fold. Google Colab provides the option to add a GPU/TPU for our use totally free of charge. This allows us to train our deep learning models at much higher rates.

From the Runtime menu, select Change runtime type . Under hardware accelerator, we can either choose None to continue with CPU or change the runtime to GPU or TPU as needed.

RzYfauj.png!web

Select between various hardware acceleration options

Once you click SAVE , the runtime will refresh and you can notice that on the top right, CONNECTING shows up to signify that we’re now activating the new runtime type.

Sharing Colab notebooks

Another significant feature is to share our work with the world. The share option at the top right corner of Google Colab allows us to select a list email addresses to share the notebook with or simply get a shareable link.

MJrEFvQ.png!web

Sharing the notebook

Conclusion

Google Colab is a great way to keep up with your work and access it wherever you want. It has many features such as creating easy shortcuts, sharing and saving notebooks and more.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK