Skip to content

Instantly share code, notes, and snippets.

@phraniiac
Last active February 28, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phraniiac/337a791bb091154a8f6fc34990672120 to your computer and use it in GitHub Desktop.
Save phraniiac/337a791bb091154a8f6fc34990672120 to your computer and use it in GitHub Desktop.
import numpy as np
import tensorflow as tf
def add_values(num_list):
# making a list of tensors.
tensor_list = tf.constant(num_list, shape=[1, 3], name='tensor_list')
tensor_ten = tf.constant([10] * len(num_list), \
shape=[len(num_list), 1], name='tensor_ten')
# the below statement declares a scope under which the
# variables will be used.
with tf.name_scope('number_layer1'):
tensor_mean = tf.reduce_mean(tensor_list)
tensor_val = tf.matmul(tensor_list, tensor_ten)
tf.summary.scalar('tensor_mean', tensor_mean)
# the below layer is like a nested layer in the above one,
# and hence will also be the same way in the graph.
with tf.name_scope('number_layer2'):
result_tensor = tf.reduce_sum(tensor_val)
tf.summary.scalar('result_tensor', result_tensor)
return result_tensor
# initializing our graph
with tf.Graph().as_default():
init = tf.global_variables_initializer()
# file writer initialization.
writer = tf.summary.FileWriter('./tf_summary/1')
with tf.Session() as sess:
sess.run(init)
sess.run(add_values([10, 20, 30]))
writer.add_graph(sess.graph)
# this function saves from writing all summaries individually to
# the file writer.
merged = tf.summary.merge_all()
# calculate the summaries.
s = sess.run(merged)
# write them.
writer.add_summary(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment