Skip to content

Instantly share code, notes, and snippets.

@nottombrown
Last active June 9, 2017 23:26
Show Gist options
  • Save nottombrown/5b3f566387e64b7d6929c50f37d7c87b to your computer and use it in GitHub Desktop.
Save nottombrown/5b3f566387e64b7d6929c50f37d7c87b to your computer and use it in GitHub Desktop.
# Based on https://github.com/tensorflow/tensorflow/issues/2652#issue-158487397
import tensorflow as tf
tf.reset_default_graph()
def run(on_gpu):
tf.reset_default_graph()
tf.set_random_seed(42) # Try to set up determinism
with tf.device('/gpu:0' if on_gpu else '/cpu:0'):
a = tf.random_normal([16, 16])
b = tf.get_variable('b', shape = [], initializer = tf.constant_initializer(value = 0.0))
c = a*b
grad = tf.gradients(c, [b], gate_gradients=tf.train.Optimizer.GATE_GRAPH)[0]
sess = tf.Session()
sess.run(tf.global_variables_initializer())
grad_val = sess.run(grad)
return grad_val
print("CPU (deterministic)")
for i in range(4):
print(repr(run(on_gpu=False)))
print("")
print("GPU (nondeterministic)")
for i in range(4):
print(repr(run(on_gpu=True)))
@nottombrown
Copy link
Author

Outputs the following:

CPU (deterministic)
23.066511
23.066511
23.066511
23.066511

GPU (nondeterministic)
23.066513
23.066511
23.066509
23.066513```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment