#方法主要有两种
1、import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1" #使用1号
2:在相应虚拟环境下手动敲入
CUDA_VISIBLE_DEVICES=0 python your_script.py
但是有时候第一个命令,不会起到作用默认的还是编号为0的GPU。我遇到的问题是在torch.device(“cuda”)指定使用了cuda,但是没有指定编号,默认的是0号。优先级高于1的写法。所以要在torch.device(“cuda”: 1)这样才有用。
参考文献
[1]torch.device
[2]在pytorch中为Module和Tensor指定GPU的例子