Google Colab for Machine Learning and Deep Learning
“Memory Error” – that all too familiar dreaded message in Jupyter notebooks when we try to execute a machine learning or deep learning algorithm on a large dataset. Most of us do not have access to unlimited computational 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. So how do we build large deep learning models without burning a hole in our pockets? Step up – Google Colab!
It’s an incredible online browser-based platform that allows us to train our models on machines for free! Sounds too good to be true, but thanks to Google, we can now work with large datasets, build complex models, and even share our work seamlessly with others. That’s the power of Google Colab.
Table of contents
- What is Google Colab?
- What is a Notebook in Google Colab?
- Google Colab Features
- GPUs and TPUs on Google Colab
- How to Use Google Colab?
- Google Colab Runtimes – Choosing the GPU or TPU Option
- Using Terminal Commands on Google Colab
- Cloning Repositories in Google Colab
- Uploading Files and Datasets
- Saving Your Notebook
- Exporting Data/Files from Google Colab
- Sharing Your Notebook
- What’s Next?
What is Google Colab?
Google Colaboratory is a free online cloud-based Jupyter notebook environment that allows us to train our machine learning and deep learning models on CPUs, GPUs, and TPUs.
Here’s what I truly love about Colab. It does not matter which computer you have, what it’s configuration is, and how ancient it might 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 GPUs like Tesla K80 and even a TPU, for free!
TPUs are much more expensive than a GPU, and you can use it for free on Colab. It’s worth repeating again and again – it’s an offering like no other.
Are you are still using that same old Jupyter notebook on your system for training models? Trust me, you’re going to love Google Colab.
What is a Notebook in Google Colab?
In Google Colab, a notebook is a web-based environment for creating and running code. Notebooks are similar to scripts or code files in other programming environments but offer some unique advantages. Notebooks allow you to write and execute code in a web browser, displaying the output in real time. This makes it easy to iterate on your code and visualize the results as you go. Colab notebooks also support markdown, allowing you to include formatted text, equations, and images alongside your code. You can also add comments and notes to your code, which makes it easier to understand and collaborate with others. Overall, notebooks are a powerful tool for data scientists and machine learning practitioners, providing a flexible and interactive environment for writing and testing code.
Google Colab Features
- Colab provides users free access to GPUs and TPUs, which can significantly speed up the training and inference of machine learning and deep learning models.
- Colab’s interface is web-based, so installing any software on your local machine is unnecessary. The interface is also intuitive and user-friendly, making it easy to get started with coding.
- Colab allows multiple users to work on the same notebook simultaneously, making collaborating with team members easy. Colab also integrates with other Google services, such as Google Drive and GitHub, making it easy to share your work.
- Colab notebooks support markdown, which allows you to include formatted text, equations, and images alongside your code. This makes it easier to document your work and communicate your ideas.
- Colab comes pre-installed with many popular libraries and tools for machine learning and deep learning, such as TensorFlow and PyTorch. This saves time and eliminates the need to manually install and configure these tools.
GPUs and TPUs on 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, takes numerous hours on a CPU. We’ve all faced this issue on our local machines. GPUs and TPUs, on the other hand, 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.
It gives you a decent GPU for free, which you can continuously run for 12 hours. For most data science folks, this is sufficient to meet their computation needs. Especially if you are a beginner, then I would highly recommend you start using Google Colab.
Google Colab gives us three types of runtime for our notebooks:
- GPUs, and
As I mentioned, Colab gives us 12 hours of continuous execution time. After that, the whole virtual machine is cleared and we have to start again. We can run multiple CPU, GPU, and TPU instances simultaneously, but our resources are shared between these instances.
Let’s take a look at the specifications of different runtimes offered by Google Colab:
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?
How to Use Google Colab?
You can go to Google Colab using this link. This is the screen you’ll get when you open Colab:
Click on the NEW NOTEBOOK button to create a new Colab notebook. Upload your local notebook to Colab by clicking the upload button:
You can also import your notebook from Google Drive or GitHub, but they require an authentication process.
You can rename your notebook by clicking on the notebook name and change it to anything you want. I usually name them according to the project I’m working on.
Google Colab Runtimes – Choosing the 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 notebook:
Step 1: Click ‘Runtime’ on the top menu and select ‘Change Runtime Type’:
Step 2: Here you can change the runtime according to your need:
A wise man once said, “With great power comes great responsibility.” I implore you to shut down your notebook after you have completed your work so that others can use these resources because various users share them. You can terminate your notebook like this:
Using Terminal Commands on Google Colab
You can use the Colab cell for running terminal commands. Most of the popular libraries come installed by default on Google Colab. Yes, Python libraries like Pandas, NumPy, scikit-learn are all pre-installed.
If you want to run a different Python library, you can always install it inside your Colab notebook like this:
!pip install library_name
Pretty easy, right? Everything is similar to how it works in a regular terminal. We just you have to put an exclamation(!) before writing each command like:
Cloning Repositories in Google Colab
You can also clone a Git repo inside Google Colaboratory. Just go to your GitHub repository and copy the clone link of the repository:
Then, simply run:
!git clone https://github.com/analyticsvidhya/Complete-Guide-to-Parameter-Tuning-in-XGBoost-with-codes-in-Python.git
And there you go!
Uploading Files and Datasets
Saving Your Notebook
All the notebooks on Colab are stored on your Google Drive. The best thing about Colab is that your notebook is automatically saved after a certain time period and you don’t lose your progress.
If you want, you can export and save your notebook in both *.py and *.ipynb formats:
Not just that, you can also save a copy of your notebook directly on GitHub, or you can create a GitHub Gist:
I love the variety of options we get.
Exporting Data/Files from Google Colab
You can export your files directly to Google Drive, or you can export it to the VM instance and download it by yourself:
Exporting directly to the Drive is a better option when you have bigger files or more than one file. You’ll pick up these nuances as you work on bigger projects in Colab.
Sharing Your Notebook
Google Colab also gives us an easy way of sharing our work with others. This is one of the best things about Colab:
Just click the Share button, and it gives us the option of creating a shareable link that we can share through any platform. You can also invite others using their email IDs. It’s exactly the same as sharing a Google Doc or Google Sheet. The intricacies and simplicity of Google’s ecosystem are astounding!
Google Colab now also provides a paid platform called Google Colab Pro, priced at $9.99 a month. In this plan, you can get the Tesla T4 or Tesla P100 GPU, and an option of selecting an instance with a high RAM of around 27 GB. Also, your maximum computation time is doubled from 12 hours to 24 hours. How cool is that?
You can consider this plan if you need high computation power because it is still quite cheap when compared to other cloud GPU providers like AWS, Azure, and even GCP.
I am also working on another article where I’ll be giving you all the tips and tricks you need to know to master Google Colab. If you found this article informative, then please share it with your friends and comment below with your feedback or queries.
If you’re new to the world of Deep Learning, I have some excellent resources to help you get started in a comprehensive and structured manner:
Leave a Reply Your email address will not be published. Required fields are marked *