Pin-Jiun / Machine-Learing-NTU

0 stars 0 forks source link

3.1-Google Colab and GPU #6

Open Pin-Jiun opened 2 years ago

Pin-Jiun commented 2 years ago

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

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 image

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

GPU

click on "Runtime"(執行階段) → "Change Runtime Type"(變更執行階段類型)→ select "GPU" for "Hardware Accelerator"(硬體加速器)

Check GPU Type

Use the command nvidia-smi to check the allocated GPU type Available GPUs: P100 > T4 > K80

File Manipulation

Download files via Google Drive image

!gdown --id 'FIELED_ID' --output filename

每個notebook有屬於自己的專屬資料夾,可以Mounting Google Drive

如果要打包下載 使用以下指令

!pwd
!ls
!zip -r ./training_log.zip ./training_log/
Pin-Jiun commented 1 year ago

.ipynb 是 ipython NoteBook的簡寫, 是Juputer所使用的一種檔案形式


GPU

GPU

確認電腦是否有GPU(Graphics Processing Unit), 並確認是否有CUDA

CUDA(Compute Unified Device Architecture,統一計算架構)是由輝達NVIDIA所推出的一種整合技術,是該公司對於GPGPU的正式名稱。 透過這個技術,使用者可利用NVIDIA的GPU進行圖像處理之外的運算,亦是首次可以利用GPU作為C-編譯器的開發環境。

cuDNN(CUDA Deep Neural Network library):是NVIDIA打造的針對深度神經網絡的加速庫,是一個用於深層神經網絡的GPU加速庫。 如果你要用GPU訓練模型,cuDNN不是必須的,但是一般會采用這個加速庫。

Win10 安裝 CUDA、cuDNN 教學

測試 CUDA 是否安裝成功,若安裝成功會輸出 CUDA 版本資訊

nvcc -V

如果上述找不到, 記得將以下的位置加入環境變數中的Path中

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin
import torch
print(torch.cuda.is_available())

要移動到GPU的包括

  1. 創建的模型
  2. 數據, 包括data和label
  3. loss function

When would I use model.to("cuda:1") as opposed to model.to("cuda:0")?

import torch

print(torch.cuda.get_device_name(torch.device('cuda:0')))

NVIDIA GPU in your case, the AMD GPU isn't visible as a cuda device, feel safe to assume cuda:0 is only a CUDA enabled GPU, and 沒有CUDA enabled GPU won't be seen by your program

nvidia-smi