AttributeError: 'Tensor' object has no attribute 'numpy'
Asked Answered
Z

9

104

I downloaded this code from GitHub.

predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].numpy()

But I get an error that says:

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

What is wrong, and how do I fix it?

Zounds answered 16/9, 2018 at 19:9 Comment(0)
H
102

I suspect the place where you copied the code from had eager execution enabled, i.e. had invoked tf.enable_eager_execution() at the start of the program.

You could do the same.

UPDATE: Note that eager execution is enabled by default in TensorFlow 2.0. So the answer above applies only to TensorFlow 1.x

Hello answered 16/9, 2018 at 23:55 Comment(1)
It changes the TensirFlie APIs so that they execute operations on tensors immediately (as opposed to adding the operations to a graph). See the links in the answer above for detailsHello
P
103

Since the accepted answer did not solve the problem for me so I thought it might be helpful for some people who face the problem and that already have tensorflow version >= 2.2.0 and eager execution enabled.

The issue seems to be that for certain functions during the fitting model.fit() the @tf.function decorator prohibits the execution of functions like tensor.numpy() for performance reasons.

The solution for me was to pass the flag run_eagerly=True to the model.compile() like this:

model.compile(..., run_eagerly=True)
Phonotypy answered 26/8, 2020 at 10:50 Comment(0)
H
102

I suspect the place where you copied the code from had eager execution enabled, i.e. had invoked tf.enable_eager_execution() at the start of the program.

You could do the same.

UPDATE: Note that eager execution is enabled by default in TensorFlow 2.0. So the answer above applies only to TensorFlow 1.x

Hello answered 16/9, 2018 at 23:55 Comment(1)
It changes the TensirFlie APIs so that they execute operations on tensors immediately (as opposed to adding the operations to a graph). See the links in the answer above for detailsHello
E
34

Tensorflow 2 has a config option to run functions "eagerly" which will enable getting Tensor values via .numpy() method. To enable eager execution, use following command:

tf.config.run_functions_eagerly(True)

Note that this is useful mainly for debugging.

See also: https://www.tensorflow.org/api_docs/python/tf/config/run_functions_eagerly

Enough answered 28/10, 2020 at 13:32 Comment(1)
For the record, this answer is the only thing that gave me a change in error output. I'm getting this error, but I'm on TF 2.0, not 1.x, so I think that's why most other answers are not helpful for me.Meaningless
T
24

This can also happen in TF2.0 if your code is wrapped in a @tf.function or inside a Keras layer. Both of those run in graph mode. There's a lot of secretly broken code out of there because behavior differs between eager and graph modes and people are not aware that they're switching contexts, so be careful!

Trowel answered 28/7, 2020 at 15:24 Comment(1)
P
10

It happens in older version of TF. So try pip install tensorflow --upgrade

otherwise run

import tensorflow as tf
tf.enable_eager_execution()

If you are using Jupyter notebook, restart the Kernel.

Platoon answered 27/2, 2020 at 10:31 Comment(0)
I
2

You can also use tf.get_static_value() to obtain the value of a tensor. This has the benefit of not needing eager mode. See docs here.

Isotope answered 6/11, 2022 at 23:35 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewGrateful
C
1

tf.multinomial returns a Tensor object that contains a 2D list with drawn samples of shape [batch_size, num_samples]. Calling .eval() on that tensor object is expected to return a numpy ndarray.

Something like this:

predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval()

You also need to ensure that you have a session active (doesn't make a lot of sense otherwise):

sess = tf.Session()
with sess.as_default():
    predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval()
Cognation answered 16/9, 2018 at 19:22 Comment(0)
F
1

I had the same issue in a tf.function(): But what has worked for me is to transform the numpy array into a tensorflow tensor via tf.convert_to_tensor Doku and then go ahead with tensorflow. Maybe this trick could be useful for anyone...

Foote answered 23/1, 2021 at 12:21 Comment(0)
P
0

I saw similar error when I run code something like the following,

tensor = tf.multiply(ndarray, 42)
tensor.numpy()  # throw AttributeError: 'Tensor' object has no attribute 'numpy'

I use anaconda 3 with tensorflow 1.14.0. I upgraded tensorflow with the command below

conda update tensorflow

now tensorflow is 2.0.0, issue fixed. Try this to see if it resolves your issue.

Plugboard answered 25/10, 2019 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.