2016-07-06 296 views
9

我已經使用anaconda環境安裝tensorflow,如tensorflow website中所述,並且在更改我的python安裝路徑之後。如何從Conda在Jupyter Notebook中導入TensorFlow並支持GPU?

[email protected]:~$ which python                         
/home/dennis/anaconda2/bin/python 

Jupyter已安裝。我認爲,如果我能夠在conda環境中導入和使用tensorflow,我將能夠在Jupyter中執行相同的操作。但是,情況並非如此 -

在我的系統導入tensorflow(未激活環境)

[email protected]:~$ 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()                               

導入tensorflow在暢達環境

[email protected]:~$ source activate tensorflow                      
prepending /home/dennis/anaconda2/envs/tensorflow/bin to PATH                  
(tensorflow) [email protected]:~$ 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環境中運行。那麼,你能告訴我,我該如何強迫它做同樣的事情?

或者,你可以爲我提供有關如何導入tensorflow在jupyter

編輯#1細節:

我已成功安裝tensorflow使用conda install -c jjhelmus tensorflow=0.9.0命令Anaconda安裝。 [來源: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。

+0

你在哪個平臺上?如果你在linux上,谷歌現在已經制作了一個預製的二進制文件,你可以很容易地使用anaconda。 https://www.tensorflow.org/versions/r0.9/get_started/os_setup.html#anaconda-installation – user2561747

回答

17

你有沒有安裝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

希望這可以幫助。

+0

是的,我能夠在相同的頻道內重新安裝它,沒有任何問題。但是我第一次無法弄清楚什麼是錯誤的。 –

+0

沒有爲我工作 – user798719

0

Tensorflow 0.9與GPU的蟒蛇Python的2

對於Linux的,使用谷歌的預構建的二進制使用CUDA 7.5和CuDNN V4(https://www.tensorflow.org/versions/r0.9/get_started/os_setup.html#anaconda-installation):

Pseudoscript: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

Pseudoscript:https://gist.github.com/nathanielatom/8c51c91d4bde3e37db0db705e8822e70

+0

正如您所提到的,我已經使用您描述的鏈接來安裝tensorflow和GPU支持。原來問題出在路徑變量的設置上。我之前設置了export PATH =「/ home/dennis/anaconda2/bin:$ PATH」,這導致了問題。它被設置路徑爲出口PATH =後固定「$ PATH:/家庭/丹尼斯/ anaconda2/BIN」 –

+0

請添加詳細介紹一下設置的路徑,森蚺,這樣我可以接受你的答案 –

+0

與蟒蛇的路徑應已經從你的anaconda安裝安裝。 PATH =「/ home/dennis/anaconda2/bin:$ PATH」前置意味着anaconda python肯定是執行的那個。事實上,當你將它移動到最後意味着你實際上正在執行一個不同的python(也許是系統python?)。嘗試'哪個python'或檢查在開始解釋程序會話時是否在前導碼中打印anaconda版本。另外,請檢查'哪個pip',因爲它似乎可能已經將tensorflow安裝到系統/其他python中。 – user2561747