colab使用GPU
https://www.bilibili.com/video/BV1K3411876U
使用里面的代码
! pip install torch
import torch
torch.cuda.is_available()
# 查看GPU数量
torch.cuda.device_count()
torch.cuda.get_device_name(0)
#查看指定GPU容量
torch.cuda.get_device_capability()
#产看GPU的内存使用情况
!nvidia-smi
下载DGL
有2种方式下载。一种是CPU版本的,一种是GPU版本的。
但是据我使用,反而是GPU版本的无法使用,会报错Device API gpu is not enabled. Please install the cuda version of dgl.可能是colab的问题。
CPU版本
pip install dgl
然后使用官方的代码
import dgl
import torch as th
u, v = th.tensor([0, 1, 2]), th.tensor([2, 3, 4])
g = dgl.graph((u, v))
g.ndata['x'] = th.randn(5, 3) # 原始特征在CPU上
g.device
#device(type='cpu')
cuda_g = g.to('cuda:0') # 这里会报错!!
cuda_g.device
#报错 Device API gpu is not enabled. Please install the cuda version of dgl.
其实,用另一种构造方法就可以了
# 由GPU张量构造的图也在GPU上
u, v = u.to('cuda:0'), v.to('cuda:0')
g = dgl.graph((u, v))
g.device
device(type='cuda', index=0)
GPU版本
pip install dgl-cu111 -f https://data.dgl.ai/wheels/repo.html
这种方式我无法import DGL 。显示没有DGL这个module
colab里我的cuda是11.2。DGL官方文档里没有相应的11.2,所以我安装了11.1