DEV Community

Cover image for Google Colab: Boost your Notebook Experience
PGzlan
PGzlan

Posted on

Google Colab: Boost your Notebook Experience

Google Colab, or Colaboratory, is a free online coding environment that offers a platform for anyone who wants to develop machine learning applications. It's fast, easy to use, and best of all, it gives you access to Google's hardware, including GPUs and TPUs, absolutely free of charge! .

What is Google Colab?

Google Colab is based on Jupyter Notebook, a popular open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. Colab extends the Jupyter Notebook system, enabling you to write and execute Python code (in lucky instances R) in your browser, with zero configuration required, free access to GPUs, easy sharing, and much more.

R runtime

Getting started with Google Colab

Starting with Google Colab is as simple as logging into your Google account and then accessing the Colab at colab.research.google.com.

Once you're there, you can create a new notebook by clicking on File -> New notebook. A new tab will open with your fresh notebook.

The interface is divided into two main areas:

  • The main toolbar which has options to share, save, connect to GitHub, etc.
  • The cell space where you can write your code or text.

Each notebook is made up of cells. There are two types of cells:

  • Code cells: Where you write and execute your code.
  • Text cells: Where you write text or HTML code for explanations, much like in this blog post.

You can add a new cell by clicking on + Code or + Text on the top left corner of your notebook.

Code_text

How to use Google Colab

Let's dive into a simple example that demonstrates how to write, run code, and use the output.

First, add a new code cell and write the following Python code:

print("Hello, Google Colab!")
Enter fullscreen mode Exit fullscreen mode

Click the play button on the left or press Shift+Enter to run the cell. The output will be displayed right below the cell:

Hello, Google Colab!
Enter fullscreen mode Exit fullscreen mode

This is a simple Python code execution. Now, let's see how we can use Google Colab to run a more complex task, such as training a machine learning model. We'll use the popular Iris dataset from the scikit-learn library to train a logistic regression model.

# Importing necessary libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

# Load Iris dataset
iris = load_iris()
X = iris.data
y = iris.target

# Split the data for training and testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define the model
model = LogisticRegression()

# Train the model
model.fit(X_train, y_train)

# Evaluate the model
score = model.score(X_test, y_test)

print(f'Model accuracy: {score*100:.2f}%')
Enter fullscreen mode Exit fullscreen mode

This code first loads the Iris dataset, then splits it into a training set and a test set. A Logistic Regression model is then defined and trained, and finally, the model's accuracy is evaluated and printed.

Using Colab's GPU

One of the significant advantages of Google Colab is the free GPU that Google provides for your computations, which can be incredibly beneficial for tasks like training deep learning models. To use the GPU, you need to change your notebook settings by going to Runtime -> Change runtime type and selecting GPU from the Hardware accelerator dropdown menu.

Once you've done that, you can verify that you're using GPU by running the following code:

import tensorflow as tf

# Check if GPU is available
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  raise SystemError('GPU device not found')
print(f'Found GPU at: {device_name}')
Enter fullscreen mode Exit fullscreen mode

If the GPU is correctly configured, the output will be something like:

Found GPU at: /device:GPU:0
Enter fullscreen mode Exit fullscreen mode

In the current version of colab, you can see the GPU name in the top right corner which has like a small resource monitor

Without GPU With GPU
Without GPU With GPU

Colab Forms

Colab Forms provide an easy way to parameterize code in Google Colab notebooks. From a code cell, you can select Insert β†’ Add form field to add a form field. When you change the value in a form, the corresponding value in the code will change. Forms support many types of fields, including string fields, raw fields, date fields, number fields, boolean fields, and markdown. You can also hide the code associated with a form by selecting View β†’ Show/hide code or using the toolbar above the selected code cell

Form

  • String fields: You can use string fields to allow users to enter text. For example:
text = 'value' #@param {type:"string"}
dropdown = '1st option' #@param ["1st option", "2nd option", "3rd option"]
text_and_dropdown = 'value' #@param ["1st option", "2nd option", "3rd option"] {allow-input: true} 
Enter fullscreen mode Exit fullscreen mode
  • Number fields: You can use number fields to allow users to enter numbers. For example:
