CoLab Learning Note

CoLab Learning Note

Here are some recommended tutorials, you can easily get started with them.

In English:

In Chinese:

Common Usages

Connect to google drive

1
2
from google.colab import drive
drive.mount('/content/drive')

Change direction

Sometimes will have problems with cd, use following commands to instead.

1
2
3
import os
path = "/content/drive/MyDrive/target_folder_name"
os.chdir(path)

Check GPU state

Use following script to print info of GPU.

1
2
3
4
5
6
7
gpu_info = !nvidia-smi
gpu_info = '\n'.join(gpu_info)

if gpu_info.find('failed') >= 0:
print('Not connected to a GPU')
else:
print(gpu_info)

Use following script to check whether GPU is available for your model.

And show the device type of GPU.

1
2
3
import torch
print(torch.cuda.is_available()) #True if Torch is using GPU, otherwise False.
print(torch.cuda.get_device_name(0)) #Show the device name.

CoLab Learning Note
https://yifan-scyyy6.github.io/2023/12/20/colab_note/
作者
Yifan Yu
发布于
2023年12月20日
许可协议