1. 程式人生 > >Python3.6 安裝Tensorflow初體驗

Python3.6 安裝Tensorflow初體驗

Google總是一個能夠讓人感到興奮的公司,今天看了李飛飛的部落格宣稱釋出雲視訊智慧介面程式,就是基於Tensorflow的,感覺其強大之外,就是Google對產品的生態系統搭建已經有了很成熟的進步,真是按捺不住內心的澎湃,也是為了平復最近煩躁的心情。

Tensorflow 全網最全學習資料彙總之Tensorflow 的入門與安裝【2】

A. 我安裝Tensorflow的機器是作業系統為macOS Sierra,系統自帶的Python是2.6, 因為比較喜歡Python3.x的語法,因此打算基於最新的python3.6. 開始安裝之前,看到網上很多基於Python3.x版本的安裝過程遇到很多問題,看到一位仁兄用anaconda來安裝Tensorflow成功了,所以我也打算這麼做,雖然我覺得miniconda更輕便,靈活,但是考慮到tensorflow依賴的安裝package還是挺多的。

安裝Anaconda很順利,也很簡單,mac版下載地址:https://www.continuum.io/downloads#osx

安裝好Anaconda後,需要建立一個虛擬工作環境,使用Anaconda能建立自己的計算環境,相當於將tensorflow的環境與其他環境做了隔離,這樣你就可以隨便玩tensorflow也不用擔心破壞之前的環境!

$ conda create -n tensorflow python=3.6

建立了tensorflow的隔離環境後,要做的是啟用tensorflow環境,然後用pip安裝TensorFlow

$ source activate tensorflow 

B. 接下來就是跟隨Google tensorflow的安裝嚮導,逐步開始安裝TensorFlow。雖然TensorFlow支援從conda直接安裝,但是google同時也宣告,conda並沒有得到官方的支援,Google還是推薦通過pip來安裝,因為我已經用了python3.6 所以需要安裝 pip3, 在安裝pip3的過程中遇到了一些問題,anaconda中安裝的pip的確是最新的,但是後面如果使用pip安裝tensorflow,就會選擇自動適配Python2.7,這裡也是耽誤我最多時間的過程,隨後找到了一個解決方案,與大家分享一下:http://vinyll.scopyleft.fr/install-python3-and-pip-on-a-mac-os/ 也就是說,anaconda 並沒有pip3的引用,你需要自己建立一個pip3的軟連線,這樣你就可以使用pip3了。後面也的確證實,只有pip3才能夠適配Python3.6來安裝tensorflow。

$ ln -s /Users/xa/anaconda3/bin/pip /Users/xa/anaconda3/bin/pip3

我安裝的pip版本相對較高,Google安裝文件建議使用pip3命令來安裝TensorFlow TensorFlow需要的所有包進入被啟用的Virtualenv環境:

$ pip3 -V

pip 9.0.1 from /Users/weijun/anaconda3/lib/python3.6/site-packages (python 3.6)

C, $ pip3 install --upgrade tensorflow

 $ pip3 install --upgrade \
 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.0.1-py3-none-any.whl
 接下來就是等待了,如果你有代理,亦或者足夠幸運的話,2分鐘就可以搞定tensorflow的安裝過程。

D. 測試一下我們安裝的Tensorflow

首先保證你是在啟用的虛擬環境中,也就是我們第一步驟中提及的

啟動python3.6

(root) weijuns-mbp:Documents weijun$ python
Python 3.6.0 |Anaconda 4.3.0 (x86_64)| (default, Dec 23 2016, 13:19:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> node1 = tf.constant(3.0, tf.float32)
>>> node2 = tf.constant(4.0)
>>> print(node1, node2)

Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)
>>> sess = tf.Session()
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
>>> print(sess.run([node1, node2]))
[3.0, 4.0]

至此tensorflow 基於Python3.6就算安裝成功了!

接下來我們進入tensorflow另外一個精彩的元件 tensorboard,啟動tensorboard的命令如下:

$ python -m tensorflow.tensorboard --logdir=~/Documents/path/to/logs