New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TF 2.0 'Tensor' object has no attribute 'numpy' while using .numpy() although eager execution enabled by default #27519
Comments
@Mainak431 Did you solved your problem? |
Yes. |
what was your solution? |
.Numpy is only supported in eager mode. If you are in graph mode, it will not be supported. To check, if you are in eager mode. Do, tf.eagerly(). It returns true or false. In graph mode, you have to use eval in a session to get the value of the tensor in numpy array. |
@Mainak431 |
I'm using the package tensorflow-gpu==2.0.0-alpha0 tf.eagerly() gives module 'tensorflow' has no attribute 'eagerly' |
tf.executing_eagerly() instead of tf.eagerly() worked for me to check if I'm in eager mode |
Has anyone found a solution for this? |
same issue here ... i invoke a keras model in eager mode and i get a Tensor, not an EagerTensor, which causes issues with OpenAI Gym |
Run this tf.enable_eager_execution() and then when you try tf.executing_eagerly() it should give True. After this you can use something.numpy() to view the values. |
@AkashNagaraj I just filed an issue where the code is executing eagerly (and should, since it's TF 2.0), but I'm having a problem of "missing numpy". Would you care to take a look? #32842 Thanks! |
I had the same issue. Turned out that I was trying to use .numpy() inside a @tf.function. As far as I understand tf.function is not executed eagerly for performance purposes. If I remove the @tf.function decorator .numpy() works. |
This works for me, @tf.function turned the whole function into graph mode |
Why isn't there a single solution mentioned in this thread =.= |
My function does not have a decorator but .numpy() still fails as described by previous posters. Has anyone found an solution to this? |
I've found that my issue clears up after inserting tf.compat.v1.enable_eager_execution() at the top of my script (very similar to what previous posters have said, but this works for TF 2.0)... |
I'm having the same issue and none of the aforementioned solutions worked for me. I'm using TF 2.0, my function does not have a decorator, tf.eagerly() returns True and I still get the same AttributeError: 'Tensor' object has no attribute 'numpy'. |
Same problem. I made sure I was executing eagerly and do not have a decorator on my custom loss function. I also tried Michael's solution two comment up which didn't work. I get the error: AttributeError: 'Tensor' object has no attribute 'numpy' |
I noticed that this error only appears when I try to convert tensors to numpy during a model fit. My best guess is that it seems to be shape issue. For example, the following tensor <class 'tensorflow.python.framework.ops.EagerTensor'> tf.Tensor([[1 3] [0 4]], shape=(2, 2), dtype=int64) is convertible using .numpy(). However, when trying to implement a custom metric for a classification problem, both y_true.numpy() and y_pred.numpy() raise AttributeError: 'Tensor' object has no attribute 'numpy'. Here is one example of both y's: y_true: y_pred: |
@renatomello I am having the same problem as you are. I have opened a new issue: #35393. |
@renatomello the problem still persists when trying to implement a custom metric. Did you find a workaround? |
No, I did not. I'm trying to see if there's a TF/Keras backend function that does something similar that I can work with. Otherwise, I'll just have to create one myself. |
I encountered this area when using the regex functions within data preprocessing. Using python logic requires the use of Once I changed my code from |
I did not face any problem using the gist. |
I am closing this issue as this was resolved in |
@bhupendrathore Can you please open a new issue with a simple standalone code to reproduce the error? Thanks! |
I get the same problem,; when I fix it in your solution with
|
@liangzelang i see you opened another new issue. we will resolve it there. Thanks |
fuck tf |
Since experimental.numpy api seems to only be available from 2.4.x onwards, these changes allow for use of numpy operations to compute a custom metric. Note this was previously failing because of model.compile requiring 'run_eagerly=True' to be set. Error: TF 2.0 'Tensor' object has no attribute 'numpy' while using .numpy() although eager execution enabled by default TF 2.0 Custom Metric 'Tensor' object has no attribute 'numpy' Furthermore, a simple transition to tensorflow operations such as + # wtable = tf.reduce_sum(y_true, axis=0) / y_true.shape[0] did not work and would through errors relating to: Error: E ValueError: in user code: E raise ValueError("None values not supported.") E E ValueError: None values not supported. Refs: - tensorflow/tensorflow#35393 - tensorflow/tensorflow#27519 modified: astronet/metrics.py modified: astronet/t2/tests/int/test_train.py
Add see: https://www.tensorflow.org/api_docs/python/tf/config/run_functions_eagerly |
@OnlyBelter thanks a million! You save my day. |
I have tried all of the above methods, but they didn't work for me. |
tf.config.run_functions_eagerly(True) |
What fixed the problem for me was changing: import keras Don't know why but import keras changed tf.executing_eagerly() to false again. |
@renatomello I encountered it when run this command Please help me. Thank you a lot. |
What worked for me at the time was updating my TF to the nightly version, where the problem was already fixed. |
What fixed it for me: TF version: 2.5.0, I use tensorflow.keras. |
If you go the tf.compat.v1.enable_eager_execution() route don't forget to restart your Kernel in Spyder. |
Holy... I just can't believe!! It worked with Spyder too. Thank you a lot! :D |
simply give up tensorflow and hardly every error disappears. pytorch yes! |
~4 years later and this is still a problem; the above solutions do not resolve my issue. I am following the TensorFlow Lite example "generative_ai" readme to the letter. I confess, my issue may be caused by using a dated version of Python (3.8) which pulls dated versions of everything else I'm assuming. Still... I expect there to be a stable version that works for Python 3.8, and I feel I am justified in feeling that way. Maybe IDK how it actually works in the software world, but in the real world when I'm done doing a thing I clean up my f*ing mess before I move on to something else. ¯_(ツ)_/¯ |
I met the same issue (TF=2.14.0, python=3.9.18, keras=2.14.0). I solved the problem in this way:
I referred to the https://www.tensorflow.org/api_docs/python/tf/config/run_functions_eagerly then the model can be run successfully! Hope this help 😄 |
I don't have the decorator but I'd imagine this is the case for when TF does the initial tracing for custom code. For my issue run_functions_eagerly seems to be the cause as previously suggested but when I do this, it causes other parts of my code to fail that work when not forcing eager execution on functions. Ie.
|
Although Eager_execution is enabled by default in TF 2.0, I am getting errors while using .numpy()
Please note that i am not using the code in compatibility mode to TF 1.0.
expt = [[[ 0, 0, 0],
[ 4, 71, 141],
[ 0, 0, 0]],
expt = tf.convert_to_tensor(expt)
expected_values = expt.numpy()
AttributeError: 'Tensor' object has no attribute 'numpy'
CPU TEST VERSION OF TENSORFLOW 2.0.
The text was updated successfully, but these errors were encountered: