在 TensorFlow 中以不同的设备模式运行多个图 [英] Running multiple graphs in different device modes in TensorFlow
问题描述
我正在同时处理多个图形.例如,我希望一个图形使用 CPU,另一个图形使用 GPU.我怎样才能做到这一点?
I am operating on multiple graphs simultanously. I would for example, like one graph to use CPU and the other to use the GPU. How can I achieve this ?
目前的方法及其问题
当我使用带有 tf.ConfigProto
的 tf.Session()
时,它不起作用,仍然使用 GPU.
When I use a tf.Session()
with a tf.ConfigProto
as follows, it does not work and still uses a GPU.
config = tf.ConfigProto(
device_count = {'GPU': 0}
)
我必须使用环境变量 CUDA_VISIBLE_DEVICES
来禁用 GPU.我后来使用 os.unsetenv()
在我工作后删除这个变量.
I have to use the environment variable CUDA_VISIBLE_DEVICES
to disable the use of a GPU. I later on use os.unsetenv()
to remove this variable after my work.
这些解决方案对我来说没有用,因为对于一个图形,我希望使用 GPU,而对于另一个图形,我不希望使用 GPU.设置 os.environ()
会影响两个图.
These solutions are not useful to me because for one graph I want GPUs to be used and for the other I don't want the GPUs to be used. Setting os.environ()
will affect both the graphs.
我怎样才能达到我的目的?
How can I achieve my purpose ?
推荐答案
tf.ConfigProto() 创建的配置可以设置会话的可见设备.例如:
The config created by tf.ConfigProto() can set the visible devices for the sessions. For exmaple:
config_cpu = tf.ConfigProto()
config_cpu.gpu_options.visible_device_list=''
sess_cpu = tf.Session(config=config_cpu)
config_gpu = tf.ConfigProto()
config_gpu.gpu_options.visible_device_list='0'
sess_gpu = tf.Session(config=config_gpu)
然后会话中的图形 sess_cpu 应仅在 CPU 上运行,会话中的图形 sess_gpu 应仅在 GPU 0 上运行.为了防止tensorflow占用整个GPU内存,您可以设置config_gpu.gpu_options.allow_growth=True
.可以根据您的定制需求采用类似的配置.你可以看看tf.ConfigProto 如果您想使用其他配置.
Then the graph in the session, sess_cpu, should run on CPU only and the graph in the session, sess_gpu, should run on GPU 0 only. To prevent tensorflow occupying the whole GPU memory, you can set config_gpu.gpu_options.allow_growth=True
. Similar configurations can be adopted for your customized needs. You can take a look at the tf.ConfigProto if you want to use other configurations.
这篇关于在 TensorFlow 中以不同的设备模式运行多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!