Reputation: 3
import tensorflow as tf
a=tf.random_normal([3, 2], mean=6, stddev=0.1, seed=1)
b=tf.random_normal([3, 2], mean=1, stddev=1, seed=1)
sess=tf.Session()
ra=sess.run(a)
rb=sess.run(b)
r1=ra-rb
r2=sess.run(tf.subtract(a,b))
Why is r1
and r2
not equal?
Shouldn't it be the same in theory?
tensorflow version : 1.15.0
Upvotes: 0
Views: 152
Reputation:
In Tensorflow 1.x
since in each session
the tf.random_normal
generates the new set of numbers which is the reason for change in results as rightly mentioned by @xdurch0 and @Addy in the comment section.
Instead, you can set the constant numbers using tf.constant
and compare the results.
Tensorflow 1.x:
import tensorflow as tf
a = tf.constant([[5.918868 , 6.14846 ],
[6.006533 , 5.7557297],
[6.009925 , 6.0591226]])
b = tf.constant([[0.32409406, 1.2866583 ],
[1.3215888 , 2.2124639 ],
[0.19414288, 0.86650544]])
sess=tf.Session()
ra=sess.run(a)
rb=sess.run(b)
r1=ra -rb
r2=sess.run(tf.subtract(a,b))
print(r1)
print(r2)
Result:
[[5.5947742 4.8618016]
[4.684944 3.5432658]
[5.815782 5.192617 ]]
[[5.5947742 4.8618016]
[4.684944 3.5432658]
[5.815782 5.192617 ]]
Tensorflow 2.x:
In Tensorflow 2.x
since eager execution is enabled by default the tf.random.normal
will execute immediately and keep the result for rest of the code.
import tensorflow as tf
a=tf.random.normal([3, 2], mean=6, stddev=0.1, seed=1)
b=tf.random.normal([3, 2], mean=1, stddev=1, seed=1)
r1=a-b
r2=tf.subtract(a,b)
print(r1)
print(r2)
Result:
tf.Tensor(
[[5.5947742 4.8618016]
[4.684944 3.5432658]
[5.815782 5.192617 ]], shape=(3, 2), dtype=float32)
tf.Tensor(
[[5.5947742 4.8618016]
[4.684944 3.5432658]
[5.815782 5.192617 ]], shape=(3, 2), dtype=float32)
Upvotes: 1