0

Returning a tensor value with command

    a, b, c, d, e = tf.unstack(x, axis=1)

then when I try to display the value of a all I get is:

<tf.Tensor 'unstack_4:0' shape=(5,) dtype=int32>

How can I see which are the values of this tensor?

I'm pretty new to TensorFlow.

James Arten
  • 523
  • 5
  • 16
  • Does this answer your question? [How to print the value of a Tensor object in TensorFlow?](https://stackoverflow.com/questions/33633370/how-to-print-the-value-of-a-tensor-object-in-tensorflow) – evantkchong Nov 29 '21 at 22:20

1 Answers1

1

In TensorFlow 2, you can use tf.backend.eval() to evaluate a tensor that is outside of a tf.function if it hasn't yet been evaluated (more on that in the next section).

a, b, c, d, e = tf.unstack(x, axis=1)
print(tf.keras.backend.eval(a))

How is it that is not possible to just type a and see your tensor value?

You likely not have Eager Execution enabled. The significance of this delves a bit into the inner workings of TensorFlow.

In TensorFlow there are two modes of execution, Eager Execution and Graph Execution.

Eager Execution

In Eager Execution, operations like tf.unstack are evaluated immediately and return concrete values. This is the usual behavior you would expect from python and is the default mode of execution in TensorFlow 2 outside of tf.function and functions like tf.compat.v1.disable_eager_execution().

In Eager Execution, passing a into the interpreter would indeed return a concrete value.

>>> import tensorflow as tf
>>> # Create a 5x5 matrix
>>> x = tf.constant([[i for i in range(5)] for i in range(5)])
# A bunch of messages regarding compute devices appear as TensorFlow initializes its backend to evaluate the expression immediately
>>> a, b, c, d, e = tf.unstack(x, axis=1)
>>> a
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 0, 0, 0, 0], dtype=int32)>

Graph Execution

With Graph Execution, every time you input an operation like tf.add(a, b) or tf.unstack, these operations are added to a TensorFlow graph rather than being immediately executed. By building the graph first and deferring execution, Tensorflow can improve the performance of the computations by optimizing the built graph.

Because of this, with a statement like a, b, c, d, e = tf.unstack(x, axis=1), a prints the TensorFlow operation rather than an output value.

>>> import tensorflow as tf
>>> # Here we disable eager execution
>>> tf.compat.v1.disable_eager_execution()
>>> # Create a 5x5 matrix as before. Notice the lack of messages
>>> x = tf.constant([[i for i in range(5)] for i in range(5)])
>>> a, b, c, d, e = tf.unstack(x, axis=1)
>>> a
<tf.Tensor 'unstack:0' shape=(5,) dtype=int32>

>>> # Evaluate the graph and store it in result
>>> result = tf.keras.backend.eval(a)
>>> result
array([0, 0, 0, 0, 0], dtype=int32)
>>> # `a` still refers to an operation if you were wondering
>>> a
<tf.Tensor 'unstack:0' shape=(5,) dtype=int32>
evantkchong
  • 2,251
  • 3
  • 14
  • 29
  • this is what I get by tf.print(a) : so I still cannot see my tensor value. How is it that is not possible to just type 'a' and see your tensor value? – James Arten Nov 30 '21 at 12:57
  • I should have spotted that your tensors aren't `EagerTensors`. `tf.print()` will only print the values if you have eager execution enabled. I will update the answer accordingly. – evantkchong Dec 01 '21 at 02:21