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

Posted

技术标签:

【中文标题】如何:在支持 GPU 的 Conda 中导入 Jupyter Notebook 中的 TensorFlow?【英文标题】:HOW TO: Import TensorFlow in Jupyter Notebook from Conda with GPU support? 【发布时间】:2016-11-09 02:16:27 【问题描述】:

我已经使用 tensorflow website 中提到的 anaconda 环境安装了 tensorflow,并且在更改了我的 python 安装路径之后。

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

并且安装了 Jupyter。我假设如果我能够在 conda 环境中导入和使用 tensorflow,我将能够在 Jupyter 中做同样的事情。但事实并非如此——

在我的系统中导入张量流(不激活环境)

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 支持,因此类似下面的代码会返回错误

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:

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

【问题讨论】:

你在哪个平台上?如果你在 linux 上,谷歌现在已经制作了一个预构建的二进制文件,你可以很容易地与 anaconda 一起使用。 tensorflow.org/versions/r0.9/get_started/… 【参考方案1】:

您是否曾经在 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

希望这能有所帮助。

【讨论】:

是的,我能够在相同的频道中重新安装它而不会出现问题。但我第一次无法找出到底是什么问题。【参考方案2】:

Tensorflow 0.9 with GPU for Anaconda 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

【讨论】:

正如您所提到的,我已经使用您描述的链接来安装具有 GPU 支持的 tensorflow。原来问题出在路径变量的设置上。我之前设置了 export PATH="/home/dennis/anaconda2/bin:$PATH" 这导致了问题。将路径设置为 export PATH="$PATH:/home/dennis/anaconda2/bin" 后已修复 请添加有关将 PATH 设置为 anaconda 的详细信息,以便我接受您的回答 anaconda 的 PATH 应该已经从你的 anaconda 安装中设置好了。将 PATH="/home/dennis/anaconda2/bin:$PATH" 放在前面意味着 anaconda python 肯定会被执行。当您将它移到最后时它起作用的事实意味着您实际上正在执行不同的python(也许是系统python?)。尝试which python 或检查启动解释器会话时是否在序言中打印出 anaconda 版本。另外,请检查which pip,因为您似乎已将 tensorflow 安装到系统/其他 python 中。 如果是这样(anaconda 没有出现在 pips 路径中),请尝试卸载系统/其他 pip,确保 which pip 为您提供 anaconda 版本(/home/dennis/anaconda2/bin/pip)并重新安装张量流点子包。成功安装 anaconda 后,tensorflow 应该位于/home/dennis/anaconda2/lib/python2.7/site-packages/tensorflow;所以尝试ls。还有一堆有用的示例网络。

以上是关于如何:在支持 GPU 的 Conda 中导入 Jupyter Notebook 中的 TensorFlow?的主要内容,如果未能解决你的问题,请参考以下文章

在 ubuntu18.04 中导入错误 tensorflow-gpu

基于docker搭建conda深度学习环境(支持GPU加速)

基于docker搭建conda深度学习环境(支持GPU加速)

在 Anaconda Python 中导入自己的模块

无法在 Jupyter Notebook 中导入 TensorFlow

如何安装支持 gpu 的 Keras?