[转][PyTorch指定GPU

来源:互联网 发布:nba2k17 mac版本 编辑:程序博客网 时间:2024/05/23 02:28

原文出处:PyTorch指定GPU

PyTorch默认使用从0开始的GPU,如果GPU0正在运行程序,需要指定其他GPU。

有如下两种方法来指定需要使用的GPU。

1 . 类似tensorflow指定GPU的方式,使用CUDA_VISIBLE_DEVICES。

1.1 直接终端中设定:

CUDA_VISIBLE_DEVICES=1 python my_script.py

1.2 python代码中设定:

import osos.environ["CUDA_VISIBLE_DEVICES"] = "2"

见网址:http://www.cnblogs.com/darkknightzh/p/6591923.html

2 . 使用函数 set_device

import torchtorch.cuda.set_device(id)

该函数见 pytorch-master\torch\cuda__init__.py。

不过官方建议使用CUDA_VISIBLE_DEVICES,不建议使用 set_device 函数。