Tensorflow极速上手: windows下的安装

本文简单介绍tensorflow在windows中的安装方法。目前在windows中的tensorflow必须是python3.5的64位版本。这个安装流程中我会强烈建议先安装conda。

(conda的好处是它可以在一台电脑中创造了多个虚拟python运行环境,各个环境之间的python版本及包互不干扰。利用conda可以很容易的在一个系统中又装2.7又装3.5,而不会搞的乱七八糟。。。)

我们这里只安装tensorflow的CPU版本。对于支持CUDA的显卡(大部分Nvidia的卡),安装GPU版本运行能更快。不过安装GPU的版本还需要安装一些CUDA驱动。这个文章目的是可以以最快的速度看到运行起来的tensorflow,所以不扯GPU版本了。

本文安装步骤包括以下几步:(1)安装miniconda;(2)安装tensorflow;(3)运行一小段tensorflow代码。

  1. 安装miniconda

Miniconda – Conda

下载python3的64-bit版本即可。注意,一定要64位的版本。(这里选择3.6或者2.7没关系,都可以)。

  1. 安装tensorflow

安装完miniconda之后,进入命令行cmd。注意,要以管理员权限进去。

然后,如果是在国内的话,首先使用下面的命令增加一个清华大学的conda镜像服务器,会把下载速度加速很多。(Tsinghua Open Source Mirror)

conda config –add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config –set show_channel_urls yes
然后,在命令行中,利用conda的命令创建一个新的python3.5运行环境,注意,一定要用3.5,不要3.6,也不要2.7(更新:最新的tensorflow在windows的安装已经支持3.6了)。否则后一步pip install会找不到tensorflow。

conda create -n tensorflow python=3.5
中间询问y/n,就直接写y再回车。-n 后面写的就是环境的名称,我这里写的是’tensorflow’,但实际上可以随便写一个你想用的名字

启动该环境

activate tensorflow
(tensorflow) > pip install tensorflow
然后正常的话,等就好了:)

  1. 运行一小段tensorflow代码

安装完成后,输入python回车

(tensorflow) > python

import tensorflow as tf
hello = tf.constant(‘Hello, TensorFlow!’)
sess = tf.Session()
print(sess.run(hello))

TF2 runs Eager Execution by default, thus removing the need for Sessions. If you want to run static graphs, the more proper way is to use tf.function() in TF2. While Session can still be accessed via tf.compat.v1.Session() in TF2, I would discourage using it. It may be helpful to demonstrate this difference by comparing the difference in hello worlds:

TF1.x hello world:

import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(msg))

TF2.x hello world:

import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
tf.print(msg)


正常的话,应该就能在最底下看到输出的Hello, TensorFlow! (上面可能会有一些Tensorflow初始化时输出的其他信息)

嗯,这样Tensorflow就安装好了。

Leave a Reply

Your email address will not be published. Required fields are marked *