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?
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?
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
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)
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
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
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!
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.
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.
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()
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...
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.
© 2022 - 2024 — McMap. All rights reserved.