Tensorflow: TF 2.0 'Tensor' object has no attribute 'numpy' while using .numpy() although eager execution enabled by default

Created on 4 Apr 2019  ·  54Comments  ·  Source: tensorflow/tensorflow

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.

Fixed in Nightly TF 2.0 core awaiting response support

Most helpful comment

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.

All 54 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
module 'tensorflow' has no attribute 'eagerly'
module 'tensorflow.compat.v1' has no attribute 'eagerly'

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'

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? https://github.com/tensorflow/tensorflow/issues/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.

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

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

y_pred:
print(y_pred): Tensor("dense/Identity:0", shape=(None, None, 6), dtype=float32)
print(type(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?

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

I ran into this issue while trying out the TextVectorization layer in TF2.1 with a very custom split. What fixed it for me was to pass in dynamic=True to the TextVectorizationconstruction.

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

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

y_pred:
print(y_pred): Tensor("dense/Identity:0", shape=(None, None, 6), dtype=float32)
print(type(pred)):

Don't know the reason, but I solved such issue by adding
experimental_run_tf_function=False
in compile function of my model.

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

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)):
y_pred:
print(y_pred): Tensor("dense/Identity:0", shape=(None, None, 6), dtype=float32)
print(type(pred)):

Don't know the reason, but I solved such issue by adding
experimental_run_tf_function=False
in compile function of my model.

It does not seem to make a difference in my case

I ran into this issue while trying out the TextVectorization layer in TF2.1 with a very custom split. What fixed it for me was to pass in dynamic=True to the TextVectorizationconstruction.

How did you do that exactly?

first, you should register Session as follow:
sess=tf.Session()
then..use :
expected_values = expt.eval(session=sess)
the result should be:
print(expected_values)
Out[41]:
array([[[ 0, 0, 0],
[ 4, 71, 141],
[ 0, 0, 0]]])
thank

I ran into this issue while trying out the TextVectorization layer in TF2.1 with a very custom split. What fixed it for me was to pass in dynamic=True to the TextVectorizationconstruction.

How did you do that exactly?

Like this. I'm still learning ML/TF in general, so this may have been the wrong thing to do. It's all black magic until it 'just works'.

vectorize_layer = tf.keras.layers.experimental.preprocessing.TextVectorization(
    standardize=tf_custom_standardize,
    split=tf_custom_split,
    max_tokens=len(vocab)+1,
    output_mode='int',
    dynamic=True
)

This problem still persists for me. I am not using a the @tf.function annotation and executing eagerly. The workaround I am using now is np.array(yourtensor.to_list())

This problem still persists for me. I am not using a the @tf.function annotation and executing eagerly. The workaround I am using now is np.array(yourtensor.to_list())

This is what I get
AttributeError: 'Tensor' object has no attribute 'to_list'

According to the codes in tensorflow, when the tensor object is in eager mode tensor(tensorflow.python.framework.ops.EagerTensor), we could call tesnsorObj.numpy()
this method is implemented in tensorflow/python/framework/ops.py
But if the tensor object is a general tensor, e.g. belonging to tensorflow.python.framework.ops.Tensor, then it'll have no numpy() method, the implementation codes is also in tensorflow/python/framework/ops.py
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/ops.py

It means all tensor object running in @tf.function couldn't call tensorObj.numpy()
The eager running mode adds the flexibility to TF2.0 and also adds complexity to it.

According to the codes in tensorflow, when the tensor object is in eager mode tensor(tensorflow.python.framework.ops.EagerTensor), we could call tesnsorObj.numpy()
this method is implemented in tensorflow/python/framework/ops.py
But if the tensor object is a general tensor, e.g. belonging to tensorflow.python.framework.ops.Tensor, then it'll have no numpy() method, the implementation codes is also in tensorflow/python/framework/ops.py
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/ops.py

It means all tensor object running in @tf.function couldn't call tensorObj.numpy()
The eager running mode adds the flexibility to TF2.0 and also adds complexity to it.

Thanks for the detailed explanation.

However, this issue is not solved in the stable 2.1.0 version. It was pointed out to me in the #38038 that it is solved in the tf-nightly 2.2.0 version.

@renatomello I'm a complete newbie on TF. So, I'm not sure if the following will help to solve your issue.
I solve the error AttributeError: 'Tensor' object has no attribute 'numpy', by use of experimental_run_functions_eagerly(True).
I use TF ver. 2.1 (For the detailed version information see the below console output). I allow to explain what I'm doing with this code:

import sys
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np

counter=0

class MyDense(layers.Dense):
  def __init__(self, units, activation,input_shape):
    super().__init__(units=units, activation=activation,input_shape=input_shape)

  def call(self, inputs):
      global counter

      counter += 1
      print('\n{}. inputs.numpy()='.format(counter))
      if hasattr(inputs,'numpy'):
          print('{}'.format(inputs.numpy()))
      else:
          print('not available.')

      return super().call(inputs)

