Analytics Vidhya
Published in

Analytics Vidhya

ML06: PyTorch on Google Colab

Free access to NVIDIA Tesla T4

Read time: 5 minThe time of this writing is 2020/11/01. The GPU Google Colab provided may change over time [1]. Complete setup codes on Colab (for installing PyTorch & CUDA on PC or laptop):
https://bit.ly/3pS3Trv
Simplified setup codes on Colab (acutally Colab has PyTorch & CUDA already):
https://bit.ly/3axxJes

Outline
(1) GPU in DL & Google Colaboratory
(2) Installing PyTorch and CUDA
(3) Initializing CUDA in PyTorch on Colab
(4) GPU of Colab: NVIDIA Tesla T4
(5) Simplified Setup Codes
(6) Summary
(7) Reference

(1) GPU in DL & Google Colaboratory

When acquiring neural network (NN) or deep learning (DL), we learn from neuron, perception, MLP (multilayer perceptron) to CNN (convolutional neural network). However, GPU plays a chief role to reduce computing time while training intricate NN models. Hence, free GPU source like Google Colaboratory would save helpless beginners.

Figure 1: Official introduction of Colab

Colab’s environment looks pretty like Jupyter Notebook. We can not only run normal Python codes (no NN involved) in Colab, but also run NN models by NVIDIA GPU provided by Google.

Now(2020/11/01) Colab provides NVIDIA Tesla T4 (we will confirm that later), which is a GPU definitely not affordable for a student like me. NVIDIA Tesla T4 costs 2,200 USD at HP store on Amazon, and it costs 93,867 TWD (=3,250 USD) on Dell Taiwan website! (This article is written on 2020/11/01, the GPU Goolge provided and the price of NVIDIA Tesla T4 may change over time.)

Figure 2: NVIDIA Tesla T4 at HP store on Amazon [2]
Figure 3: NVIDIA Tesla T4 on Dell Taiwan website (TWD = NTD) [3]

(2) Installing PyTorch and CUDA on Colab

Above all, let’s open a new notebook in Colab and run the following codes.

1. Operating System Info

!cat /etc/*-release
Figure 4: Check the operating system

To install PyTroch & CUDA, We have to get information of the operating system. It’s Ubuntu 18.04 here.

2. Installing PyTorch

PyTorch: START LOCALLY
https://pytorch.org/get-started/locally/

Check the PyTorch installation web page above and choose the suitable one for your machine. Here we get the following codes and run them.

Figure 5: Installation codes of PyTorch with CUDA=11.0
pip install torch torchvision

We try the command for CUDA 10.2 first, and get “Requirement already satisfied” at this step, so Colab has PyTorch already! Then let’s try this command:

pip uninstall torch

Hold on a minute, please don’t type “y” to proceed. We are here just for inspecting the PyTorch version inherent in Colab.

Figure 6: Inspect the PyTorch version inherent in Colab
pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio===0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
Figure 7: Install PyTorch

Note that we chose PyTorch with CUDA=11.0 since we will install CUDA 11.1 later. Seems that Colab has PyTorch already ! Good.

3. Installing CUDA

CUDA Toolkit 11.1 Update 1 Downloads
https://developer.nvidia.com/cuda-downloads

CUDA is a special API allowing computing be done on GPU. CUDA is established by NVIDIA and only available on NVIDIA’s GPU.

Check the CUDA installation web page above and choose the suitable one for your machine. Here we get the following codes and run them.

Figure 8: Installation codes of CUDA 11.1
!wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
!sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
!wget https://developer.download.nvidia.com/compute/cuda/11.1.1/local_installers/cuda-repo-ubuntu1804-11-1-local_11.1.1-455.32.00-1_amd64.deb
!sudo dpkg -i cuda-repo-ubuntu1804-11-1-local_11.1.1-455.32.00-1_amd64.deb
!sudo apt-key add /var/cuda-repo-ubuntu1804-11-1-local/7fa2af80.pub
!sudo apt-get update
!sudo apt-get -y install cuda
Figure 9: Install CUDA 11.1

It takes a while like 2 minutes.

(3) Initializing CUDA in PyTorch on Colab

Now, let’s check if CUDA is avaliable now.

import torch
torch.cuda.is_available()
Figure 10: CUDA isn’t available for PyTorch

So … what’s the problem ? Let’s go “Runtime” -> ”Change runtime type” -> “Hardware accelerator” -> Choose “GPU”. Just as the following figure.

Figure 11: Select GPU as hardware accelerator
Figure 12: CUDA is available for PyTorch

CUDA is available, so you can try intricate DL models now.

(4) GPU of Colab: NVIDIA Tesla T4

Let’s see the GPU type.

!nvidia-smi
Figure 13: GPU info

It’s exactly NVIDIA Tesla T4 !

(5) Simplified Setup Codes

Actually, Colab has PyTorch & CUDA already, so the installation procedures above are redundant. However, the installation procedures above are Indispensable for set up PyTorch & CUDA on PC or laptop!

So we can simply do like:

#### (1) Check if PyTorch and CUDA are available
import torch
torch.cuda.is_available()
#### (2) Inspect GPU
!nvidia-smi
#### (3) Test PyTorch
import torch
print(torch.ones(3,2))

(6) Summary

We installed PyTorch and CUDA on Google Colab, then initialized CUDA in PyTorch. Finally, we checked the GPU of Colab is NVIDIA Tesla T4 (2020/11/01), which costs 2,200 USD on Amazon and is too expensive to a student like me.

Check here for complete setup codes (for installing PyTorch & CUDA on PC or laptop):
https://bit.ly/2WfovwS
Check here for simplified setup codes (acutally Colab has PyTorch & CUDA already):
https://bit.ly/3mhyNH9

(7) Reference

[1] Colaboratory tweet (2019/04/24). Retrieved from
https://twitter.com/googlecolab/status/1120777276503683072
[2] NVIDIA Tesla T4 at HP store on Amazon. Retrieved from
https://amzn.to/320aiqK
[3] NVIDIA Tesla T4 on Dell Taiwan website. Retrieved from
https://dell.to/3efH7VA
[4] Rao, D. and McMahan, B. (2019). Natural Language Processing with PyTroch. California, CA: O’Reilly Media.

(Chinese)

[5] 邢夢來等人 (2018)。深度学习框架PyTorch快速开发与实战。北京,中國:電子工業。

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yu-Cheng (Morton) Kuo

Yu-Cheng (Morton) Kuo

ML/DS using Python & R. A Taiwanese earned MBA from NCCU and BS from NTHU with MATH major & ECON minor. Email: morton.kuo.28@gmail.com