-
Notifications
You must be signed in to change notification settings - Fork 74k
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
Allow py_function to support functions that return RaggedTensor #26453
Comments
In #27679 (comment), I showed how py_function could be extended to handle composite tensor inputs and outputs (and other nested structures, like dicts, tuples, lists, etc). If you have bandwidth to work on a PR that adds that to TensorFlow (with tests etc.), then it would be very welcome; otherwise, you could just use the |
The issue should have been fixed by @edloper. The following example should work. import tensorflow
# TF1
tf = tensorflow.compat.v1
with tf.Graph().as_default():
elements = [[1., 2., 3.], [4., 5.]]
ragged1 = tf.ragged.constant(elements)
def py_func():
return tf.ragged.constant(elements)
ragged2 = tf.py_function(
py_func, [], Tout=tf.RaggedTensorSpec([2, None], tf.float32)
)
with tf.Session() as sess:
print(sess.run(ragged1)) # <tf.RaggedTensorValue [[1.0, 2.0, 3.0], [4.0, 5.0]]>
print(sess.run(ragged2)) # <tf.RaggedTensorValue [[1.0, 2.0, 3.0], [4.0, 5.0]]>
# TF2
tf = tensorflow.compat.v2
def py_func():
return tf.ragged.constant([[1., 2., 3.], [4., 5.]])
print(tf.py_function(py_func, [], Tout=tf.RaggedTensorSpec([2, None], tf.float32))) # <tf.RaggedTensor [[1.0, 2.0, 3.0], [4.0, 5.0]]> Note the main difference of this example with the example in the original issue is the See https://www.tensorflow.org/api_docs/python/tf/py_function for more information on |
Thanks for your brief tutorial, but the given code will not work, if I convert it into the lambda function. look forward to see the further improvement of tf.. |
@HuangChiEn Are you using TensorFlow 2.7? Support for using composite tensors (such as tmp = lambda _: tf.ragged.constant([[1., 2., 3.], [4., 5.]])
tf.py_function(tmp, [], Tout=tf.RaggedTensorSpec([2, None], tf.float32)) And it failed with " tmp = lambda: tf.ragged.constant([[1., 2., 3.], [4., 5.]])
tf.py_function(tmp, [], Tout=tf.RaggedTensorSpec([2, None], tf.float32)) Then it succeeds for me (in TF 2.7). |
System information
Describe the feature and the current behavior/state.
py_function
only supports functions that returnTensor
s. However the wrapped function is executed in eager mode and therefore ideally should support other return types consistent with eager mode. As of tf-1.13.1,py_function
attempts to convert the returned objects toTensor
s, but aRaggedTensor
cannot be converted directly to aTensor
. Any attempt to return aRaggedTensor
raises an exception during the attempted conversion. E.g.,I propose that
py_function
detect which output arguments, if any, areRaggedTensor
s and returns them without attempting to convert toTensor
s. If the proposal is rejected, I suggest that the documentation is updated to make clearer (either in the API or guides) that aRaggedTensor
is not a suitable return type for functions wrapped bypy_function
.Will this change the current api? How? No.
Who will benefit with this feature? Anyone who uses
RaggedTensor
in conjunction withpy_function
.Any Other info.
As a workaround, one can construct a
RaggedTensor
from the output ofpy_function
. E.g.,The text was updated successfully, but these errors were encountered: