TensorFlow 最初由Google大腦小組(隸屬於Google機器智能研究機構)的研究員和工程師們開發出來,用於機器學習和深度神經網絡方面的研究,但這個系統的通用性使其也可廣泛用於其他計算領域。目前來說,Github上star最多的項目就是它了。
在這之前,筆者寫過一篇簡單的入門文章《初探 TensorFlow》。當時沒能成功搭建環境,加上後期的工作原因,至此擱置了一段時間。今天,終於各種折騰,在自己的Mac上經過多種嘗試之後,完美搭建成功。這裏就把它分享出來,希望對大家有所幫助。
基於 Anaconda 的安裝
Anaconda 是一個集成許多第三方科學計算庫的 Python
科學計算環境,Anaconda
使用 conda
作爲自己的包管理工具,同時具有自己的計算環境,類似 Virtualenv
.
和 Virtualenv
一樣,不同 Python
工程需要的依賴包,conda
將他們存儲在不同的地方。 TensorFlow
上安裝的 Anaconda
不會對之前安裝的 Python
包進行覆蓋.
- 安裝 Anaconda
- 建立一個
conda
計算環境 - 激活環境,使用
conda
安裝TensorFlow
- 安裝成功後,每次使用
TensorFlow
的時候需要激活conda
環境
安裝 Anaconda :
參考 Anaconda 的下載頁面的指導
建立環境
建立一個 conda 計算環境名字叫tensorflow
:
# Python 2.7
$ conda create -n tensorflow python=2.7
# Python 3.4
$ conda create -n tensorflow python=3.4
激活
激活tensorflow
環境,然後使用其中的 pip
安裝 TensorFlow
. 當使用easy_install
使用--ignore-installed
標記防止錯誤的產生。
URL of the TensorFlow Python package
$ source activate tensorflow
(tensorflow)$ # Your prompt should change
# Ubuntu/Linux 64-bit, CPU only, Python 2.7:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7. Requires CUDA toolkit 7.5 and CuDNN v4.
# For other versions, see "Install from sources" below.
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
# Mac OS X, CPU only:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py2-none-any.whl
對於 Python 3.x :
$ source activate tensorflow
(tensorflow)$ # Your prompt should change
# Ubuntu/Linux 64-bit, CPU only, Python 3.4:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp34-cp34m-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4. Requires CUDA toolkit 7.5 and CuDNN v4.
# For other versions, see "Install from sources" below.
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp34-cp34m-linux_x86_64.whl
# Mac OS X, CPU only:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py3-none-any.whl
conda 環境激活後,你可以測試:
$ python
>>> import tensorflow as tf
>>> print(tf.__version__)
# 0.11.0rc0
開啓或關閉環境
當你不用 TensorFlow 的時候,關閉環境:
(tensorflow)$ source deactivate
$ # Your prompt should change back
再次使用的時候再激活 :
$ source activate tensorflow
(tensorflow)$ # Your prompt should change.
# Run Python programs that use TensorFlow.
...
# When you are done using TensorFlow, deactivate the environment.
(tensorflow)$ source deactivate
PyCharm 配置
重點:正確配置Project
的Interpreter
即可
方法
- Preferences
- Project Interpreter
- Click More
附圖
- 打開Preferences
- 打開Project Interpreters
- Demo運行結果