windows 下 Anaconda 安装 TensorFlow

转自:html

什么是 Anaconda?python

Anaconda is the leading open data science platform powered by Python.
Anaconda 是一个由 Python 语言编写领先的开放数据科学平台git

什么是 TensorFlow?github

TensorFlow is an open source software library for numerical computation using data flow graphs.
TensorFlow是一个开源软件库,用于使用数据流图进行数值计算。sql

TensorFlow r0.12 及之后版本添加了对 windows 系统的支持,自此实现了三大平台,一套代码多平台运行。安装 TensorFlow 方式有不少种,下面使用 Anaconda 在 windows10 安装 TensorFlow (CPU版)。windows

1. 下载 Anaconda

在 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 寻找你与你电脑系统对应的版本,我这里使用 Anaconda3-4.2.0-Windows-x86_64.exebash

下载并安装完成后,打开 CMD, 输入 'conda --version', 若是输出以下信息nosql

conda 4.2.0

Anaconda 安装成功。测试

接下来须要设置 Anaconda 仓库镜像,由于默认链接的是国外镜像地址,下载速度比较慢,咱们把镜像地址改成清华大学开源软件镜像站,打开 Anaconda Prompt, 输入:this

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 上查看更新日志。
相关文章
相关标签/搜索