Created
April 18, 2018 14:20
-
-
Save john-bradshaw/4480573cf3086de95d881a92b2ef0b09 to your computer and use it in GitHub Desktop.
Tensorflow Control Dependencies. Resouce Variables versus normal variables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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