Wednesday, October 28, 2026
HomeArtificial IntelligenceGoogle Colab for Machine Studying Tasks

Google Colab for Machine Studying Tasks

[ad_1]

Final Up to date on April 30, 2022

Have you ever ever wished an easy-to-configure interactive surroundings to run your machine studying code that got here with entry to GPUs without spending a dime? Google Colab is the reply you’ve been in search of. It’s a handy and straightforward to make use of method to run Jupyter notebooks on the cloud and their free model comes with some restricted entry to GPUs as nicely.

When you’re acquainted with Jupyter notebooks, studying Colab might be a bit of cake and we will even import Jupyter notebooks to be run on Google Colab. However, there are a number of nifty issues that Colab can do as nicely, which we’re going to discover on this article. Let’s dive proper in!

After finishing tutorial, you’ll learn to:

  • Velocity up coaching utilizing Google Colab’s free tier with GPU
  • Utilizing Google Colab’s extensions to save lots of to Google Drive, current interactive show for pandas DataFrame, and so forth.
  • Save your mannequin’s progress when coaching with Google Colab

Let’s get began!

Google Colab for Machine Studying Tasks

Google Colab for Machine Studying Tasks
Photograph by NASA and processing by Thomas Thomopoulos. Some rights reserved.

Overview

This tutorial is split into 5 components:

  • What’s Google Colab?
  • Google Colab fast begin information
  • Exploring your Colab surroundings
  • Helpful Google Colab extensions
  • Instance: Saving mannequin progress on Google Drive

What’s Google Colab?

From the “Welcome to Colab” pocket book,

Colab notebooks assist you to mix executable code and wealthy textual content in a single doc, together with pictures, HTML, LaTeX and extra. While you create your personal Colab notebooks, they’re saved in your Google Drive account. You’ll be able to simply share your Colab notebooks with co-workers or buddies, permitting them to remark in your notebooks and even edit them.

We will use Google Colabs like Jupyter notebooks however they’re actually handy as a result of they’re hosted by Google Colab so we don’t use any of our personal compute sources to run the pocket book. We will additionally share these notebooks so different folks can simply run our code as nicely, all with an ordinary surroundings because it’s not depending on our personal native machines, although we would want to put in some libraries to the environment throughout initialization.

Google Colab Fast Begin Information

To get create your Google Colab file and get began with Google Colab, you possibly can go to Google Drive and create a Google Drive account if you happen to don’t have one. Then, click on on the “New” button on the highest left nook of your Google Drive web page, then click on on Extra â–· Google Colaboratory.

Creating a brand new Google Colab pocket book

You’ll then enter the web page on your new Google Colab file.

New Google Colab pocket book

From right here, you possibly can share your Google Colab file with others utilizing the Share button on the highest proper hand nook or begin coding!

