When in TF V1 mode (that is, outside tf.function) Assert needs a control
dependency on the output to ensure the assertion executes:
# Ensure maximum element of x is smaller or equal to 1assert_op=tf.Assert(tf.less_equal(tf.reduce_max(x),1.),[x])withtf.control_dependencies([assert_op]):...codeusingx...
[[["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.debugging.Assert\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/control_flow_assert.py#L62-L130) |\n\nAsserts that the given condition is true.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.Assert`](https://www.tensorflow.org/api_docs/python/tf/debugging/Assert)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.Assert`](https://www.tensorflow.org/api_docs/python/tf/debugging/Assert), [`tf.compat.v1.debugging.Assert`](https://www.tensorflow.org/api_docs/python/tf/debugging/Assert)\n\n\u003cbr /\u003e\n\n tf.debugging.Assert(\n condition, data, summarize=None, name=None\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Client-efficient large-model federated learning via \\`federated_select\\` and sparse aggregation](https://www.tensorflow.org/federated/tutorials/sparse_federated_learning) |\n\nIf `condition` evaluates to false, print the list of tensors in `data`.\n`summarize` determines how many entries of the tensors to print.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|---------------------------------------------------|\n| `condition` | The condition to evaluate. |\n| `data` | The tensors to print out when condition is false. |\n| `summarize` | Print this many entries of each tensor. |\n| `name` | A name for this operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| `assert_op` | An `Operation` that, when executed, raises a [`tf.errors.InvalidArgumentError`](../../tf/errors/InvalidArgumentError) if `condition` is not true. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|---|---|\n\n\u003cbr /\u003e\n\n| **Note:** The output of this function should be used. If it is not, a warning will be logged or an error may be raised. To mark the output as used, call its .mark_used() method.\n\n\u003cbr /\u003e\n\nTF1 compatibility\n-----------------\n\n\u003cbr /\u003e\n\nWhen in TF V1 mode (that is, outside [`tf.function`](../../tf/function)) Assert needs a control\ndependency on the output to ensure the assertion executes: \n\n # Ensure maximum element of x is smaller or equal to 1\n assert_op = tf.Assert(tf.less_equal(tf.reduce_max(x), 1.), [x])\n with tf.control_dependencies([assert_op]):\n ... code using x ...\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\neager compatibility\n-------------------\n\n\u003cbr /\u003e\n\nreturns None\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e"]]