安装Python环境
到python官网下载最新3.6版本。虽然2.7兼容性好,不过tf例子大多3.x版本,所以。
Python 3.6.x 64-bit from python.org
安装时确保选中所有组件,尤其pip3,类似yum的安装工具,一定要带上。记得check上add python to path,把目录设置好,方便命令行。
安装完命令行敲python,出现console,说明安装成功。
可以直接在console里面敲入代码执行,exit()退出。
也可以把代码写入一个文件,例如test.py,直接写入代码:
#this is a test program of python.
print("hello, world!")
然后命令行
python test.py
即可执行。
安装Tensorflow
这个还是要翻墙tensorflow.org官网看文档比较靠谱。首先就来个难题,要选安装哪个:
TensorFlow with CPU support only. If your system does not have a NVIDIA® GPU, you must install this version. Note that this version of TensorFlow is typically much easier to install (typically, in 5 or 10 minutes), so even if you have an NVIDIA GPU, we recommend installing this version first.
TensorFlow with GPU support. TensorFlow programs typically run significantly faster on a GPU than on a CPU. Therefore, if your system has a NVIDIA® GPU meeting the prerequisites shown below and you need to run performance-critical applications, you should ultimately install this version.
Surface Book的显卡并没标明是NVidia,不过按照推荐,还是选CPU support only吧,别找麻烦。
用pip安装:
pip3 install --upgrade tensorflow
过程大约需要5分钟,视网络情况。用Anaconda也可以安装,没试。
安装完成后,可以验证一下。在console中,输入下面代码,看跑的结果:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
运行:
嘿嘿,第一个高大上的tf程序ok了!
继续写程序:
import tensorflow as tf
a=tf.constant(2)
b=tf.constant(3)
with tf.Session() as sess:
print("a=2, b=3")
print("Addition with constants: %i" % sess.run(a+b))
print("Multiplecation with constants: %i" % sess.run(a*b))