强制 TensorFlow-GPU 从命令行使用 CPU [英] Forcing TensorFlow-GPU to use the CPU from the command line

查看:99
本文介绍了强制 TensorFlow-GPU 从命令行使用 CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have TensorFlow-GPU 1.0.0 installed on a server running Ubuntu 14.04.4 LTS x64.

我知道我可以使用 CUDA_VISIBLE_DEVICES 来隐藏一个或多个 GPU.有时,我想隐藏所有 GPU,以便基于 TensorFlow 的程序仅使用 CPU.结果,我尝试了

I know I can use CUDA_VISIBLE_DEVICES to hide one or several GPUs. Sometimes, I would like to hide all GPUs so that the TensorFlow-based program only uses the CPU. As a result, I tried

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

I know I could use device_count={'GPU': 0} to prevent the TensorFlow-based program from using the GPU, but I wonder whether this can be achieved from the command line when launching the program (without changing the ConfigProto).

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

The option allow_soft_placement=True, according to the documentation, is supposed to have TensorFlow automatically choose an existing and supported device to run the operations in case the specified one doesn't exist.

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

My first reaction when I saw the message was that CUDA needs at least one GPU to successfully load but I read that one can install GPU drivers and use TensorFlow-GPU on a machine even if the machine doesn't have GPU.

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

Here is the my_script.py script I use for the test:

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",这只是一个信息性消息,不应影响程序的运行

I think that "E" should've been a "W" or "I", that's just an informational message and should not affect functioning of your program

这篇关于强制 TensorFlow-GPU 从命令行使用 CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