tf.experimental.dispatch_for_binary_elementwise_assert_apis
Stay organized with collections
Save and categorize content based on your preferences.
Decorator to override default implementation for binary elementwise assert APIs.
tf.experimental.dispatch_for_binary_elementwise_assert_apis(
x_type, y_type
)
The decorated function (known as the "elementwise assert handler")
overrides the default implementation for any binary elementwise assert API
whenever the value for the first two arguments (typically named x
and y
)
match the specified type annotations. The handler is called with two
arguments:
elementwise_assert_handler(assert_func, x, y)
Where x
and y
are the first two arguments to the binary elementwise assert
operation, and assert_func
is a TensorFlow function that takes two
parameters and performs the elementwise assert operation (e.g.,
tf.debugging.assert_equal
).
The following example shows how this decorator can be used to update all
binary elementwise assert operations to handle a MaskedTensor
type:
class MaskedTensor(tf.experimental.ExtensionType):
values: tf.Tensor
mask: tf.Tensor
@dispatch_for_binary_elementwise_assert_apis(MaskedTensor, MaskedTensor)
def binary_elementwise_assert_api_handler(assert_func, x, y):
merged_mask = tf.logical_and(x.mask, y.mask)
selected_x_values = tf.boolean_mask(x.values, merged_mask)
selected_y_values = tf.boolean_mask(y.values, merged_mask)
assert_func(selected_x_values, selected_y_values)
a = MaskedTensor([1, 1, 0, 1, 1], [False, False, True, True, True])
b = MaskedTensor([2, 2, 0, 2, 2], [True, True, True, False, False])
tf.debugging.assert_equal(a, b) # assert passed; no exception was thrown
a = MaskedTensor([1, 1, 1, 1, 1], [True, True, True, True, True])
b = MaskedTensor([0, 0, 0, 0, 2], [True, True, True, True, True])
tf.debugging.assert_greater(a, b)
Traceback (most recent call last):
InvalidArgumentError: Condition x > y did not hold.
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 assert 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_assert_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#L936-L1002) |\n\nDecorator to override default implementation for binary elementwise assert 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_assert_apis`](https://www.tensorflow.org/api_docs/python/tf/experimental/dispatch_for_binary_elementwise_assert_apis)\n\n\u003cbr /\u003e\n\n tf.experimental.dispatch_for_binary_elementwise_assert_apis(\n x_type, y_type\n )\n\nThe decorated function (known as the \"elementwise assert handler\")\noverrides the default implementation for any binary elementwise assert API\nwhenever the value for the first two arguments (typically named `x` and `y`)\nmatch the specified type annotations. The handler is called with two\narguments:\n\n`elementwise_assert_handler(assert_func, x, y)`\n\nWhere `x` and `y` are the first two arguments to the binary elementwise assert\noperation, and `assert_func` is a TensorFlow function that takes two\nparameters and performs the elementwise assert operation (e.g.,\n[`tf.debugging.assert_equal`](../../tf/debugging/assert_equal)).\n\nThe following example shows how this decorator can be used to update all\nbinary elementwise assert 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_assert_apis(MaskedTensor, MaskedTensor)\n def binary_elementwise_assert_api_handler(assert_func, x, y):\n merged_mask = tf.logical_and(x.mask, y.mask)\n selected_x_values = tf.boolean_mask(x.values, merged_mask)\n selected_y_values = tf.boolean_mask(y.values, merged_mask)\n assert_func(selected_x_values, selected_y_values)\n a = MaskedTensor([1, 1, 0, 1, 1], [False, False, True, True, True])\n b = MaskedTensor([2, 2, 0, 2, 2], [True, True, True, False, False])\n tf.debugging.assert_equal(a, b) # assert passed; no exception was thrown\n\n a = MaskedTensor([1, 1, 1, 1, 1], [True, True, True, True, True])\n b = MaskedTensor([0, 0, 0, 0, 2], [True, True, True, True, True])\n tf.debugging.assert_greater(a, b)\n Traceback (most recent call last):\n\n InvalidArgumentError: Condition x \u003e y did not hold.\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 assert APIs are:\n\n\\\u003c\\\u003e"]]