PyTorch

Anh-Thi Dinh
draft
⚠️
This is a quick & dirty draft, for me only!
This note is for things with PyTorch. Personal notes only.

Installation

  • Verify that your computer has a graphic card (NVIDIA): lspci -nn | grep '[03'
  • Check the CUDA version on Ubuntu: nvidia-smi. If have problems, check this note.
  • Install the lates version: here.

Errors when installing

Problem with CUDA version

For example, need to install corresponding versions: torch==1.2.0 <- torchvision==0.4.0 <- Pillow<7.0.0
1pip3 install -U torch==1.2.0
2pip3 install -U torchvision==0.4.0
3pip3 install -U "pillow<7"
Another option (worked on XPS 15 7950): torch==1.5.1, torchvision==0.6.1, pillow==7.2.0 (with nvcc --version is 11.1).

NVIDIA too old

Problem The NVIDIA driver on your system is too old (found version 10010).
From this website, problem is solved by
  1. Switching from nvidia-driver-435 to nvidia-driver-440.
  1. Restart the computer.
It works on Dell XPS 7950 whose GPU is NVIDIA GTX 1650.

Other errors

🔅 RuntimeError: cuda runtime error (804) : forward compatibility was attempted on non supported HW at /pytorch/aten/src/THC/THCGeneral.cpp:47 (after update system including nvdia-cli, maybe): check this note.

Import

1import torch
2import torch.nn as nn
3import torch.nn.functional as F
4import torch.optim as optim

Device's info

1print('cuda is available? ', torch.cuda.is_available())
2print('device_count: ', torch.cuda.device_count())
3print('current device: ', torch.cuda.current_device())
4print('device name: ', torch.cuda.get_device_name(0))
1# Determine supported device
2def get_device():
3    if torch.cuda.is_available():
4        device = torch.device('cuda:0') # or something else
5    else:
6        device = torch.device('cpu') # don't have GPU
7    return device

Convert DataFrame / Series to Tensor

1def df_to_tensor(df):
2    device = get_device() # see other section
3    return torch.from_numpy(df.values).float().to(device)