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

Python 从命令行强制TensorFlow GPU使用CPU,python,tensorflow,Python,Tensorflow,我在运行Ubuntu 14.04.4 LTS x64的服务器上安装了TensorFlow GPU 1.0.0 我知道我可以用来隐藏一个或几个GPU。有时,我想隐藏所有GPU,以便基于TensorFlow的程序只使用CPU。结果,我尝试了 username@server:/scratch/coding/src$ CUDA_VISIBLE_DEVICES="" python my_script.py 但这给了我一个错误信息: E tensorflow/stream_executor/cuda/c

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

我知道我可以用来隐藏一个或几个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

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\u count={'GPU':0}
来阻止基于TensorFlow的程序使用GPU,但我想知道在启动程序时是否可以通过命令行实现这一点(不更改
ConfigProto

根据文档,选项
allow\u soft\u placement=True
,应该让TensorFlow自动选择一个现有且受支持的设备来运行操作,以防指定的设备不存在

当我看到这条消息时,我的第一反应是CUDA至少需要一个GPU才能成功加载,但我相信即使机器没有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”,这只是一个信息性消息,不应该影响程序的运行,我一直都能看到work@YaroslavBulatov很高兴知道。@YaroslavBulatov顺便说一句,欢迎您将您的评论转换为答案:0 upvote和0答案的堆栈交换问题可能会自动删除。