更改TensorFlow中的默认GPU

MBZ*_*MBZ 17 python tensorflow

根据文档,默认GPU是id最低的GPU:

如果系统中有多个GPU,则默认情况下将选择ID最低的GPU.

是否可以从命令行或一行代码更改此默认值?

mrr*_*rry 26

Suever的答案正确地说明了如何将您的操作固定到特定的GPU.但是,如果在同一台计算机上运行多个TensorFlow程序,建议您CUDA_VISIBLE_DEVICES在启动进程之前将环境变量设置为公开不同的GPU.否则,TensorFlow将尝试在所有可用GPU上分配几乎整个内存,这会阻止其他进程使用这些GPU(即使当前进程未使用它们).

请注意,如果您使用CUDA_VISIBLE_DEVICES的设备名称"/gpu:0","/gpu:1"等是指0级和1 可见设备的当前进程.

  • 我只是`导出CUDA_VISIBLE_DEVICES ="0"`它似乎工作以防万一我只想使用GPU = 0.这是正确的吗? (3认同)

Fra*_*urt 23

只是要清楚使用环境变量CUDA_VISIBLE_DEVICES:

my_script.py仅在GPU 1上运行脚本,可以在Linux终端中使用以下命令:

username@server:/scratch/coding/src$ CUDA_VISIBLE_DEVICES=1 python my_script.py 
Run Code Online (Sandbox Code Playgroud)

更多说明语法的示例:

Environment Variable Syntax      Results
CUDA_VISIBLE_DEVICES=1           Only device 1 will be seen
CUDA_VISIBLE_DEVICES=0,1         Devices 0 and 1 will be visible
CUDA_VISIBLE_DEVICES="0,1"       Same as above, quotation marks are optional
CUDA_VISIBLE_DEVICES=0,2,3       Devices 0, 2, 3 will be visible; device 1 is masked
CUDA_VISIBLE_DEVICES=""          No GPU will be visible
Run Code Online (Sandbox Code Playgroud)

供参考: