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 上查看更新日志。
    原文作者:tensorflow
    原文地址: https://www.cnblogs.com/nosqlcoco/p/6923861.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