1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import tensorflow as tf
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) 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('Output', output) tf.summary.scalar('Sum of outputs over time', update_total) tf.summary.scalar('Average of outputs over time', avg)
with tf.name_scope("global_ops"): init = tf.initialize_all_variables() merged_summaries = tf.summary.merge_all()
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()
|