def test(run_eagerly):
    print('\n*** *** *** test(run_eagerly={})'.format(run_eagerly))
    tf.config.experimental_run_functions_eagerly(run_eagerly)

    dim0=256
    dim1=24
    train = np.arange(dim0*dim1).reshape(dim0,dim1)
    label = np.ones(dim0)

    model = tf.keras.Sequential()
    model.add(MyDense(10,activation='softmax',input_shape=(dim1,)))

    model.compile(optimizer=tf.keras.optimizers.SGD(),
                  loss=tf.keras.losses.SparseCategoricalCrossentropy())

    model.fit(train,
              label,
              batch_size=dim0,
              epochs=1)

print("Python version")
print (sys.version)
print("TensorFlow version")
print(tf.__version__)
print('\n\n')

test(False)
test(True)

I execute the above code in the editor Spyder’s console (Spyder Version 4.0.1). The corresponding console output is (just parts of the output):

Python version
3.7.6 (default, Jan  8 2020, 19:59:22) 
[GCC 7.3.0]
TensorFlow version
2.1.0

*** *** *** test(run_eagerly=False)

1. inputs.numpy()=
not available.
Train on 256 samples

2. inputs.numpy()=
not available.

3. inputs.numpy()=
not available.
256/256 [==============================] - 0s 899us/sample - loss: 6344.8682

*** *** *** test(run_eagerly=True)

4. inputs.numpy()=
not available.
Train on 256 samples

5. inputs.numpy()=
[[2328. 2329. 2330. ... 2349. 2350. 2351.]
 [5280. 5281. 5282. ... 5301. 5302. 5303.]
 [2208. 2209. 2210. ... 2229. 2230. 2231.]
 ...
 [5160. 5161. 5162. ... 5181. 5182. 5183.]
 [ 840.  841.  842. ...  861.  862.  863.]
 [6048. 6049. 6050. ... 6069. 6070. 6071.]]
256/256 [==============================] - 0s 132us/sample - loss: 16.1181

Now, see in the console output below test(run_eagerly=False): All three MyDense.call() calls state, that input.numpy() is not available (Side note: When I understand it correctly, the first two MyDense.call() calls are just for building the model. So, it’s meaningful, that they don’t have input.numpy() available.). So, this is meaningful.
See in the console output below test(run_eagerly=True): In the fifth MyDense.call() is input.numpy() available. So, this is also meaningful. And it solves the error.

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.

It works for me.

I've tried all solutions mentioned in this thread, and none of them worked for me. I'm on 2.2.0-rc2.

It doesn't seem to be an issue with eager execution in my case. The error comes from these lines when I called keras.backend.get_value():

  if context.executing_eagerly() or isinstance(x, ops.EagerTensor):
    return x.numpy()

Had the same issue. It turns out that by default, eagerly execution is disabled when running the fit function on a model. To counter this, you can run : model.run_eagerly = True before running model.fit.
This worked like a charm for me on Tensorflow 2.2

model.compile(..., run_eagerly=True) worked for me when creating a custom metric

I had similar problem while working with Keras.
I don't know if this will help other , but following helped me:
I was using:
model = Sequential() before and changed that to
model = tf.keras.Sequential()
And it worked for me.

@Mainak431 would you mind reopening the issue?

I had similar problem while working with Keras.
I don't know if this will help other , but following helped me:
I was using:
model = Sequential() before and changed that to
model = tf.keras.Sequential()
And it worked for me.

That was actually the solution for me, I reused some old code which imported directly from keras, not tensorflow.keras layers etc. changing the import statements resolved the attribute error.

@Blubbaa Same here. It was only thing that worked for me.

If I want to check the value in tensor but I have to disable eager mode because of tf.placeholder compatibility, how should I do? I've tried to add tf.enable_eager_execution between the layer without tf.placeholder only to get this error ValueError: tf.enable_eager_execution must be called at program startup.

Following code worked:

def parse_str(str_tensor):
    raw_string = str_tensor.numpy().decode("utf-8") 

    # play with raw string
    raw_string = 'AAA'+raw_string     
    return raw_string

Call parse function:

def tf_pre_processing(row):
  return tf.py_function(parse_str, [row['context']], [tf.string])


train = t.map(tf_pre_processing).batch(1).take(1)

list(train)

@Mainak431 The original code was working as expected. Here is the gist.

@tu1258 You don't need to disable eager execution. Just import it as tf.compat.v1.placeholder instead of tf.placeholder. If it still doesn't work, then please share a standalone code to reproduce the issue.

Please let me know if there are any more questions. 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.

I think following option in Tensorflow2.x can help
tf.config.run_functions_eagerly

The place you are calling tf.function, you can try to execute it eagerly and then disable eager execution for tf.function.

I'm new to tensorflow but following this tutorial (on Jupyter Notebook) https://www.tensorflow.org/tutorials/quickstart/beginner?hl=en will give you the error

predictions = model(x_train[:1]).numpy()
Tensor object has no attribute 'numpy'

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

I did not face any problem using the gist.

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

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

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

Was this page helpful?
0 / 5 - 0 ratings