MacOS 搭建 Tensorflow PyCharm 環境

TensorFlow 最初由Google大腦小組(隸屬於Google機器智能研究機構)的研究員和工程師們開發出來,用於機器學習和深度神經網絡方面的研究,但這個系統的通用性使其也可普遍用於其餘計算領域。目前來講,Github上star最多的項目就是它了。python

在這以前,筆者寫過一篇簡單的入門文章《初探 TensorFlow》。當時沒能成功搭建環境,加上後期的工做緣由,至此擱置了一段時間。今天,終於各類折騰,在本身的Mac上通過多種嘗試以後,完美搭建成功。這裏就把它分享出來,但願對你們有所幫助。linux

基於 Anaconda 的安裝

Anaconda 是一個集成許多第三方科學計算庫的 Python 科學計算環境,Anaconda 使用 conda 做爲本身的包管理工具,同時具備本身的計算環境,相似 Virtualenv.git

Virtualenv 同樣,不一樣 Python 工程須要的依賴包,conda 將他們存儲在不一樣的地方。 TensorFlow 上安裝的 Anaconda 不會對以前安裝的 Python 包進行覆蓋.github

  • 安裝 Anaconda
  • 創建一個 conda 計算環境
  • 激活環境,使用 conda 安裝 TensorFlow
  • 安裝成功後,每次使用 TensorFlow 的時候須要激活 conda 環境

安裝 Anaconda :

參考 Anaconda 的下載頁面的指導c#

創建環境

創建一個 conda 計算環境名字叫tensorflow:api

# 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標記防止錯誤的產生。bash

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 環境激活後,你能夠測試:ide

$ 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運行結果
相關文章
相關標籤/搜索