tf.experimental.dispatch_for_binary_elementwise_apis
Stay organized with collections
Save and categorize content based on your preferences.
Decorator to override default implementation for binary elementwise APIs.
tf.experimental.dispatch_for_binary_elementwise_apis(
x_type, y_type
)
Used in the notebooks
The decorated function (known as the "elementwise api handler") overrides
the default implementation for any binary elementwise API whenever the value
for the first two arguments (typically named x
and y
) match the specified
type annotations. The elementwise api handler is called with two arguments:
elementwise_api_handler(api_func, x, y)
Where x
and y
are the first two arguments to the elementwise api, and
api_func
is a TensorFlow function that takes two parameters and performs the
elementwise operation (e.g., tf.add
).
The following example shows how this decorator can be used to update all
binary elementwise operations to handle a MaskedTensor
type:
class MaskedTensor(tf.experimental.ExtensionType):
values: tf.Tensor
mask: tf.Tensor
@dispatch_for_binary_elementwise_apis(MaskedTensor, MaskedTensor)
def binary_elementwise_api_handler(api_func, x, y):
return MaskedTensor(api_func(x.values, y.values), x.mask & y.mask)
a = MaskedTensor([1, 2, 3, 4, 5], [True, True, True, True, False])
b = MaskedTensor([2, 4, 6, 8, 0], [True, True, True, False, True])
c = tf.add(a, b)
print(f"values={c.values.numpy()}, mask={c.mask.numpy()}")
values=[ 3 6 9 12 5], mask=[ True True True False False]
Args |
x_type
|
A type annotation indicating when the api handler should be called.
|
y_type
|
A type annotation indicating when the api handler should be called.
|
Registered APIs
The binary elementwise APIs are:
<>
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.experimental.dispatch_for_binary_elementwise_apis\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/util/dispatch.py#L878-L933) |\n\nDecorator to override default implementation for binary elementwise APIs.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.experimental.dispatch_for_binary_elementwise_apis`](https://www.tensorflow.org/api_docs/python/tf/experimental/dispatch_for_binary_elementwise_apis)\n\n\u003cbr /\u003e\n\n tf.experimental.dispatch_for_binary_elementwise_apis(\n x_type, y_type\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|----------------------------------------------------------------------|\n| - [Extension types](https://www.tensorflow.org/guide/extension_type) |\n\nThe decorated function (known as the \"elementwise api handler\") overrides\nthe default implementation for any binary elementwise API whenever the value\nfor the first two arguments (typically named `x` and `y`) match the specified\ntype annotations. The elementwise api handler is called with two arguments:\n\n`elementwise_api_handler(api_func, x, y)`\n\nWhere `x` and `y` are the first two arguments to the elementwise api, and\n`api_func` is a TensorFlow function that takes two parameters and performs the\nelementwise operation (e.g., [`tf.add`](../../tf/math/add)).\n\nThe following example shows how this decorator can be used to update all\nbinary elementwise operations to handle a `MaskedTensor` type: \n\n class MaskedTensor(tf.experimental.ExtensionType):\n values: tf.Tensor\n mask: tf.Tensor\n @dispatch_for_binary_elementwise_apis(MaskedTensor, MaskedTensor)\n def binary_elementwise_api_handler(api_func, x, y):\n return MaskedTensor(api_func(x.values, y.values), x.mask & y.mask)\n a = MaskedTensor([1, 2, 3, 4, 5], [True, True, True, True, False])\n b = MaskedTensor([2, 4, 6, 8, 0], [True, True, True, False, True])\n c = tf.add(a, b)\n print(f\"values={c.values.numpy()}, mask={c.mask.numpy()}\")\n values=[ 3 6 9 12 5], mask=[ True True True False False]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|---------------------------------------------------------------------|\n| `x_type` | A type annotation indicating when the api handler should be called. |\n| `y_type` | A type annotation indicating when the api handler should be called. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A decorator. ||\n\n\u003cbr /\u003e\n\n#### Registered APIs\n\nThe binary elementwise APIs are:\n\n\\\u003c\\\u003e"]]