Last active
June 9, 2017 23:26
-
-
Save nottombrown/5b3f566387e64b7d6929c50f37d7c87b to your computer and use it in GitHub Desktop.
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
# 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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs the following: