原文教程:tensorflow官方教程
翻译教程:极客学院
记录关键内容与学习感受。未完待续。
1、使用Python API编写的tensorflow代码
生成二维数据,并且拟合成一条直线
import tensorflow as tf
import numpy as np
#Create 100 phony x,y data point in Numpy, y=x*0.1+0.3
x_data = np.random.rand(100).astyoe(np.float32)
y_data = x_data * 0.1 + 0.3
#Try to find values for W and b that compute y_data = W*x_data+b
#(we konw that W should be 0.1 and b 0.3,but tensorflow will figure
#that out for us.)
W = tf.Variable(tf.random_uniform([1],-1.0,1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b
# minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
# before starting, initialize the variables. we will 'run' this first.
init = tf.global_variables_initializer()
# launch the graph
sess = tf.Session()
sess.run(init)
# fit the line.
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step,sess.run(W),sess.run(b))
# learn best fit is W:[0.1],b[0.3]
此上第一部分主要创建数据流图,tensorflow在session创建、run函数调用前不会运行任何计算。
2、安装,略
3、基本用法
在使用之前,你需要明白tensorflow:
1、用图graph来表示计算任务
2、在会话Session的上下文中执行图graph
3、用tensors来表示数据
4、通过变量维持状态
5、使用feeds和fetches在任意操作中获取数据
3.1 综述
—–使用图来表示计算任务,图中的节点称为op,一个op携带0到多个tensor,通过计算又产生0到多个tensor,每个tensor是一个类型化的多维数组。
—–图描述了计算过程,图必须在会话Session中启动,会话将图的op分发送到GPU或者CPU设备上,并提供方法计算这些op,这些方法返回的是,在Python中Numpy ndarray对象产生的op,在C、C++中tensorflow::Tensors实例。
3.2 计算图computation graph
—–tensorflow程序经常结构化为一个构建阶段和一个执行阶段。在构建阶段,聚集图,在执行阶段,使用一个会话来执行图中的op。
—–例如,通常在构建阶段,创建一个图来表示和训练神经网络,在执行阶段,反复执行图中的op训练集。
3.2.1建立图
—–第一步,创建源op,源op不需要任何输入,如常数,源op的输出会传给其他op继续做计算。
—–在Python库中,op构造器返回的对象代表构建的这个op的输出,这些输出可以传递给其他op构造器作为输入。
—–tensorflow Python库中有一个默认图,op构造器可以为其添加节点。这个图对于大多数程序已经够用了,如果你想学习如果管理多个图,可以看官方文档
# create a constant op that produces a 1x2 的 matrix, the op
# is added as a node to the default graph.
# the value returned by the constructor represents the output
# of the constant op.
matrix1 = tf.constant([[3.,3.]])
# create another constant that produces a 2x1 matrix
matrix2 = tf.constant([[2.],[2.]])
# create a matmul op that takes matrix1 and matrix2 as inputs.
# the returned value, 'product', represents the result of the matrix
# multiplication
product = tf.matmul(matrix1,matrix2)
—–以上代码,完成图中有三个节点,两个常数op,一个常数op相乘后的结果op。如果你真的想得到这个相乘的结果,你必须在会话中启动图。
3.2.2在一个会话中启动图
—–启动图在构建图完成之后,为了启动一个图,必须创建一个会话对象,如果没有任何参数,会话构造器会启动默认图。
# launch the default graph.
sess = tf.Session()
# to run the matmul op ,we call the session 'run()' method,passing 'product'
# which represents the output of the matmul op. this indicates to the call
# that we want to get the output id the matmul op back.
#
# all inputs needed by the op are run automatically by the session. they
# typically are run in parallel.
#
# the call 'run(product)' thus causes the execution of three ops in the
# graph:the two constants and matmul.
#
# the output of the matmul is returned in 'resutlt' as a numpy 'ndarray' object
result = sess.run(product)
print(result)
# ==> [[12.]]
# close the session when we are done
sess.close()
—–实际运行截图:
—–会话需要关闭以释放资源,也可以用with代码块自动关闭会话,代码如下:
with tf.Session() as sess:
result = sess.run([product])
print(result)
—–在实现上,tensorflow将图定义转换为分布式执行的操作以尽可能的利用计算资源,如CPU和GPU。一般来说不需要我们显示指定CPU或者GPU,如果有1个或多个GPU,tensorflow会自动找第一个GPU来使用。
—–如果有多个GPU,tensorflow只会用第一个。如果你想用其他的,必须自己将op指定给某个GPU。用with–device语句来指定CPU或者GPU。如下:
with tf.Session() as sess:
with tf.device("/gpu:1"):
marix1 = tf.constant([[3.,3.]])
marix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1,matrix2)
result = sess.run([product])
—–设备使用字符串指定,目前支持的设备有:
- “/cpu:0”:自己机器的CPU
- “/gpu:0”:自己机器的GPU,如果你有1个
- “/gpu:1”:你机器的第2块GPU,等等
3.2.3在一个分布式会话中启动图
—–为了创建一个tensorflow集群,在集群的每一个机器上启动一个tensorflow服务器。在你的客户机上,你要通过集群中一个机器的网络位置实例化一个会话。
with tf.Session("grpc://example.org:2222") as sess:
# calls to sess.run(...) will be executed on the cluster
...
—–这台机器是集群中的master,它分配图中的其他机器(worker),跟本地实现时在一台机器上给图分配计算资源是一样的。
—–你可以直接用”with tf.device():”指定workers对图中特定的部分负责。
with tf.device("/job:ps/task:0"):
weights = tf.Variable(...)
biases = tf.Variable(...)
3.3 交互式使用
—–文档中Python的例子中,用会话启动一个图,并且用Session.run()方法执行操作。
—–为了使用如IPython的交互式Python环境,可以使用InteractiveSession类, Tensor.eval() 和 Operation.run()方法来代替Session类和Session.run()方法。这样可以避免使用一个变量来持有会话。
# 8 enter an interactive tensorflow session.
sess = tf.InteractiveSession()
x = tf.Variable([1.0,2.0])
a = tf.constant([3.0,3.0])
# initialize 'x' using the run() method of its initializer op.
x.initializer.run()
# add an op to subtract 'a' from 'x'. run it and print the result.
sub = tf.subtract(x,a)
print(sub.eval())
# ==>[-2.,-1.]
# close the session when we are done
sess.close()
3.4 Tensors
—–tensorflow程序使用tensor数据结构来表示所有数据,在计算图中,只有tensors可以在操作之间传递,你可以把tensorflow tensor看做一个n维数组或者列表。一个tensor包括一个静态类型,一个rank,一个shape。
3.5 Variables
—–变量Variable在图的执行过程中维持状态。下面的例子展示了如何使用一个变量实现一个计数器。
# 9 create a variable, that will be initialized to the scalar value 0.
state = tf.Variable(0,name="counter")
# create an op to add one to 'state'
one = tf.constant(1)
new_value = tf.add(state,one)
update = tf.assign(state,new_value)
# variables must be initialized by running an 'init' op after having
# launched the graph. we first have to add the 'init' op to the graph.
init_op = tf.global_variables_initializer()
# launch the graph and run the ops.
with tf.Session() as sess:
# run the 'init' op
sess.run(init_op)
# print the initial value of 'state'
print(sess.run(state))
# run the op that updates 'state' and print 'state'
for _ in range(3):
sess.run(update)
print(sess.run(state))
# output
# 0
# 1
# 2
# 3
—–实际运行截图
—–assign()操作是图描绘表达式的一部分,与add()操作一样,在run()执行前不会真正的进行赋值操作。
—–通常会将一个统计模型中的参数表示为一个变量集合。例如你想讲神经网络中的权重作为一个变量存储在tensor中。在训练过程中,通过重复执行训练图,来更新tensor。
3.6 Fetches
—–为了取得操作后的输出结果,可以在使用session对象的run()调用执行图时,传入一些tensors,这些tensors会帮你取回结果。在前面的例子中,我们取回了单个节点state,我们也可以取回多个tensors。
input1 = tf.constant([3.0])
input2 = tf,constant([2.0])
input3 = tf.constant([5.0])
intermed = tf.add(input2,input3)
mul = tf.multiply(input1,intermed)
with tf.Session as sess:
result = sess.run([mul,intermed])
print(result)
# output:
# [array([21.],dtyoe=float32),array([7.],dtype=float32)]
—–实际运行结果:
——写到这里,突然发现因为tensorflow发了新版本的原因,有些api的名称做了改动,比如tf.sub需要改成tf.subtract,tf.mul改成tf.nultiply,tf.neg改成tf.negative,而tf.add没有改动,这里将前面代码中已做改变。详细内容可以点击官网说明
—–需要获取的多个tensors值,在op的一次运行中一起获得,而不是单个逐一去获得。
3.7 Feeds
—–上面的例子中介绍了计算图中的tensor,以常量或变量的形式存储。tensorflow也提供了一个feed机制,用来在图的任何操作中直接修改tensor。
—–一个feed可以临时替代任何操作的tensor值的输出。你可以提供feed数据作为run()调用的参数,feed只在调用它的方法内有效,最常见的用例是将某些特定的操作标明为feed操作,通过tf.placeholder() 为这些操作建立占位符。
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1,input2)
with tf.Session as sess:
print(sess.run([output],feed_dict={input1:[7.],input2:[2.]}))
# output
# [array([14.],dtype=float32)]
—–实际运行结果:
—–如果没有正确提供feed,placeholder()操作会产生错误,MNIST
全连通feed 教程(source code) 给出了一个更大规模的使用 feed 的例子。