windows 下 Anaconda 安裝 TensorFlow


什么是 Anaconda?

Anaconda is the leading open data science platform powered by Python.
Anaconda 是一個由 Python 語言編寫領先的開放數據科學平台

什么是 TensorFlow?

TensorFlow is an open source software library for numerical computation using data flow graphs.
TensorFlow是一個開源軟件庫,用於使用數據流圖進行數值計算。

TensorFlow r0.12 及以后版本添加了對 windows 系統的支持,自此實現了三大平台,一套代碼多平台運行。安裝 TensorFlow 方式有很多種,下面使用 Anaconda 在 windows10 安裝 TensorFlow (CPU版)。

1. 下載 Anaconda

https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 尋找你與你電腦系統對應的版本,我這里使用 Anaconda3-4.2.0-Windows-x86_64.exe

https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-4.2.0-Windows-x86_64.exe

下載並安裝完成后,打開 CMD, 輸入 'conda --version', 如果輸出如下信息

conda 4.2.0

Anaconda 安裝成功。

接下來需要設置 Anaconda 倉庫鏡像,因為默認連接的是國外鏡像地址,下載速度比較慢,我們把鏡像地址改為清華大學開源軟件鏡像站,打開 Anaconda Prompt, 輸入:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

2.安裝 TensorFlow

繼續在 Anaconda Prompt 窗口輸入:

conda create -n tensorflow python=3.5

按回車。

表示創建 TensorFlow 依賴環境,TensorFlow 目前不支持Python3.6,這里我們使用Python3.5。

繼續看控制台輸出:

Fetching package metadata ...............
Solving package specifications: .

Package plan for installation in environment D:\Program Files\anaconda\envs\tensorflow:

The following NEW packages will be INSTALLED:

    pip:            9.0.1-py35_1  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    python:         3.5.3-0       https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    setuptools:     27.2.0-py35_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    vs2015_runtime: 14.0.25123-0  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    wheel:          0.29.0-py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free

Proceed ([y]/n)? y

提示我們安裝哪些依賴軟件,輸入‘y’,回車。

控制台繼續輸出:

python-3.5.3-0 100% |###############################| Time: 0:00:42 754.91 kB/s
setuptools-27. 100% |###############################| Time: 0:00:00   1.92 MB/s
wheel-0.29.0-p 100% |###############################| Time: 0:00:00   2.68 MB/s
pip-9.0.1-py35 100% |###############################| Time: 0:00:00   2.31 MB/s
#
# To activate this environment, use:
# > activate tensorflow
#
# To deactivate this environment, use:
# > deactivate tensorflow
#
# * for power-users using bash, you must source
#

開始下載安裝依賴軟件,我這里使用的是清華大學鏡像倉庫,所以下載速度很快。

安裝 CPU 版本:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ https://mirrors.tuna.tsinghua.edu.cn/tensorflow/windows/cpu/tensorflow-1.1.0-cp35-cp35m-win_amd64.whl

如果控制台最終輸出 如下信息表示安裝成功。
你也可以打開 https://mirrors.tuna.tsinghua.edu.cn/tensorflow/ 選擇合適的 whl 文件地址進行安裝;或者打開https://mirrors.tuna.tsinghua.edu.cn/help/tensorflow/ 可視化選擇 whl 版本。

Successfully installed numpy-1.12.1 protobuf-3.3.0 six-1.10.0 tensorflow-1.1.0 werkzeug-0.12.2

繼續輸入:

activate tensorflow

激活 TensorFlow 虛擬環境,當不使用 TensorFlow 時,使用 deactivate tensorflow 關閉。

3.測試

進入到 Anaconda 安裝目錄下 /envs /tensorflow 文件夾,繼續在 Anaconda Prompt 窗口輸入輸入:

python.exe

回車后,復制復制如下內容拷貝到Anaconda Prompt,自動輸出:

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
>>> sess.run(hello)
>>> a = tf.constant(10)
>>> b= tf.constant(32)
>>> sess.run(a+b)
>>>

輸出:

...
b'Hello, TensorFlow!'
...
42

表示 TensorFlow 已經安裝成功。

4.有些坑

  • 如果使用 py 文件編寫tensorflow 腳本,文件名不能使用 tensorflow.py
  • 目前 API 變動比較大,例如tf.mul、tf.sub 和 tf.neg 被棄用,現在使用的是 tf.multiply、tf.subtract 和 tf.negative. 使用不當會輸出 AttributeError: module 'tensorflow' has no attribute 'mul' 等錯誤,需要我們經常 github 上查看更新日志。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM