今天在linux上安裝tensorflow遇到一些坑,因此寫下博客記錄一下。python
一、安裝anacondalinux
anaconda組織了很是多的Python科學計算、數據分析的包,好比numpy和pandas等,不須要咱們一個一個地安裝這些包,很是方便。docker
瀏覽器進入anaconda下載頁,下載對應版本的 .sh 包,下載完畢後打開終端,運行該下載文件便可安裝ubuntu
$ /usr/Downloads/Anaconda2-x.x.x-Linux-xx.sh
一路按下 enter 鍵,按照默認配置安裝api
而後,終端輸入 $ conda -V ,若是顯示版本號即說明安裝成功。不然多是環境變量的問題。瀏覽器
二、安裝tensorflowapp
須要注意的是tensorflow官方支持的linux發行版本爲ubuntu,不過其它版本也能夠安裝和使用,只是bug的fix支持力度不大python2.7
安裝教程在 這裏 若是沒法訪問,請往下看,不然直接跳第三步google
在anaconda中建立名字爲"tensorflow"的環境,我理解爲anaconda的命名空間,能夠統一管理包的引入問題,稍後解釋spa
$ conda create -n tensorflow pip python=2.7 # python版本請根據須要更換,好比3.6
激活這個環境:
$ source activate tensorflow
此時你的終端的前面會出現(tensorflow)字樣,表明你目前在tensorflow環境中,則你可使用和管理屬於這個環境的包,注意若是要使用這裏的包須要先激活它
如今,在這個環境中安裝tensorflow必須的包:
$ pip install --ignore-installed --upgrade tfBinaryURL
注意 tfBinaryURL 須要根據tensorflow版本而改變,對應的URL以下:
若是你的電腦沒有NVDIA GPU,則須要安裝cpu-only版本
python2.7 https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.6.0-cp27-none-linux_x86_64.whl
python3.6 https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.6.0-cp36-cp36m-linux_x86_64.whl
NVDIA GPU supported:*
python2.7 https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.6.0-cp27-none-linux_x86_64.whl
python3.6 https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.6.0-cp36-cp36m-linux_x86_64.whl
*注意:若是你安裝gpu版本則須要提早啓動nvdia-docker容器:
$ nvidia-docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow:latest-gpu # 8888:8888是hostport:containerport 後面是tensorflowiGPUImage
按上述配置,你能夠在jupyter notebook使用GPU supported的tensorflow
如今,檢驗你在該一步的安裝狀況:
確保你的tensorflow環境是激活狀態,輸入
$ python
則出現 >>> 的python交互prompt
輸入:
1 import tensorflow as tf 2 3 hello = tf.constant('Hello, TensorFlow!') 4 5 sess = tf.Session() 6 7 print(sess.run(hello))
若是顯示 Hello, TensorFlow! 則配置成功,不然多是環境的問題
三、使用notebook
爲了在notebook中能夠直接使用tensorflow的包,須要在tensorflow被激活狀況下,安裝
$ conda install ipykernel # ipykernel是你在該環境使用notebook的cond內核,
而後進入notebook
$ jupyter notebook
進入編輯界面,更改你的notebook內核
菜單欄,依次 Kernel -> chage kernel -> Python [conda env:tensorflow]
在編輯框中輸入
import tensorflow as tf
若沒有ERROR則這一步配置成功,不然conda內核沒有配置成功
解釋上面提到的conda環境,最直接的影響就是你在當前環境下只能引入當前環境的包,即使在conda環境中安裝的包對你也是不可見的。