Skip to content
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

Closed
Mainak431 opened this issue Apr 4, 2019 · 68 comments
Assignees
Labels
comp:core issues related to core part of tensorflow Fixed in Nightly Issues that are resolved in nightly version stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:support Support issues

Comments

@Mainak431
Copy link

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]],

       [[ 83,  25,  85],
        [ 90, 190, 143],
        [  4, 141,  49]],

       [[  0,   0,   0],
        [  4,  71,  49],
        [  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.

@lucasjinreal
Copy link

@Mainak431 Did you solved your problem?

@Mainak431
Copy link
Author

Yes.

@aagnone3
Copy link

aagnone3 commented May 7, 2019

what was your solution?

@Mainak431
Copy link
Author

.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.

@veonua
Copy link

veonua commented Jun 26, 2019

@Mainak431
module 'tensorflow' has no attribute 'eagerly'
module 'tensorflow.compat.v1' has no attribute 'eagerly'

@francoisruty
Copy link

I'm using the package tensorflow-gpu==2.0.0-alpha0
.numpy() on one of my tensors yield 'Tensor' object has no attribute 'numpy'

tf.eagerly() gives module 'tensorflow' has no attribute 'eagerly'

@blutjens
Copy link

tf.executing_eagerly() instead of tf.eagerly() worked for me to check if I'm in eager mode

@joshmyersdean
Copy link

Has anyone found a solution for this?

@bionicles
Copy link

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

@AkashNagaraj
Copy link

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.

@Efaq
Copy link

Efaq commented Sep 26, 2019

@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!

@xro7
Copy link

xro7 commented Oct 2, 2019

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.

@DeanLeeFumu
Copy link

DeanLeeFumu commented Oct 18, 2019

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

@malharjajoo
Copy link

Why isn't there a single solution mentioned in this thread =.=

@michael-lash
Copy link

My function does not have a decorator but .numpy() still fails as described by previous posters. Has anyone found an solution to this?

@michael-lash
Copy link

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)...

@renatomello
Copy link

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'.

@wevonosky
Copy link

wevonosky commented Dec 16, 2019

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'

@renatomello
Copy link

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:
print(y_true): Tensor("dense_target:0", shape=(None, None, None), dtype=float32)
print(type(y_true)): <class 'tensorflow.python.framework.ops.Tensor'>

y_pred:
print(y_pred): Tensor("dense/Identity:0", shape=(None, None, 6), dtype=float32)
print(type(pred)): <class 'tensorflow.python.framework.ops.Tensor'>

@Haakam21
Copy link

@renatomello I am having the same problem as you are. I have opened a new issue: #35393.

@sid027
Copy link

sid027 commented Jan 17, 2020

@renatomello the problem still persists when trying to implement a custom metric. Did you find a workaround?

@renatomello
Copy link

@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.

@harrisonjansma
Copy link

harrisonjansma commented Feb 5, 2020

I encountered this area when using the regex functions within data preprocessing. Using python logic requires the use of tf.py_function as mentioned in docs and this StackOverflow thread (Note tf.py_func() is now tf.py_function())

Once I changed my code from
data = fnames.map(process_path)
to
data = fnames.map(lambda x: tf.py_function(process_path, [x], [tf.string]))
the code executed correctly.

@jvishnuvardhan
Copy link
Contributor

@louisnot I cannot reproduce the error. Please check the gist here. Thanks!

If you are facing the error, can you please share a gist? Thanks!

@louisnot
Copy link

louisnot commented Oct 2, 2020

I did not face any problem using the gist.

@jvishnuvardhan jvishnuvardhan added the Fixed in Nightly Issues that are resolved in nightly version label Oct 2, 2020
@jvishnuvardhan
Copy link
Contributor

I am closing this issue as this was resolved in tf-nightly. Please feel free to reopen if I the issue persists. Thanks!

@jvishnuvardhan
Copy link
Contributor

@bhupendrathore Can you please open a new issue with a simple standalone code to reproduce the error? Thanks!

@liangzelang
Copy link

liangzelang commented Oct 22, 2020

I get the same problem,; when I fix it in your solution with pip install tf-nightly , the errors occurs:

ERROR: Could not find a version that satisfies the requirement tf-nightly (from versions: none)
ERROR: No matching distribution found for tf-nightly

@jvishnuvardhan

@jvishnuvardhan
Copy link
Contributor

@liangzelang i see you opened another new issue. we will resolve it there. Thanks

@ghost
Copy link

ghost commented Jan 16, 2021

fuck tf

tallamjr added a commit to tallamjr/astronet that referenced this issue Jan 20, 2021
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
@OnlyBelter
Copy link

Add tf.config.run_functions_eagerly(True) before run other code.

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

@kaelzhang
Copy link

Add tf.config.run_functions_eagerly(True) before run other code.

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

@OnlyBelter thanks a million! You save my day.

@billybatsaikhan
Copy link

I have tried all of the above methods, but they didn't work for me.
I solved this problem by simply restarting my Jupyter notebook.
It turned out that activating eager mode or trying different TensorFlow commands messed up my environment.

@GranMin
Copy link

GranMin commented Jul 16, 2021

tf.config.run_functions_eagerly(True)
I fixed my problem by explicitly specify eager execution.
my tf version is 2.3.0

@Nico-ba
Copy link

Nico-ba commented Jul 22, 2021

What fixed the problem for me was changing:

import keras
to
from tensorflow import keras

Don't know why but import keras changed tf.executing_eagerly() to false again.

@mainguyenanhvu
Copy link

@renatomello I encountered it when run this command model.tabnet.aggregate_feature_selection_mask.numpy(). In google colab, it worked well. However, on my local, it threw this error.
I ensured that the eager model is enabled. print(tf.executing_eagerly()) returns True.

Please help me. Thank you a lot.
notebook

@renatomello
Copy link

@renatomello I encountered it when run this command model.tabnet.aggregate_feature_selection_mask.numpy(). In google colab, it worked well. However, on my local, it threw this error.
I ensured that the eager model is enabled. print(tf.executing_eagerly()) returns True.

Please help me. Thank you a lot.
notebook

What worked for me at the time was updating my TF to the nightly version, where the problem was already fixed.

@mervess
Copy link

mervess commented Sep 2, 2021

What fixed it for me:
Use either tf.config.run_functions_eagerly(True), or compile the model with the flag run_eagerly=True.
Then, add if tf.executing_eagerly(): before calling the .numpy() function.

TF version: 2.5.0, I use tensorflow.keras.

@dpintof
Copy link

dpintof commented Sep 21, 2021

If you go the tf.compat.v1.enable_eager_execution() route don't forget to restart your Kernel in Spyder.

@aghajanyan
Copy link

aghajanyan commented Nov 17, 2021

I have tried all of the above methods, but they didn't work for me. I solved this problem by simply restarting my Jupyter notebook. It turned out that activating eager mode or trying different TensorFlow commands messed up my environment.

Holy... I just can't believe!! It worked with Spyder too. Thank you a lot! :D

@dragen1860
Copy link

simply give up tensorflow and hardly every error disappears. pytorch yes!

@CharlesScoville
Copy link

~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. ¯_(ツ)_/¯

@White-Charles
Copy link

White-Charles commented Nov 2, 2023

I met the same issue (TF=2.14.0, python=3.9.18, keras=2.14.0). I solved the problem in this way:
add functions which disabled some actions in TF2

import tensorflow as tf
tf.config.run_functions_eagerly(True)
tf.compat.v1.disable_v2_behavior()
tf.compat.v1.disable_eager_execution()
if tf.executing_eagerly():
        x.numpy()

I referred to the https://www.tensorflow.org/api_docs/python/tf/config/run_functions_eagerly
and https://github.com/onnx/keras-onnx/issues/651#issuecomment-872061539

then the model can be run successfully! Hope this help 😄

@ben-arnao
Copy link
Contributor

ben-arnao commented Dec 9, 2023

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.

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.

2023-12-08 22:00:36.120954: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at strided_slice_op.cc:108 : INVALID_ARGUMENT: slice index 1 of dimension 0 out of bounds.
Traceback (most recent call last):
  File "C:/Users/Ben/PycharmProjects/tradingbot/src/test.py", line 158, in <module>
    multiple_weights_model.predict(samples)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1743, in predict
    tmp_batch_outputs = self.predict_function(iterator)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1579, in predict_function
    return step_function(self, iterator)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1569, in step_function
    outputs = model.distribute_strategy.run(run_step, args=(data,))
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\distribute\distribute_lib.py", line 1316, in run
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\distribute\distribute_lib.py", line 2892, in call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\distribute\distribute_lib.py", line 3695, in _call_for_each_replica
    return fn(*args, **kwargs)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 601, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1562, in run_step
    outputs = model.predict_step(data)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1535, in predict_step
    return self(x, training=False)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1044, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 393, in call
    outputs = layer(inputs, **kwargs)
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1044, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "C:/Users/Ben/PycharmProjects/tradingbot/src/test.py", line 107, in call
    ts.append(tf.matmul([inputs[x]], self.w[:, s_ind:e_ind]) + self.b[s_ind:e_ind])
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\Ben\PycharmProjects\tradingbot\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 7107, in raise_from_not_ok_status
    raise core._status_to_exception(e) from None  # pylint: disable=protected-access
tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index 1 of dimension 0 out of bounds. [Op:StridedSlice] name: sequential/multi_weight/strided_slice/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:core issues related to core part of tensorflow Fixed in Nightly Issues that are resolved in nightly version stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:support Support issues
Projects
None yet
Development

No branches or pull requests