I'm not sure if this is a Tensorflow bug or my misunderstanding about what this function is supposed to do, but I can't get tf.py_function
to return an EagerTensor
while in graph mode. Consequently, calling .numpy()
on the output of this function fails.
The issue can be reproduced using the exact example given in the official documentation (https://www.tensorflow.org/api_docs/python/tf/py_function):
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
def log_huber(x, m):
if tf.abs(x) <= m:
return x**2
else:
return m**2 * (1 - 2 * tf.math.log(m) + tf.math.log(x**2))
x = tf.constant(1.0)
m = tf.constant(2.0)
with tf.GradientTape() as t:
t.watch([x, m])
y = tf.py_function(func=log_huber, inp=[x, m], Tout=tf.float32)
dy_dx = t.gradient(y, x)
assert dy_dx.numpy() == 2.0
This generates the following error:
Traceback (most recent call last):
File "<input>", line 17, in <module>
File "C:\Users\...\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 446, in __getattr__
self.__getattribute__(name)
AttributeError: 'Tensor' object has no attribute 'numpy'
About version
I am running Python 3.8 and Tensorflow v2.9.1.
Any help would be greatly appreciated!