Use Google Colab for Deep Learning and Machine Learning Models

Share on facebook
Share on twitter
Share on linkedin
Share on telegram
Share on whatsapp

Contents

Google Colab: now create great deep learning models on your machine.

“Memory error”: that familiar and dreaded message in Jupyter notebooks when trying to run a machine learning or deep learning algorithm on a large data set. Most of us don't have access to unlimited computing power on our machines.. And let's face it, it costs an arm and a leg to get a decent GPU from existing cloud providers.

Then, How do we build great deep learning models without putting a hole in our pockets??

Step up: ¡Google Colab! Is incredible browser-based online platform that allows us to train our models on machines for free. It sounds too good to be true, but thanks to google, now we can work with large data sets, create complex models and even share our work seamlessly with others. That's the power of Google Colab.

featured_image-2-8594261

Honestly, working with Colab has opened up so many avenues for me that I previously thought were not possible. We no longer have the restriction of little computational power in our machines. Free GPUs are at your fingertips, then, What are you waiting for?

If you are new to the world of deep learning, I have some great resources to help you get started in a comprehensive and structured way:

Table of Contents

What is Google Colab?

Google Collaborative it is a free online cloud-based Jupyter notebook environment that allows us to train our machine learning and deep learning models on CPUs, GPU y TPU.

This is what I really love about Colab. It doesn't matter what computer you have, what your setup is and how old it may be. You can still use Google Colab! All you need is a Google account and a web browser. And here's the cherry on top: you get access to GPU like Tesla K80 and even a TPU, for free!

TPUs are much more expensive than a GPU and you can use them for free at Colab. It bears repeating over and over again: it is an offer like no other.

Are you still using the same Jupyter laptop in your system to train models? Créame, you will love Google Colab.

GPU and TPU in Google Colab

Ask anyone who uses Colab why they love it. The answer is unanimous: the availability of free GPUs and TPUs. Training models, especially deep learning ones, require several hours on a CPU. We have all faced this problem on our local machines. GPUs and TPUs, Secondly, they can train these models in a matter of minutes or seconds.

If you still need a reason to work with GPUs, check out this excellent explanation by Faizan Shaikh.

If is one data science hackathon or a deep learning project, I always prefer a GPU to any other CPU because of the great computational power and speed of execution. But not everyone can afford a GPU because it is expensive. That's where Google Colab comes in..

Gives you a decent GPU for free, that you can run continuously for 12 hours. For most data science folks, this is enough to meet your computing needs. Especially if you are a beginner, I recommend that you start using Google Colab.

Google Colab offers us three types of runtime for our laptops:

As I mention, Colab offers us 12 hours of continuous runtime. Thereafter, the entire virtual machine is erased and we have to start over. We can run multiple instances of CPU, GPU and TPU simultaneously, but our resources are shared between these instances.

Let's take a look at the specifications of the different runtimes offered by Google Colab:

uc2-e1583900164853-300x74-4698612

It will cost you A LOT to buy a GPU or TPU from the market. Why not save that money and use Google Colab from the comfort of your own machine?

Introduction to Google Colab

You can go to Google Colab using this link. This is the screen you will get when you open Colab:

uc1-5934919

Click on the NEW NOTEBOOK to create a new Colab notebook. You can also upload your local notebook to Colab by clicking the upload button:

uc17-3993356

You can also import your laptop from Google Drive or GitHub, but require an authentication process.

uc3-300x132-1205564

You can change the name of your notebook by clicking on the notebook name and change it to whatever you want. I usually name them according to the project I'm working on.

Google Colab runtimes: choice of GPU or TPU option

The ability to choose different types of runtimes is what makes Colab so popular and powerful. Here are the steps to change the runtime of your laptop:

Paso 1: Click on 'Runtime’ in the top menu and select 'Change runtime type':

uc14-300x262-5668042

Paso 2: Here you can change the runtime according to your needs:

uc4-1277397

A wise man once said: “With great power comes great responsibility”. I implore you to turn off your laptop after you have completed your work so that others can use these resources because they are shared by multiple users. You can finish your laptop like this:

uc15-e1583920445689-2428502

Using terminal commands in Google Colab

You can use the Colab cell to run terminal commands. Most of the popular libraries are installed by default in Google Colab. Yes, Piton libraries like Pandas, NumPy, scikit-learn they are all pre-installed.

If you want to run a different python library, you can always install it inside your Colab notebook this way:

!pip install library_name

easy enough, truth? Everything is similar to how it works in a normal terminal. You just have to put a exclamation(!) before typing each command as:

!ls

O:

!pwd

Cloning repositories on Google Colab

You can also clone a Git repository within Google Colaboratory. Just go to your GitHub repository and copy the repository clone link:

uc5-300x139-1724668

Later, just run:

!git clone https://github.com/analyticsvidhya/Complete-Guide-to-Parameter-Tuning-in-XGBoost-with-codes-in-Python.git

uc6-7278709

And there you go!

Upload files and data sets

This is an aspect that any data scientist should know. The ability to import your dataset into Colab is the first step on your data analysis journey.

The most basic approach is to upload your dataset to Colab directly:

uc7-300x239-6521716

You can use this approach if your dataset or file is very small because the loading speed in this method is quite low. Another approach I recommend is uploading your dataset to Google Drive and mounting your disk in Colab. You can do this with just one click of your mouse:

uc8-300x239-7869186

You can also upload your dataset to any other platform and access it via its link. I tend to go for the second approach most of the time (when possible).

Save your notebook

All Colab notebooks are stored on your Google Drive. The best thing about Colab is that your notebook is automatically saved after a certain period of time and you do not lose your progress.

If you wish, you can export and save your notebook in formats * .py y * .ipynb:

uc13-2339002

Not only that, you can also save a copy of your notebook directly to GitHub, or you can create a GitHub Gist:

uc10-185x300-2245243

I love the variety of options we have.

Data export / Google Colab files

You can export your files directly to Google Drive, or you can export them to the VM instance and download them yourself:

uc9-6816546

Exporting directly to Drive is a better option when you have larger files or more than one file. You'll learn these nuances as you work on larger projects in Colab.

Share your notebook

Google Colab also offers us an easy way to share our work with others. This is one of the best things about Colab:

uc11-e1583901050742-4951056

Just click on the Share , and it gives us the option to create a link to share that we can share through any platform. You can also invite other people using their email IDs. It is exactly the same as sharing a Google Doc or Google Sheet.. The complexities and simplicity of the Google ecosystem are staggering!!

uc12-300x132-6718933

Whats Next?

Google Colab now also offers a payment platform called Google Colab Pro, At a price of $ 9,99 one month. In this plan, you can get the Tesla T4 O Tesla P100 GPU and the option to select an instance with a high RAM of around 27 GB. What's more, its maximum calculation time doubles from 12 hours to 24 hours. How cool is that?

You can consider this plan if you need high computing power because it is still quite cheap compared to other cloud GPU providers like AWS, Azure and even GCP.

I am also working on another article where I will give you all the tips and tricks you need to know to master Google Colab. If you found this article informative, Share it with your friends and comment below with your comments or queries.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.