从命令行强制 TensorFlow-GPU 使用 CPU

2024-04-18

我在运行 Ubuntu 14.04.4 LTS x64 的服务器上安装了 TensorFlow-GPU 1.0.0。

我知道我可以使用CUDA_VISIBLE_DEVICES https://stackoverflow.com/a/42382106/395857隐藏一个或多个 GPU。有时,我想隐藏所有 GPU,以便基于 TensorFlow 的程序仅使用 CPU。结果我尝试了

username@server:/scratch/coding/src$ CUDA_VISIBLE_DEVICES="" python my_script.py 

但这给了我错误消息:

E tensorflow/stream_executor/cuda/cuda_driver.cc:509] failed call to cuInit: CUDA_ERROR_NO_DEVICE

这里是ConfigProto I use:

session_conf = tf.ConfigProto(
      device_count={'CPU': 1, 'GPU': 1},
      allow_soft_placement=True, 
      log_device_placement=False
      )
sess = tf.Session(config=session_conf)

我知道我可以使用device_count={'GPU': 0}防止基于 TensorFlow 的程序使用 GPU,但我想知道这是否可以在启动程序时从命令行实现(无需更改ConfigProto).

选项allow_soft_placement=True根据文档,应该让 TensorFlow 自动选择现有且受支持的设备来运行操作,以防指定的设备不​​存在。

当我看到这条消息时,我的第一反应是 CUDA 需要至少一个 GPU 才能成功加载,但我read https://stackoverflow.com/a/42287754/395857即使机器没有 GPU,也可以在机器上安装 GPU 驱动程序并使用 TensorFlow-GPU。


这里是my_script.py我用于测试的脚本:

import tensorflow as tf
a = tf.constant(1, name = 'a')
b = tf.constant(3, name = 'b')
c = tf.constant(9, name = 'c')
d = tf.add(a, b, name='d')
e = tf.add(d, c, name='e')

session_conf = tf.ConfigProto(
          device_count={'CPU': 1, 'GPU': 1},
          allow_soft_placement=True,
          log_device_placement=False
          )
sess = tf.Session(config=session_conf)
print(sess.run([d, e]))

我认为“E”应该是“W”或“I”,这只是一条信息性消息,不应影响程序的运行

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从命令行强制 TensorFlow-GPU 使用 CPU 的相关文章

随机推荐