The new keys on Colab and that on Jupyter notebooks are related. These are a few of the helpful one:

  • Run cell: Ctrl + Enter
  • Run cell and add new cell under: Alt + Enter
  • Run cell and goto cell under: Shift + Enter
  • Indent line by two areas: Ctrl + ]
  • Unindent line by two areas: Ctrl + [

But there’s also one extra that’s pretty useful that lets you only run a particular selected part of the code in a cell:

  • Run selected part of a cell: Ctrl + Shift + Enter

Just like Jupyter notebook, you can also write text with Markdown cells, but Colab has an additional feature that automatically generates a table of contents based on your markdown content and you can also hide parts of the code based on their heading in the markdown cells.

Google Colab with Markdown and Table of Contents

If you run Jupyter on your own computer, you have no choice but use the CPU from your computer. But in Colab, you can change the runtime to include GPUs and TPUs in addition to CPUs, because it is executed on Google’s cloud. You can switch to a different runtime by going to Runtime ▷ Change runtime type:

Changing the runtime type for Gooel Colab

You can then select from the different hardware accelerators to equip your environment with.

Adding GPU/TPU to Google Colab notebook environment

Unlike your own computer, Google Colab does not provide you a terminal to enter commands to manage your Python environment. To install Python libraries and other programs, we can use the ! character to run shell commands just like in Jupyter notebooks, e.g. !pip install numpy (but as we’ll see later on, Colab already comes pre-installed with a lot of the libraries we’ll need such as NumPy)

Now that we know how to set up our Colab environment and start running some code, let’s do some exploration of the environment!

Exploring your Colab environment

As we can run some shell command with !, using the wget command is probably the easiest way to get some data. For example, running this will bring you a CSV file to the Colab environment:

To explore the current working directory of your Colab file on the virtual machine, click on the File icon on the left hand side of the screen. By default, Colab provides you a directory named sample_data with a few files:

Files tab of Google Colab notebook

This is the current working directory for our Colab notebook. You can read one of these file in Python by using a code like this on the notebook:

Later we’ll see how to use Colab extensions to mount our Google Drive to this directory in order to store and access files on our Google Drive account.

By running shell commands using !, we can also look at the hardware configuration of our Colab environment. To take a look at the CPU, we can use

which gave the output for my environment as

We can also check if we have a GPU attached to the runtime by using

which gave the output if you got one:

These are just some examples of the shell commands that we can use to explore the Colab environment. There are also many others such as !pip list to look at the libraries which the colab environment has access to, the standard !ls to explore the files in the working directory, etc.

Useful Colab extensions

Colab also comes with a lot of really useful extensions. One such extension allows us to mount our Google Drive to our working directory. We can do this using

Then, Colab will request for permission to access your Google Drive files, which you can do after selecting which Google account you want to give it access to. After giving it the required permissions, we can see our Google Drive mounted in the Files tab on the left hand side.

Google Drive mounted onto current working directory of Google Colab notebook

Then, to write a file to our Google Drive, we can do

This code snippet writes Hello World! to a test.txt file in the top level of your Google Drive. Similarly, we can read from a file in our Google Drive as well by using:

which outputs

based on our earlier example.

Furthermore, Google Colab comes with some extensions to create better experience on using a notebook. If we use pandas DataFrame a lot, there is an extension to display interactive tables. To use this, we can use magic functions:

which enables the interactive display for DataFrames, then when we run

This can present you the DataFrame as an interactive desk, the place we will filter based mostly on columns, see the completely different rows within the desk, and so forth.

Interactive interface for pandas DataFrame in Google Colab

To disable this function in a while, we will run

and once we show the identical DataFrame X once more, we get the usual Pandas DataFrame interface:

Commonplace interface for pandas DataFrame

Instance: Saving Mannequin Progress on Google Drive

Google Colab might be the simplest method to give us highly effective GPU sources on your machine studying challenge. However within the free model of Colab, Google limits our time we will use our Colab pocket book in every session. Our kernel could terminate for no cause. We will restart our pocket book and proceed our work, however we could misplaced all the things within the reminiscence. This can be a downside if we have to prepare our mannequin for a very long time. Our Colab occasion could terminate earlier than the coaching accomplished.

Utilizing the Google Colab extension to mount our Google Drive and Keras ModelCheckpoint callback, we’re capable of save our mannequin progress on Google Drive. That is notably helpful to workaround Colab timeouts. It’s extra lenient for paid Professional and Professional+ customers, however there may be all the time an opportunity that our mannequin coaching terminates halfway at random occasions. It’s priceless if we don’t misplaced our partially educated mannequin.

For this demonstration, we’ll use the LeNet-5 mannequin on the MNIST dataset.

Then, to save lots of mannequin progress throughout coaching on Google Drive, first we have to mount our Google Drive onto our Colab surroundings.

Afterwards, we declare the Callback to save lots of our checkpoint mannequin to the Google Drive.

Subsequent, coaching on the MNIST dataset, with the checkpoint callbacks to make sure we will resume on the final epoch ought to our Colab session timed out:

This trains our mannequin and offers the output:

and from the output, we will see that the checkpoints have been saved. Taking a look at my Google Drive folder, we will additionally see the checkpoints saved there.

Checkpoints saved in Google Drive

Colab occasion is on Google’s cloud surroundings. The machine it’s run has some storage so we will set up a package deal or obtain some file into it. Nevertheless, we must always not save our checkpoint there as a result of now we have no assure to get it again after our session terminated. Due to this fact, within the above, we mount our Google Drive into the occasion and save the checkpoint in our Google Drive. That is how we may be assured the checkpoint information are accessible.

Right here we connect the complete code for the mannequin coaching and saving to Google Drive:

If mannequin coaching stops halfway, we will proceed by simply recompiling the mannequin and loading the weights, after which we will proceed our coaching:

Additional studying

This part offers extra sources on the subject in case you are seeking to go deeper.

Articles

Abstract

On this tutorial, you might have learnt what’s Google Colab, learn how to leverage Google Colab to get free entry to GPUs utilizing its free tier, learn how to use Google Colab together with your Google Drive account, and learn how to save fashions to retailer mannequin progress throughout coaching on Google Drive in a Google Colab pocket book.

Particularly, you learnt:

  • What’s Google Colab and learn how to begin utilizing it
  • discover your Google Colab pocket book’s surroundings utilizing bash instructions with !
  • Helpful extensions that include Google Colab
  • Saving mannequin progress throughout coaching to Google Drive



[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments