Free GPU for Deep Learning
A simple tutorial of using free GPU on Google Colab for deep learning
Google’s Colab provides GPU and it’s totally free. Seriously?
What is Google Colab?
Google Colab is a free online cloud-based Jupyter notebook environment that allows us to train our machine learning and deep learning models.
If you have ever used Jupyter notebook, Colab won’t look strange to you. In fact, you’re going to love Google Colab – you get free access to CPUs and GPUs such as Tesla K80 and even a TPU!
You can create notebooks in Colab, upload, import and download notebooks, store notebooks, easily share notebooks with others (just like sharing Google Docs), mount your Google Drive to use data stored in there.
Getting Google Colab Ready
All you need is a Google account and a web browser.
Create a folder on your Google Drive. You can name it whatever you like, for example “colab””.
Go to the folder you just created, create a Colab as follows:
Open the notebook you just created. You can write and run any Python codes.
Setting Runtime Type (CPU, GPU or TPU)
It is simple to choose the runtime type (CPU, GPU or TPU). You can just go to Runtime -> Change runtime type:
Then select the one you want:
- None (CPU)
- GPU
- TPU
The specifications of these runtimes are as follows:
We select GPU runtime In this tutorial.
Running codes on GPU
To verify if the free GPU is up, run the following codes in your Colab:
import tensorflow as tf
print("Version: ", tf.__version__)
print("GPU is", "available" if tf.test.is_gpu_available() else "NOT AVAILABLE")
If it says “GPU is available”, the free GPU is up and running.
Now feel free to play around with it by running some of the following machine learning examples:
Reading files on Google Drive
When the input data is from a file, you can read it by following these steps:
First upload the file to your Google Drive.
Mount your Google Drive by running the following code on your Colab:
from google.colab import drive drive.mount('drive/')
Follow the instructions to mount your Drive.
Run the following code to verify if you have successfully mounted your Drive:
!ls "drive/My Drive"
or the folder you created earlier:
!ls "drive/My Drive/colab"
You should be able to see the files on your Drive.
Let’s say you want to read a file in “My Drive/colab/data.txt”, run the following code:
input_file = "drive/My Drive/colab/data.txt' data = open(input_file, 'r').read()
In the end…
The coolest thing Google Colab offers is probably the free GPU (free CPU and TPU as well). It is a great place for everyone to build machine learning and deep learning models.
Can’t wait to train models in GPU? You could follow the projects in these books to get started:
- Hands-On Deep Learning Architectures with Python
- R Deep Learning Projects
- Python Machine Learning by Example 2nd Edition
Let me know if you enjoy those examples, or you want to chat about machine learning.
Happy learning and coding!