'Issue with Tensorflow tensor and eager execution

I would like to convert a Tensorflow tensor into a numpy array. My code looks as follows:

t = tf.gather_nd(angle, [1,1]) # extract row 1, column 1 element of angle tensor

t = t.numpy() # convert tensor t to numpy array

which results in:

AttributeError: 'Tensor' object has no attribute 'numpy'

I've tried different things that are proposed here, like trying out different Tensorflow versions (2.0,2.2,2.7) and triggering eager execution with tf.compat.v1.enable_eager_execution() or tf.enable_eager_execution() but the error message remains. I have used Python 3.7 on all Tensorflow versions.

Has anyone faced a similar issue? Im running out of ideas on this one.



Solution 1:[1]

What does the angle parameter consist of? You can try again by upgrading your existing TensorFlow in your system which will upgrade the numpy version also.

!pip install --upgrade tensorflow

It seems there is no problem with tf.gather_nd() api.

Suppose,

angle = np.array([['a', 'b'],['c', 'd']])

t = tf.gather_nd(angle, [1,1]) # extract row 1, column 1 element of angle tensor


print(t)
type(t)

Output:

tf.Tensor(b'd', shape=(), dtype=string)
tensorflow.python.framework.ops.EagerTensor

After converting this tensor to numpy:

t = t.numpy()
print(t)
type(t)

Output:

b'd'
bytes

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1