number_input = 10.0 #@param {type:"number"}
integer_input = 10 #@param {type:"integer"}
Enter fullscreen mode Exit fullscreen mode
  • Sliders: You can use sliders to allow users to select a value from a range. For example:
number_slider = 0 #@param {type:"slider", min:-1, max:1, step:0.1}
integer_slider = 1 #@param {type:"slider", min:0, max:100, step:1}
Enter fullscreen mode Exit fullscreen mode
  • Date fields: You can use date fields to allow users to select a date. For example:
date_input = '2018-03-22' #@param {type:"date"}
Enter fullscreen mode Exit fullscreen mode
  • Boolean fields: You can use boolean fields to allow users to select a true or false value. For example:
boolean_checkbox = True #@param {type:"boolean"}
boolean_dropdown = True #@param ["False", "True"] {type:"raw"}
Enter fullscreen mode Exit fullscreen mode
  • Raw fields: You can use raw fields to allow users to enter any value without type checking. For example:
raw_input = None #@param {type:"raw"}
raw_dropdown = raw_input #@param [1, "raw_input", "False", "'string'"] {type:"raw"}
Enter fullscreen mode Exit fullscreen mode

Forms_result

Colab Magics

Colab provides a set of commands that provide a mini command language for Google Colab notebooks. They are called "Magics", which are a feature of IPython, which is the kernel used by Google Colab. These commands are orthogonal to the syntax of Python and are extensible by the user with new commands. Magics come in two kinds: Line magics, which are commands prepended by one % character and whose arguments only extend to the end of the current line, and Cell magics, which use two percent characters as a marker (%%), and they receive as argument both the current line where they are declared and the whole body of the cell.

You can list all available magic commands by using the %lsmagic command. This command will display a list of all the available line and cell magics. You can also access the help module for magic commands by using the %magic command. You can also bring up the help module for a specific command by appending a ? to the end of the command, for example %timeit?

lsmagic

magic_help

You can create your own custom magic commands in Google Colab. Magic commands are a feature of IPython, which is the kernel used by Google Colab. IPython has a system of commands called β€˜magics’ that provide a mini command language that is orthogonal to the syntax of Python and is extensible by the user with new commands. Magics come in two kinds: Line magics, which are commands prepended by one % character and whose arguments only extend to the end of the current line, and Cell magics, which use two percent characters as a marker (%%), and they receive as argument both the current line where they are declared and the whole body of the cell

from IPython.core.magic import register_line_magic

@register_line_magic
def greet(line):
    name = line.strip()
    print(f"Hello, {name}!")

# Register the magic command
get_ipython().register_magic_function(greet, magic_kind='line')
Enter fullscreen mode Exit fullscreen mode

Custom line magic demo

You can also use line magic within your code to do some actions such as changing directory

changing directory

Run system commands

You can run system commands in Google Colab by using the ! character before the command.

OS type

Since the underlying host of this notebook is Linux, you can run all the Linux commands you desire.

For example, to run the ls command, you would enter !ls in a code cell and run it. This will execute the ls command and display the output in the cell. You can also use this method to run more complex commands and even chain multiple commands together using &&. For example, to update the package list and then install a package, you could enter !apt-get update && apt-get install -y package-name in a code cell and run it.

This can be useful for cases when you want to perhaps do some processing on some video (perhaps extract a part of it based on timestamps) with a certain tool such as ffmpeg, you can run your ffmpeg command before executing your code

Conclusion

Google Colab is a powerful tool for anyone who wants to dive into machine learning without worrying about setting up an environment or the computational resources (you can also use it to test out things quickly if especially if you're testing something Linux related). It has an easy to get into user interface for coding and collaboration, and the free access to Google's hardware makes it a go-to choice for many data scientists and researchers.

So, start integrating Google Colab into your workflow to take it to the next level

Note: While Google Colab is a fantastic resource, remember that it is not meant for long, continuous usage. The session will disconnect after a period of inactivity. You can check around for how you can reduce the likelyhood of that happening

Oldest comments (0)