Colab Tutorial

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Colab Tutorial

TA: 陳光銘
2023.02.20
[email protected]
Link of this Tutorial
https://reurl.cc/eWj6jL

(Things mentioned in this tutorial will be covered in this notebook)


Outline
● Introduction
● Getting Started
● Changing Runtime
● Executing Code Block
● Check GPU type
● File Manipulation
● Mounting Google Drive
● Saving Notebook
● Useful Linux Commands
● Problems You May Encounter… (very important)
● References
Introduction
What is Colab?
Colab, or "Colaboratory", allows you to write and execute Python in your
browser, with

● Zero configuration required


● Free access to GPUs
● Easy sharing
Getting Started
Creating a new cell
You can create a new code cell by clicking on +Code, clicking on +Text
generates a text cell

There are options for moving your cell up/down or copy or delete it

Code Cell

Text
Cell
Getting Started
You can type python code in the code cell, or use a leading exclamation mark ! to
change the code cell to treating the input as a shell script

→ python

→ shell script
Getting Started
Using an exclamation mark (!) starts a new shell, does the operations, and then
kills that shell, while percentage (%) affects the process associated with the
notebook, and it is called a magic command.

Use % instead of ! for cd (change directory) command

other magic commands are listed here


Changing Runtime
To utilize the free GPU provided by google,
click on "Runtime"(執行階段) → "Change Runtime
Type"(變更執行階段類型).

select "GPU" for "Hardware Accelerator"(硬體加速器)

Doing this will restart the session, so make sure you


change to the desired runtime before executing any
code.
Executing Code Block
Click on the play button to execute a specific code cell
Executing Code Block
Other options to run your code
Check GPU Type
Use the command nvidia-smi to
check the allocated GPU type

Available GPUs:

T4 > K80

(but most of the time you get K80 using


the free Colab)
File Manipulation
Download files via Google Drive
File Manipulation
File Structure
● You may click on the folder icon on the left refresh

to view your current files


● After downloading files, if the files are not
immediately shown, click the refresh button
● Files are temporarily stored, and will be
removed once you end your session.
File Manipulation
Upload and Download Files

Click the upload icon to upload


click to download files to your local
local files to your session
Mounting Google Drive
If you don’t want to download the data every time you start a new session, or you
want some files to be saved permantly,

you can mount your own google drive to colab and directly download/save the
data to your google drive.

Mount Drive:
Mounting Google Drive
Click on the Google Drive icon, the Mount Drive code block will be generated
Mounting Google Drive
Execute the following three code blocks in order
This will download the image to your google drive, and
you can access it later
Saving Notebook
● Download the .ipynb file to your local device
(File > Download .ipynb)
● Save the colab notebook to your google drive
(File > Save a copy in Drive).
● Convert .ipynb to .py and download
(File > Download .py)
Useful Linux Commands (in Colab)
ls : List all files in the current directory
ls -l : List all files in the current directory with more detail
pwd : Output the working directory
mkdir <dirname> : Create a directory <dirname>
cd <dirname> : Move to directory <dirname>
gdown : Download files from google drive
wget : Download files from the internet
python <python_file>: Executes a python file
Problems You May Encounter…
● Colab will automatically disconnect if idle timeout(90 min., sometimes varying) or
when your screen goes black
→ solution: keep your screen on or try using javascript

● GPU usage is not unlimited ! (your account will be stopped for a period if you reached
the max gpu usage 12 hrs)
* The cooldown period before you can connect to another GPU will extend from hours to
days to weeks depending on your usage

→ solution: open another account

Best solution:
1. buy colab pro :)
2. use your own resource (if able)

Reminder: TAs are not required to help you solve environment problems
Reference
● https://colab.research.google.com
● https://research.google.com/colaboratory/faq.html

You might also like