Python Tensorflow 在 jupyter 中设置 CUDA_VISIBLE_DEVICES
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37893755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tensorflow set CUDA_VISIBLE_DEVICES within jupyter
提问by Tim
I have two GPUs and would like to run two different networks via ipynb simultaneously, however the first notebook always allocates both GPUs.
我有两个 GPU,想同时通过 ipynb 运行两个不同的网络,但是第一个笔记本总是分配两个 GPU。
Using CUDA_VISIBLE_DEVICES, I can hide devices for python files, however I am unsure of how to do so within a notebook.
使用 CUDA_VISIBLE_DEVICES,我可以隐藏 python 文件的设备,但是我不确定如何在笔记本中这样做。
Is there anyway to hide different GPUs in to notebooks running on the same server?
无论如何,是否可以将不同的 GPU 隐藏到在同一台服务器上运行的笔记本电脑中?
回答by Yaroslav Bulatov
You can set environment variables in the notebook using os.environ
. Do the following before initializing TensorFlow to limit TensorFlow to first GPU.
您可以使用os.environ
. 在初始化 TensorFlow 之前执行以下操作以将 TensorFlow 限制为第一个 GPU。
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"]="0"
You can double check that you have the correct devices visible to TF
您可以仔细检查您是否拥有对 TF 可见的正确设备
from tensorflow.python.client import device_lib
print device_lib.list_local_devices()
I tend to use it from utility module like notebook_util
我倾向于从像notebook_util这样的实用程序模块中使用它
import notebook_util
notebook_util.pick_gpu_lowest_memory()
import tensorflow as tf
回答by Salvador Dali
You can do it faster without any imports just by using magics:
只需使用魔法,您就可以在没有任何导入的情况下更快地完成:
%env CUDA_DEVICE_ORDER=PCI_BUS_ID
%env CUDA_VISIBLE_DEVICES=0
Notice that all env variable are strings, so no need to use "
. You can verify that env-variable is set up by running: %env <name_of_var>
. Or check all of them with %env
.
请注意,所有 env 变量都是字符串,因此无需使用"
. 您可以验证ENV变量是通过运行设置:%env <name_of_var>
。或使用%env
.