如何:在支持 GPU 的情况下从 Conda 在 Jupyter Notebook 中导入 TensorFlow?

HOW TO: Import TensorFlow in Jupyter Notebook from Conda with GPU support?

我已经使用 tensorflow website 中提到的 anaconda 环境安装了 tensorflow,并且在完成后我的 python 安装路径发生了变化。

dennis@dennis-HP:~$ which python                                                                                                   
/home/dennis/anaconda2/bin/python  

并且安装了 Jupyter。我假设如果我能够在 conda 环境中导入和使用 tensorflow,我将能够在 Jupyter 中执行相同的操作。但事实并非如此 -

在我的系统中导入tensorflow(不激活环境)

dennis@dennis-HP:~$ python                                                                                                         
Python 2.7.11 |Anaconda 4.1.0 (64-bit)| (default, Jun 15 2016, 15:21:30)                                                           
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2                                                                                   
Type "help", "copyright", "credits" or "license" for more information.                                                             
Anaconda is brought to you by Continuum Analytics.                                                                                 
Please check out: http://continuum.io/thanks and https://anaconda.org                                                              
>>> import tensorflow as tf                                                                                                        
Traceback (most recent call last):                                                                                                 
  File "<stdin>", line 1, in <module>                                                                                              
ImportError: No module named tensorflow                                                                                                                                                                                                         
>>> exit()                                                                                                                         

conda环境导入tensorflow

dennis@dennis-HP:~$ source activate tensorflow                                                                                     
prepending /home/dennis/anaconda2/envs/tensorflow/bin to PATH                                                                      
(tensorflow) dennis@dennis-HP:~$ python                                                                                            
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:42:40)                                                         
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2                                                                                   
Type "help", "copyright", "credits" or "license" for more information.                                                             
Anaconda is brought to you by Continuum Analytics.                                                                                 
Please check out: http://continuum.io/thanks and https://anaconda.org                                                              
>>> import tensorflow as tf                                                                                                        
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally                              
I tensorflow/stream_executor/dso_loader.cc:102] Couldn't open CUDA library libcudnn.so. LD_LIBRARY_PATH: /usr/local/cuda-7.5/lib64 
I tensorflow/stream_executor/cuda/cuda_dnn.cc:2092] Unable to load cuDNN DSO                                                       
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally                               
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so locally                                
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so locally

由于上述导入成功,我尝试在 jupyter 中执行相同操作 (在环境中启动 jupyter) 但在导入时出现以下错误 -

ImportError                               Traceback (most recent call last)
<ipython-input-1-41389fad42b5> in <module>()
----> 1 import tensorflow as tf

ImportError: No module named tensorflow

我的猜测是笔记本不在conda环境中运行。那么,你能告诉我如何强制它做同样的事情吗?

或者您可以向我提供有关如何在 jupyter 中导入 tensorflow 的详细信息

编辑#1:

我已经使用 conda install -c jjhelmus tensorflow=0.9.0 命令在 anaconda 安装中成功安装了 tensorflow。 [来源:conda.anaconda.org/jjhelmus]

但这会禁用 GPU 支持,因此像下面这样的代码 returns 会出错

with tf.Session() as sess:
  with tf.device("/gpu:0"): #GPUs are not enabled on the system so it throws an error
    matrix1 = tf.constant([[3., 3.]])
    matrix2 = tf.constant([[2.],[2.]])
    product = tf.matmul(matrix1, matrix2)
    result = sess.run([product])
    print result

那么,如何启用 GPU 支持?是否有替代解决方案在支持 GPU 的 conda 中安装 tensorflow?

编辑#2:

有人提到 ,只有在为目标 GPU 构建源代码时才可能支持 GPU。 如果这是真的,请提供有关如何完成的详细信息 以便我安装启用 GPU 的 tensorflow。

带有适用于 Anaconda 的 GPU 的 Tensorflow 0.9 Python 2

对于 linux,使用 Google 的预构建二进制文件与 Cuda 7.5 和 CuDNN v4 (https://www.tensorflow.org/versions/r0.9/get_started/os_setup.html#anaconda-installation):

伪脚本:https://gist.github.com/nathanielatom/ccdf39d9f20dca4c9e418ea0e00ccd25

对于 Mac,使用 Cuda 7.5 和 CuDNN v5.1 RC (https://www.tensorflow.org/versions/r0.9/get_started/os_setup.html#installation-for-mac-os-x)

从源代码安装

伪脚本:https://gist.github.com/nathanielatom/8c51c91d4bde3e37db0db705e8822e70

你有没有在tensorflow环境中安装过jupyter

键入 which jupyter 进行查找。结果:

(tensorflow) [..]$ <anaconda_home>/envs/tensorflow/bin/jupyter # installed within the tensorflow environment.
(tensorflow) [..]$ <anaconda_home>/bin/jupyter                 # not installed.

如果未安装,请在 tensorflow 环境中键入 pip install jupyter。然后再次尝试在笔记本中import tensorflow

希望对您有所帮助。