Attributeerror: 'Tensor' Object Has No Attribute 'Numpy'

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

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

AttributeError: 'Tensor' object has no attribute 'numpy' while extending keras sequential model

The direct using of this numpy function is impossible - as it's neither implemented in Tensorflow nor in Theano. Moreover, there is no direct correspondence between tensors and arrays. Tensors should be understood as algebraic variables whereas numpy arrays as numbers. Tensor is an abstract thing and applying a numpy function to it is usually impossible.

But you could still try to re-implement your function on your own using keras.backend. Then you'll use the valid tensor operations and no problem would be raised.

Another way to tackle your problem would be to use tf.numpy_function, see the documentation, this allows you to use numpy functions but there are some limitations.

AttributeError: 'Tensor' object has no attribute 'numpy' while mapping a function through my dataset

I solved it! I didn't understand exactly where the error was, I think the previous code mixed eager mode and graph mode, so I changed the code of get_label function and it worked!

def get_lab(file_path):

parts = tf.strings.split(file_path, os.path.sep)[-1]
part=tf.strings.split(parts, sep='_')[0]
print(part)
label=tf.strings.to_number(part)
return label

AttributeError: 'Tensor' object has no attribute 'numpy' when using a Keras-based custom loss function

You have two choices:

a) Use Tensorflow ufuncs for your loss function, for instance not using .numpy() and replacing np.sum() by tf.reduce_sum()

b) Use NumPy ufuncs, but train eagerly, by passing run_eagerly=True in model.compile()

AttributeError: 'Tensor' object has no attribute 'numpy' in Tensorflow 2.1

The problem in your code is that you cannot use .numpy() inside functions that are mapped onto tf.data.Datasets, because .numpy() is Python code not pure TensorFlow code.

When you use a function like my_dataset.map(my_function), you can only use tf.* functions inside your my_function function.

This is not a bug of TensorFlow 2.x versions, but rather on how static graphs are generated behind the scenes for performance purposes.

If you want to use custom Python code inside a function which you map on your dataset, you have to use tf.py_function(), docs: https://www.tensorflow.org/api_docs/python/tf/py_function. There is really no other way to mix Python code and TensorFlow code when mapping on a dataset.

You can also consult this question for further information; it's the exact question that I asked a couple of months ago: Is there an alternative to tf.py_function() for custom Python code?



Related Topics



Leave a reply



Submit