1、数据流图简介
两个基础构件:节点和边
构建第一个TensorFlow数据流图:
>>> import tensorflow as tf
>>> a = tf.constant(5, name="input_a")
>>> b = tf.constant(3, name="input_b")
>>> c = tf.multiply(a, b, name="mul_c")
>>> d = tf.add(a, b, name="add_d")
>>> e = tf.add(c, d, name="add_e")
>>> sess = tf.Session()
>>> sess.run(e)
# 保存数据流图的数据和概括统计量
>>> writer = tf.summary.FileWriter('./my_graph', sess.graph)
$ tensorboard --logdir="my_graph"
张量:1D张量等价于向量,2D张量等价于矩阵,N维张量
a = tf.constant([5,3], name="input_a")
b = tf.reduce_prod(a, name="prod_b") # 累乘
c = tf.reduce_sum(a, name="sum_c") # 累加
d = tf.add(c, d, name="add_d")
NumPy数组:TensorFlow数据类型是基于NumPy的数据类型的。字符串数据类型
Graph对象:在大多数TensorFlow程序中,只使用默认数据流图就足够了。然而,如果需要定义多个相互之间不存在依赖关系的模型,则创建多个Graph对象十分有用。
Session对象:Session类负责数据流图的执行,tf.Session接收3个可选参数:
.target指定了所要使用的执行引擎。
.graph参数指定了将要在Session对象中加载的Graph对象。
.config参数允许用户指定配置Session对象所需的选项,如限制CPU或GPU的使用数目,为数据流图设置优化参数及日志选项等。
session.run()接收一个参数fetches,以及其他三个可选参数:feed_dict、options和run_metadata。
利用占位节点添加输入:tf.placeholder Op可创建占位符。
# 创建一个长度为2,数据类型为int32的占位向量
a = tf.placeholder(tf.int32, shape=[2], name="my_input")
b = tf.reduce_prod(a, name="prod_b")
c = tf.reduce_sum(a, name="sum_c")
# 完成数据留图的定义
d = tf.add(b, c, name="add_d")
Variable对象:Tensor对象和Op对象都是不可变的,但机器学习任务的本质决定了需要一种机制保存随事件变化的值。借助TensorFlow中的Variable对象,便可达到这个目的。Variable对象包含了在对Session.run()多次调用中可持久的可变张量值。
通过名称作用域组织数据流图:现实世界中的模型往往会包含几十或上百个节点,以及数以百万计的参数。为使这种级别的复杂性可控,TensorFlow当前提供了一种帮助用户组织数据流图的机制---名称作用域(name scope)
综合实例:
import tensorflow as tf
tf.merge_all_summaries = tf.summary.merge_all
tf.train.SummaryWriter = tf.summary.FileWriter
graph = tf.Graph()
with graph.as_default():
with tf.name_scope("variables"):
# 用于追踪模型的运行次数
global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step")
# 追踪该模型的所有输出随时间的累加和
total_output= tf.Variable(0.0, dtype=tf.float32, trainable=False, name="total_output")
with tf.name_scope("transformation"):
# 独立的输入层
with tf.name_scope("input"):
a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")
# 独立的中间层
with tf.name_scope("intermediate_layer"):
b = tf.reduce_prod(a, name="product_b")
c = tf.reduce_sum(a, name="sum_c")
# 独立的输出层
with tf.name_scope("output"):
output = tf.add(b, c, name="output")
with tf.name_scope("update"):
# 用最新的输出更新
update_total = total_output.assign_add(output)
# global_step增1
increment_step = global_step.assign_add(1)
with tf.name_scope("summaries"):
avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")
# 为输出节点创建汇总数据
tf.summary.scalar(b'Output', output)
tf.summary.scalar(b'Sum of outputs over time', update_total)
tf.summary.scalar(b'Average of outputs over time', avg)
with tf.name_scope("global_ops"):
init = tf.initialize_all_variables()
merged_summaries = tf.merge_all_summaries()
sess = tf.Session(graph=graph)
writer = tf.summary.FileWriter("./improved_graph", graph)
sess.run(init)
def run_graph(input_tensor):
feed_dict = {a: input_tensor}
_, step, summary = sess.run([output, increment_step, merged_summaries], feed_dict=feed_dict)
writer.add_summary(summary, global_step=step)
run_graph([2,8])
run_graph([3,1,3,3])
run_graph([8])
run_graph([1,2,3])
run_graph([11,4])
run_graph([4,1])
run_graph([7,3,1])
run_graph([6,3])
run_graph([0,2])
run_graph([4,5,6])
writer.flush()
writer.close()
sess.close()