How to use free GPU, TPU provided by Google to train your Machine Learning models.

Hello! Welcome to calloftechies. Are you at the point where CPU inside your computer is not enough to train the machine models you always want to train?. Don't worry, Google is there for you, they give a Nvidia Tesla K80, and TPU(Tensor Processing Unit) for free to use. I am here to tell you, how to use them. Please ignore ads, sorry.

There are two places where you can use GPUs for free:
(A). Google Colaboratory(or Colab).
(B). Kaggle(A platform owned by Google).

Let's talk about Google Colab, I won't be talking about Kaggle, as Google colab is enough, lol. There will be separate article about what is Kaggle, go to footer, you can subscribe us.


What is Google Colaboratory(or Google Colab):

It is a free environment provided by Google to train machine learning models. You can also run other Python code there, but they do not allow illegal activities like mining Bitcoins using their GPU:). Let me introduce the features Google Colab provides, and how to use them. Keep patience :).

The most useful thing after GPU in colab is you can use Google Drive to store, and read data. First of all you have to open Colab notebook to use any feature.

1. Colab Notebook:

Same as Jupyter notebook. If you don't know what is jupyter notebook, it is a web application where you can run Python, or R code. Also, you can write documents, visualize data without any need to open new window.

How to install, and open Google Colaboratory notebook:

Step 1: Visit Google Drive, and go to the folder wherever you want to save your notebook(ipynb file).

Step 2: If Colab isn't installed, install it.
Right click, and then expand the option named more. After that, click on connect more apps option. A pop will open in front of you, search for Google Colaboratory, and then install by clicking on it.

Now Google colab is available to you.

Step 3:  After installing Google Colaboratory, whenever you want to use it, just right click, choose more, and then you will have the option named Google Colaboratory. Choose the option, and you will have a notebook opened in front of you in a new tab.

[Sorry for image quality, it wasn't possible to take screen shot with right click menus]

Press connect, and try running python codes in the cells. It is not limited only to Python, and R codes, you can also run many linux commands also using Google Drive as disk, we will talk about it later.

2. GPU, and TPU:

Google Colab gives free Nvidia Tesla K80 GPU which is enough to those who are learning Machine Learning, and not want to build revolutionary products using a free GPU:). TPUs are specially made for processing Tensors, Tensors are just multi-dimensional(like 3d, 4d etc.) matrices. If you know, Tensorflow is a machine learning library developed by Google, it uses Tensors to store data.

How to use free GPU, or TPU provided by Google Colaboratory:

Step 1: Click on Runtime button at the top, near to the insert button.

Step 2: Choose change runtime type option, you will have a pop like this:

Step 3: Wow, you can see there is a dropdown with GPU, and TPU as options, you can choose whatever runtime you want.

That's it, now your machine learning libraries can use these devices. 

GPUs, and TPUs are available almost everytime, but sometimes if the users limit is crossed, they may not be available. Don't worry, you are not gonna experience that, you can try reconnecting if it happens.

3. Using Google Drive with Google Colaboratory :

Step 1: Paste, and run  these 2 lines of code in the first cell of the notebook.

from google.colab import drive

Step 2: Click on the link given in the output, it will open a new tab. Choose your respective Google account, and copy the code given to you after that.

Step 3: Paste that code in the textfiled showed in output, and then press Enter.
Now you are connected to Google Drive, and you can use it as a disk. You can run Linux/shell commands like wget, cd, ls, unzip, echo, mkdir, rm, and even Python with filename as argument. The biggest advantage of this is that it will allow you to directly download files/data your Google Drive. You can read, write, and do anything with the data using Python, or R. 

To get details about how to run shell commands, visit here.

Another important point, the current directory will not be your Google Drive home, where you see you folders, and files. You will have to cd(change directory) to see the ls(list) of your file, and folders inside Drive. Run the command given below to see them:

%cd '/content/drive/My Drive/colab/'

Important Note:
  • When you close your notebook, the runtime gets destroyed. That means there will be no data on the memory(RAM). You will have to run all cells again, if you want to add new code, and work ahead.
  • Also, reconnect Google Drive evertime you reopen your notebooks.

Post a Comment