Tensorflow+PyCharm(Mac)

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 配置

重點:正確配置ProjectInterpreter即可

方法

  • Preferences
  • Project Interpreter
  • Click More

附圖

  • 打開Preferences
打開Preferences
選擇more
  • 打開Project Interpreters
選擇正確的tensorflow路徑
  • Demo運行結果
demo運行結果

原文地址:http://chars.tech/tensorflow

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章