Skip to content

Instantly share code, notes, and snippets.

@dat-boris
Created January 4, 2020 20:11
Show Gist options
  • Save dat-boris/bf38eba706f812425ac79670e1000e02 to your computer and use it in GitHub Desktop.
Save dat-boris/bf38eba706f812425ac79670e1000e02 to your computer and use it in GitHub Desktop.
Tensorflow v2 compat v1 graph
"""Tutorial of example graph based on TFv1
We need to do a bunch of tfv1 summary, which is what is happening.
Graph output: https://snipboard.io/7tjoS2.jpg
Refactored from
https://towardsdatascience.com/understanding-fundamentals-of-tensorflow-program-and-why-it-is-necessary-94cf5b60e255
"""
import tensorflow as tf
tfv1 = tf.compat.v1
# Need to do this since tfv2 runs eager by default
tfv1.disable_eager_execution()
# setup placeholder using tf.placeholder
x = tfv1.placeholder(tf.int32, shape=[3], name='x')
'''it is of type integer and it has shape 3 meaning it is a 1D vector with 3 elements in it
we name it x. just create another placeholder y with same dimension. we treat the
placeholders like we treate constants. '''
y = tfv1.placeholder(tf.int32, shape=[3], name='y')
sum_x = tf.reduce_sum(x, name="sum_x")
prod_y = tf.reduce_prod(y, name="prod_y")
'''we dont know what values x and y holds till we run the graph'''
#final_div = tf.div(sum_x, prod_y)
final_div = sum_x / prod_y
# nwe give fetches and feed_dict pass into every session.run commandame="final_div")
final_mean = tf.reduce_mean([sum_x, prod_y], name="final_mean")
sess = tfv1.Session()
print ("sum(x): ", sess.run(sum_x, feed_dict={x: [100, 200, 300]}))
print ("prod(y): ", sess.run(prod_y, feed_dict={y: [1, 2, 3]}))
writer = tfv1.summary.FileWriter('./tensorflow_example', sess.graph)
writer.close()
sess.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment