在Linux上没有CUDA的情况下如何使用TensorFlow?
问题内容:
我有两台没有CUDA的计算机:一台在Microsoft Windows上运行,另一台在Linux上运行(Ubuntu 14.04 64位/ Linux
3.13.0-100通用)
我可以在Microsoft
Windows上不使用CUDA的情况下使用TensorFlow,而不会出现任何问题:TensorFlow使用CPU。但是,如果在Linux计算机上以python运行
import tensorflow as tf
,则由于未安装CUDA,因此TensorFlow无法导入:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 24, in <module>
from tensorflow.python import *
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 72, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 61, in <module>
from tensorflow.python import pywrap_tensorflow
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
_pywrap_tensorflow = swig_import_helper()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
ImportError: libcudart.so.8.0: cannot open shared object file: No such file or directory
Failed to load the native TensorFlow runtime.
See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md#import_error
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
在Linux上没有CUDA的情况下如何使用TensorFlow?
我用tensorflow-gpu==1.0.0
。
我知道的参数device_count
中 tensorflow.ConfigProto
,允许停用GPU,例如:
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')
config = tf.ConfigProto(device_count={'CPU': 1, 'GPU': 0})
sess = tf.Session(config=config)
print(sess.run([d, e]))
但这没有帮助,因为这 import tensorflow as tf
是问题所在。
我也知道如何安装CUDA 8:
# Install Nvidia drivers, CUDA and CUDA toolkit, following some instructions from http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
wget https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64-deb
sudo dpkg -i cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64-deb
sudo apt-get update
sudo apt-get install cuda
但宁愿避免在这两台计算机上使用它。
问题答案:
如果您使用来构建二进制文件--config=cuda
(已完成tensorflow- gpu
),则您的计算机必须具有GPU驱动程序。即使机器没有GPU,也可以在机器上安装GPU驱动程序,这是常见的实用解决方案。
发生的事情是在代码中--config=cuda
设置了GOOGLE_CUDA宏,从而在运行时更改了行为。特别是它会导致dso_loader运行,您可以通过打印以下行来查看
2017-02-16 17:15:08: I tensorflow/stream_executor/dso_loader.cc:125] successfully opened CUDA library libcurand.8.0.dylib locally
Google的一种流行方法是部署“胖”二进制文件-即二进制文件,它将所有可能的硬件加速器的驱动程序和客户端代码捆绑在一起,因为这简化了测试和部署。
在开源版本中,驱动程序和客户端代码是分开的,但是这种模式仍然存在-具有GPU功能的二进制文件有望提供GPU驱动程序。