Chia Yi
Chia Yi

Reputation: 562

Computations (such as tf.greater and tf.cond) on random value tensors not working as expected

I am a tensorflow beginner. According to the documentation, tf.greater returns the truth value of (x>y) element-wise

My code is as below:

x = tf.random_uniform([])  # Empty array as shape creates a scalar.
y = tf.random_uniform([])
print('x: '+str(x.eval()))
print('y: ' +str(y.eval()))
out = tf.cond(tf.greater(x, y), lambda: x + y, lambda: x - y)
print(sess.run(tf.greater(x, y)))
print(sess.run(out))

The output I got is:

x: 0.79379404
y: 0.30891895
False
0.3438499

x is bigger than y so it should return True and x+y should be 1.10271299 why is my expected output different than the actual output?

Upvotes: 1

Views: 2542

Answers (2)

E_net4
E_net4

Reputation: 30052

There is nothing wrong with tf.greater or tf.cond. But since the two have been executed in different session runs, the values sampled from x and y will be different in each one. This can be noticed by fetching x and y alongside the output:

x = tf.random_uniform([])
y = tf.random_uniform([])
g = tf.greater(x, y)      # you can also do `x > y`, same TF op
c = tf.cond(g, lambda: x + y, lambda: x - y)
sess = tf.Session()
sess.run([x, y, g])
sess.run([x, y, c])

The output:

[0.11019015, 0.028247476, True]
[0.29905212, 0.9846852, -0.68563306]

In order to ensure that these operations use the same value, you can perform both operations in the same run:

sess.run([x, y, g, c])
[0.74283457, 0.8982569, False, -0.15542233]

Or turn x and y into TensorFlow variables, which would retain the same numbers from the distribution until the next reassignment.

x = tf.get_variable('x', shape=[], initializer=tf.random_uniform_initializer())
y = tf.get_variable('y', shape=[], initializer=tf.random_uniform_initializer())

g = x > y
c = tf.cond(g, lambda: x + y, lambda: x - y)

reset_vars = tf.variables_initializer([x, y])

sess = tf.Session()
sess.run(reset_vars) # this must be called once
sess.run([x, y, g])
sess.run([x, y, c])
[0.4862318, 0.48253357, True]
[0.4862318, 0.48253357, 0.9687654]

Upvotes: 0

syltruong
syltruong

Reputation: 2723

The tricky part with tensorflow is that the code is used to build a graph of computations, which can then be ran/evaluated in a tf.Session.

In your example, at each evaluation, random tensors x and y are generated.

  • So, your call x.eval() generates random tensors x and y.

  • Your call y.eval() regenerates random tensors x and y.

  • And so do sess.run(tf.greater(x, y)) and sess.run(out).

Therefore, your prints of x and y do not reflect the actual x and y used in sess.run(tf.greater(x, y)) and sess.run(out) (which by the way also explains why your result 0.3438499 do not correspond to neither x+y or x-y).

By freezing tensors x and y to tf.constant, you get the expected behaviour:

import numpy as np
import tensorflow as tf

with tf.Session() as sess:
   x = tf.constant(np.random.uniform())
   y = tf.constant(np.random.uniform())
   print('x: '+str(x.eval()))
   print('y: '+str(y.eval()))
   out = tf.cond(tf.greater(x, y), lambda: x + y, lambda: x - y)
   print(sess.run(tf.greater(x, y)))
   print(sess.run(out))

prints (on one run on my side)

x: 0.75513345
y: 0.04605962
True
0.80119306

Upvotes: 2

Related Questions