4 mins read

Google Colab: working machine learning in the cloud with Python

colorful toothed wheels
Photo by Digital Buggu on Pexels.com

Google Colab is a tool that allows you to create an executable document in several programming languages. This document allows you to write, execute and share code (hence the colab or collaborative). Part of the so-called Jupyter project  that is available on Google Drive. A notebook document is made up of cells, each of which can contain code, text, images, and so on. We could make the simile that is like a text document that contains boxes inside that allow you to execute code.

Colab connects your notebook to a runtime environment that allows you to load all the applications in a program and run them on a cloud-based platform, which means you can run Python code without prior installation on your device. Python is installed in the cloud-hosted runtime environment that automatically connects your open session.

Introduction to Google Colab

Colab, or “Colaboratory”, allows you to write and execute Python code in your browser, with no configuration required, with free access to a GPU (Graphics Processing Unit) and ease of sharing. Whether you’re a student, data scientist , or AI researcher, Colab makes your job easier. This introductory video about Colab will allow you to start using it from 0.

Integrated library

We can search Colab’s built-in library for code snippets and embed code to create an interactive visualization of the data. This visualization is created with Altair, one of several visualization libraries supported by Colab.  Altair is a declarative statistical visualization library for Python. Altair offers a powerful and concise visualization grammar to quickly build a wide range of statistical graphs.

By declarative, it means that you can provide a high-level specification of what you want to include visualization, in terms of data, graphic marks, and coding channels, rather than having to specify how to implement visualization in terms of for-loops, low-level drawing commands, and so on. The key idea is to declare links between data fields and visual coding channels, such as the x-axis, y-axis, color, and so on. The rest of the chart details are handled automatically. Based on this idea of declarative plotting, a surprising range of simple to sophisticated visualizations can be created using concise grammar.

Example of code whose result can be seen in Fig. 1:

import altair as alt
from vega_datasets import data

source = data.disasters.url

    alt.X('Year:O', axis=alt.Axis(labelAngle=0)),
        scale=alt.Scale(range=[0, 4000]),
        legend=alt.Legend(title='Annual Global Deaths')
    alt.Color('Entity:N', legend=None)
    alt.datum.Entity != 'All natural disasters'
Google Colab
Figure 1 Global annual deaths from natural disasters

Share notebooks

Notebooks are shared as a Google document, and it is quite useful to use text cells that explain what is being done. It is very similar to Markdown, which as the name suggests is a lightweight markup language created that tries to achieve maximum readability and ease of publication both in its input and output form, drawing inspiration from many existing conventions for marking email messages using plain text.

Text cells are formatted with Markdown. The Markdown format is simple and powerful, allowing you to add titles (headings), paragraphs, lists, and even mathematical formulas. Titles are very useful because they divide the document into parts, and you can create indexes with different levels as shown in Fig. 2.

Google Colab
Figure 2. Index in Google Colab

To share your notebooks with others you can do it through Google Drive or even exporting your notebook to GitHub. The notebook is stored in the Jupyter notebook format. The notebooks you create can be viewed and run on Jupyter Notebook, JupyterLab, and other supported systems.

Seedbank Project

A useful collection is the Seedbank project in research.google.com/seedbank. For example, the Neural Style Transfer seed  shows how to use deep learning to transfer styles between images and is connected to a Colab notebook where you can run and change the code. You can see a copy on https://colab.research.google.com/drive/1TcSQgGllR0YWjKOKMsmgzlT_Y1phC4l1?usp=sharing.

It is a tutorial that through deep learning images are created with the style of another image. This is known as neural style transfer! This is a technique described in Leon A. Gatys’ article, A Neural Algorithm of Artistic Style, which is very interesting to read and sure gives you a lot of ideas. The result can be seen in the following figure where the before and after are shown.

Connecting Google Colab to Google Drive

From your notebook you can save a copy to your Google Drive. It is to be placed in the Colab Notebooks folder. This copy will allow you to run a notebook and save your changes to GoogleDrive. In addition, once a notebook is opened you can download it to work in other Python or R desktop environments, since you have the option to download the file .py or .ipynb.

Integration with R

You can work on R, a statistics-oriented programming language from your notebook. To use the notebook directly with R you have to:

  1. Open a browser.
  2. Go to this address: https://colab.research.google.com/#create=true&language=r, or use this shortened URL https://colab.to/r

After accessing the URL, a new notebook will be created that will be named Untitled.ipynb. In principle, there is no difference between notebooks with Python or R. However, in the Notebook settings you can verify that the environment is R.

Examples of Machine Learning in Google Colab

Next, I share several documents made of Machine Learning that will surely be interesting to you.


Avelino Dominguez

👨🏻‍🔬 Biologist 👨🏻‍🎓 Teacher 👨🏻‍💻 Technologist 📊 Statistician 🕸 #SEO #SocialNetwork #Web #Data ♟Chess 🐙 Galician

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Previous Story

Google Colab: trabajando machine learning en la nube utilizando Python

Next Story

Cómo mejorar el SEO de tu página web en 2022

%d bloggers like this: