Skip to content

Instantly share code, notes, and snippets.

@john-bradshaw
Created April 18, 2018 14:35
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 john-bradshaw/227663340d85261dabb912d3d90c5406 to your computer and use it in GitHub Desktop.
Save john-bradshaw/227663340d85261dabb912d3d90c5406 to your computer and use it in GitHub Desktop.
Demo on using control dependencies version 2.
import tensorflow as tf
def main1():
sess = tf.Session()
x = tf.Variable(1.)
sess.run(tf.initialize_variables([x]))
y = x + 3
assign_op = tf.assign(x, x+1)
w = tf.identity(x)
u = x + 0
with tf.control_dependencies([assign_op]):
z = tf.identity(x)
q = x + 0
print(sess.run([y,z,q,w, u]))
print(sess.run([y,z,q,w, u]))
print(sess.run([y,z,q,w, u]))
print(sess.run([y,z,q,w, u]))
def main2():
sess = tf.Session()
x = tf.Variable(1.)
sess.run(tf.initialize_variables([x]))
y = x + 3
with tf.control_dependencies([y]):
assign_op = tf.assign(x, x+1)
w = tf.identity(x)
u = x + 0
with tf.control_dependencies([assign_op]):
z = tf.identity(x)
q = x + 0
print(sess.run([y,z,q,w, u]))
print(sess.run([y,z,q,w, u]))
print(sess.run([y,z,q,w, u]))
print(sess.run([y,z,q,w, u]))
if __name__ == '__main__':
main1()
tf.reset_default_graph()
print('=')
main2()
# TF 1.4
# prints (although main1 appears to be stochastic!)
# [4.0, 2.0, 2.0, 2.0, 1.0]
# [6.0, 3.0, 3.0, 3.0, 3.0]
# [6.0, 4.0, 4.0, 4.0, 3.0]
# [8.0, 5.0, 5.0, 5.0, 5.0]
# =
# [4.0, 2.0, 2.0, 2.0, 1.0]
# [5.0, 3.0, 3.0, 3.0, 2.0]
# [6.0, 4.0, 4.0, 4.0, 3.0]
# [7.0, 5.0, 5.0, 5.0, 4.0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment