Tensorflow 运行 版本仅在 CPU 上带有 CUDA

Tensorflow running version with CUDA on CPU only

我在集群上 运行ning tensorflow。我安装了 CUDA 版本。它工作没有任何问题。要使用 GPU,我必须请求资源。现在,我只想在 CPU 上 运行 而不请求 GPU 资源。

On import tensorflow as tf, I get the error:
ImportError: /home/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: undefined symbol: cuDevicePrimaryCtxRetain


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

我意识到我必须只在 CPU 上 运行 并设置环境变量 CUDA_VISIBLE_DEVICES=""。我是通过 export 在 bash 和 python 脚本上完成的。我仍然遇到同样的错误。

如何仅在 CPU 上使用 GPU 版本的 tensorflow?可能吗?其他一些页面,例如 建议更改会话配置参数。但是由于我在 import 本身得到错误,我认为这不适用或没有帮助。

堆栈跟踪:

File "<FileNameReplaced>", line 10, in <module>
    import tensorflow as tf
  File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 51, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 52, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)

附加信息:

版本:1.1.0

看看issue #2175 in the TensorFlow repo, where this problem is discussed. What worked for me was to set CUDA_VISIBLE_DEVICES="-1", not "", following to the documentation of CUDA environment variables。当您第一次创建会话时,它可能会产生一些警告,但计算应该可以正常进行。如果您使用的是 Bash 或类似的,您可以通过在 运行 程序之前导出它来完成此操作,就像您说的那样,或者只是使用:

$ CUDA_VISIBLE_DEVICES="-1" python my_program.py

或者,一个可能更便携的解决方案是让 Python 自己设置环境变量 ,然后 TensorFlow 被任何模块导入:

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
import tensorflow as tf

另一位用户建议按以下方式创建您的会话:

import tensorflow as tf

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

with tf.Session(config=session_conf) as sess:
    sess.run(...)

这也应该允许您进行更细粒度的控制(例如,我有两个 GPU,但只希望 TensorFlow 使用其中之一)。