0% found this document useful (0 votes)
20 views

Colab Tutorial 2022

Uploaded by

Da HUANG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Colab Tutorial 2022

Uploaded by

Da HUANG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Colab Tutorial

TA: 孟妍 (Yen Meng)


2022.02.18
[email protected]
Link of this Tutorial
https://reurl.cc/Epg3M0

(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:

P100 > 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
refresh
● You may click on the folder icon on the left
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