TensorFlow官方教程学习笔记(一)——起步

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wspba/article/details/54089132

教程地址:TensorFlow官方文档中文版


TensorFlow可以拆成两个词:Tensor(张量)和Flow(流),Tensor代表最底层的数据结构,每一个Tensor可以简易的理解为一个多维数组,类似于Caffe中的Blob,不过与Blob不同的是,对于一张图片,Tensor的四个维度分别是[batch, height, width, channel](Blob的为[batch, channel, height, width])。Tensor存在于节点op(operation)中,op执行计算功能,值得注意的是,Tensor并不真正具有op的输出值(it does not hold the values of that operation's output),它只是代表了这个op操作的返回值,它也可以作为另外一个op的输入。一个Tensor可以在多个op中流动(Flow),这样就组成了一个图(Graph),当图构建好之后,上下文(context)就会启动一个会话(session)来执行图中的op,这时Tensor的值才被计算出来。


创建一个默认图(default graph):

g = tf.Gragh()
with = g.as_default():
它与以下代码是等效的:

with tf.Graph().as_default() as g:
这样context就创建了一个默认图。


创建好一个默认图后,就可以通过插入op来构建图:

g = tf.Gragh()
with = g.as_default():
  matrix1 = tf.constant([[3., 3.]])
  matrix2 = tf.constant([[2.],[2.]])
  product = tf.matmul(matrix1, matrix2)
这里插入了两种类型的op:constant和matmul(常量和矩阵相乘),matrix1、matrix2和product代表op返回值的Tensor。

插入op的方式有很多种,如:

tf.Operation.name       #The full name of this operation.
tf.Operation.type       #The type of the op (e.g. "MatMul").
tf.Operation.inputs     #The list of Tensor objects representing the data inputs of this op.

创建好图后,需要创建一个session来启动这个图,例如上面代码中,只有在session中启动这个图才能真正的进行矩阵相乘的运算,并得到结果:

g = tf.Gragh()
with = g.as_default():
  matrix1 = tf.constant([[3., 3.]])
  matrix2 = tf.constant([[2.],[2.]])
  product = tf.matmul(matrix1, matrix2)
  sess = tf.Session()                      #Construct a `Session` to execut the graph.
  result = sess.run(product)               #Execute the graph and store the value that `product` represents in `result`.
  print result
  sess.close()                             #Close the Session.


 sess.run(product)触发了三个op的执行,返回值 'result' 是一个 numpy `ndarray` 对象。也可以使用以下代码来启动图: 
 

with tf.Session() as sess:
  result = sess.run(product)
  print result


这样就可以在会话结束后自动的关闭会话而不需要调用close()。

除了sess.run(product)来执行op操作以外,用Tensor的操作product.eval()也可以表达同样的效果。

对于Tensor的操作有很多种,如:

tf.Tensor.dtype                                 #The DType of elements in this tensor.
tf.Tensor.name                                  #The string name of this tensor.
tf.Tensor.eval(feed_dict=None, session=None)    #Evaluates this tensor in a Session.
tf.Tensor.get_shape()                           #Returns the TensorShape that represents the shape of this tensor.
tf.Tensor.set_shape(shape)                      #Updates the shape of this tensor.


在计算图时,还可以使用with...device来调用特定的CPU或者GPU:

with tf.Session() as sess:
  with tf.device("/gpu:1"):
    matrix1 = tf.constant([[3., 3.]])
    matrix2 = tf.constant([[2.],[2.]])
    product = tf.matmul(matrix1, matrix2)
    ...
这么多个with,可以把它们合到一起,表达一个完整的计算图的过程:

g = tf.Graph()
with g.as_default(), g.device('/gpu:1'), tf.Session() as sess:
  matrix1 = tf.constant([[3., 3.]])
  matrix2 = tf.constant([[2.],[2.]])
  product = tf.matmul(matrix1, matrix2)
  result = sess.run(product)
  print result


在TensorFlow中还有一个重要的成员:变量(Variable)

在官方文档中,对变量的说明很简单:变量维护图执行过程中的状态信息。这可能不太好理解,我的个人理解是,变量实际上就是通过不断的迭代来更新它的值,最终来满足模型的条件。一个简单的例子:

# 创建一个变量, 初始化为标量 0.
state = tf.Variable(0, name="counter")

# 创建一个 op, 其作用是使 state 增加 1
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

# 启动图后, 变量必须先经过`初始化` (init) op 初始化,
# 首先必须增加一个`初始化` op 到图中.
init_op = tf.initialize_all_variables()

# 启动图, 运行 op
with tf.Session() as sess:
  # 运行 'init' op
  sess.run(init_op)
  # 打印 'state' 的初始值
  print sess.run(state)
  # 运行 op, 更新 'state', 并打印 'state'
  for _ in range(3):
    sess.run(update)
    print sess.run(state)

# 输出:

# 0
# 1
# 2
# 3
变量存在于内存的缓存区,它与Tensor是包含的关系,变量在创建之后,必须被显式的初始化。tf.initialize_all_variables()就是将所有变量初始化的op操作。在session创建之后,需要执行这个op操作。

在整个session执行完之后,可以将变量save,在这之前你需要使用tf.train.Saver()创建一个saver。

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
  sess.run(init_op)
  # Do some work with the model.
  ..
  # Save the variables to disk.
  save_path = saver.save(sess, "/tmp/model.ckpt")
  print "Model saved in file: ", save_path
你也可以对一个已存有的变量进行restore,这里不需要再进行初始化:

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
  # Restore variables from disk.
  saver.restore(sess, "/tmp/model.ckpt")
  print "Model restored."
  # Do some work with the model
  ...
当然你也可以选择某一个变量进行保存:

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore only 'v2' using the name "my_v2"
saver = tf.train.Saver({"my_v2": v2})
# Use the saver object normally after that.
...


在Tensor的操作中有一个很重要的机制:feed,它实际上起着替换的作用,常常用来为run()、eval()中的Tensor提供数据,不过前提是这个需要为这些Tensor创建占位符:tf.placeholder()。具体的使用方法如下:

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)

with tf.Session() as sess:
  print sess.run([output], feed_dict={input1:[7.], input2:[2.]})

# 输出:
# [array([ 14.], dtype=float32)]
代码中,feed_dict的作用就是将[7.]传递给input1,将[2.]传递给input2,从而来进行run()的操作。

值得注意的是,如果你使用tf.placeholder()创建了占位符,而并没有正确的为它提供feed,将会产生很严重的错误。












猜你喜欢

转载自blog.csdn.net/wspba/article/details/54089132