Skip to content

Instantly share code, notes, and snippets.

@john-bradshaw
Created April 18, 2018 14:20
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/4480573cf3086de95d881a92b2ef0b09 to your computer and use it in GitHub Desktop.
Save john-bradshaw/4480573cf3086de95d881a92b2ef0b09 to your computer and use it in GitHub Desktop.
Tensorflow Control Dependencies. Resouce Variables versus normal variables.
import numpy as np
import tensorflow as tf
def main1():
x = tf.get_variable("x", initializer=np.float64(0))
x_id = tf.identity(x)
x_rd = x.read_value()
x_p0 = x + 0
with tf.control_dependencies([x_id, x_rd, x_p0]):
y = tf.assign_add(x, 1)
tf_sess = tf.Session()
tf_sess.run(tf.global_variables_initializer())
print(tf_sess.run([y, x, x_id, x_rd, x_p0]))
def main2():
x = tf.get_variable("x", initializer=np.float64(0), use_resource=True)
x_id = tf.identity(x)
x_rd = x.read_value()
x_p0 = x + 0
with tf.control_dependencies([x_id, x_rd, x_p0]):
y = tf.assign_add(x, 1)
tf_sess = tf.Session()
tf_sess.run(tf.global_variables_initializer())
print(tf_sess.run([y, x, x_id, x_rd, x_p0]))
if __name__ == '__main__':
main1()
tf.reset_default_graph()
main2()
# prints:
# [1.0, 1.0, 1.0, 1.0, 0.0]
# [1.0, 1.0, 0.0, 0.0, 0.0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment