Skip to content

Instantly share code, notes, and snippets.

@diogoaurelio
Last active May 31, 2017 19:19
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 diogoaurelio/f25eb666176bed1b2eb6927ce2e460c1 to your computer and use it in GitHub Desktop.
Save diogoaurelio/f25eb666176bed1b2eb6927ce2e460c1 to your computer and use it in GitHub Desktop.
import tensorflow as tf
x = tf.Variable(3, name="x", dtype=tf.int16)
y = tf.Variable(4, name="y", dtype=tf.int16)
a = tf.constant(5, dtype=tf.int16)
b = tf.constant(10, dtype=tf.int16)
c = tf.constant(15, dtype=tf.int16)
op1 = x*x*y
op2 = a*b*c
f = op1 + op2
with tf.Session() as ses:
ses.run(x.initializer)
ses.run(y.initializer)
result = ses.run(op1)
print("Result from computation of graph op1 is: {}".format(result))
# Result from computation of graph op1 is: 36
# here is a better/more concise way:
with tf.Session() as ses:
x.initializer.run()
y.initializer.run()
result = op2.eval()
print("Result from computation of graph op2 is: {}".format(result))
# Result from computation of graph op2 is: 750
# even better/more concise way - instead of running initializer
# for every single variable, use global_variables_initializer() function:
init = tf.global_variables_initializer()
# Note: on older tensorflow versions the function was called: initialize_all_variables()
with tf.Session() as ses:
init.run()
r1 = op1.eval()
r2 = op2.eval()
result = f.eval()
print("Result from computation of graph f is: {}".format(result))
# Result from computation of graph f is: 786
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment