Tensor.name is meaningless in eager execution

Viewed 4071

I was doing some exercise in tensorflow in google colab and trying something under eager execution. When I was practicing on the tf.case by running the code below:

x = tf.random_normal([])
y = tf.random_normal([])

op = tf.case({tf.less(x,y):tf.add(x,y), tf.greater(x,y):tf.subtract(x,y)}, default = tf.multiply(x,y), exclusive = True)

I have followed the example in the tf.case carefully but it just keeps reporting an attribute error:

AttributeError: Tensor.name is meaningless when eager execution is enabled.

I am new to python and TF as well as deep learning. Can anyone try to run the code above and help me figure out?

Thank you

2 Answers

This seems like a bug in eager execution, which you should feel encouraged to report.

That said, using tf.case to express what it does only makes sense when constructing graphs. Enabling eager execution allows one to write easier to read, more idiomatic Python code. For the example you had, it would be something like this:

def case(x, y):
  if tf.less(x, y):
    return tf.add(x, y)
  if tf.greater(x, y:
    return tf.subtract(x, y)
  return tf.multiply(x, y)

Hope that helps. You may want to report this as a bug so that using tf.case when eager executing is enabled has the same effect as the code above.

I have found the answer. In the tf.case don't use a dictionary for the first argument. Instead, use a list that contains a series of tuples like below:

#under eager execution

op = tf.case([(tf.less(x,y),tf.add(x,y)),(tf.greater(x,y),tf.subtract(x,y))], default = tf.multiply(x,y), exclusive = True)
Related