我们知道在terminal 中可以通过 CUDA_VISIBLE_DEVICES=x python3 my_script.py 来选择使用的GPU。那么在python 脚本中怎样选择呢。
可以通过如下简单操作
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "x"
import torch # this should be after device selection
网上的另一种办法 torch.cuda.set_device(x) 经试验似乎并不起作用