Given a tensor t, this operation returns a tensor of the same type and
shape as t with its values clipped to clip_value_min and clip_value_max.
Any values less than clip_value_min are set to clip_value_min. Any values
greater than clip_value_max are set to clip_value_max.
For example:
Basic usage passes a scalar as the min and max value.
Broadcasting fails, intentionally, if you would expand the dimensions of t
t=tf.constant([[-1,0.,10.],[-1,0,10]])clip_min=[[[2,1]]]# Has a third axist4=tf.clip_by_value(t,clip_value_min=clip_min,clip_value_max=100)Traceback(mostrecentcalllast):InvalidArgumentError:Incompatibleshapes:[2,3]vs.[1,1,2]
It throws a TypeError if you try to clip an int to a float value
(tf.cast the input to float first).
[[["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 2023-10-06 UTC."],[],[],null,["# tf.clip_by_value\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/clip_ops.py#L32-L123) |\n\nClips tensor values to a specified min and max.\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.clip_by_value`](https://www.tensorflow.org/api_docs/python/tf/clip_by_value)\n\n\u003cbr /\u003e\n\n tf.clip_by_value(\n t, clip_value_min, clip_value_max, name=None\n )\n\nGiven a tensor `t`, this operation returns a tensor of the same type and\nshape as `t` with its values clipped to `clip_value_min` and `clip_value_max`.\nAny values less than `clip_value_min` are set to `clip_value_min`. Any values\ngreater than `clip_value_max` are set to `clip_value_max`.\n| **Note:** `clip_value_min` needs to be smaller or equal to `clip_value_max` for correct results.\n\n#### For example:\n\nBasic usage passes a scalar as the min and max value. \n\n t = tf.constant([[-10., -1., 0.], [0., 2., 10.]])\n t2 = tf.clip_by_value(t, clip_value_min=-1, clip_value_max=1)\n t2.numpy()\n array([[-1., -1., 0.],\n [ 0., 1., 1.]], dtype=float32)\n\nThe min and max can be the same size as `t`, or broadcastable to that size. \n\n t = tf.constant([[-1, 0., 10.], [-1, 0, 10]])\n clip_min = [[2],[1]]\n t3 = tf.clip_by_value(t, clip_value_min=clip_min, clip_value_max=100)\n t3.numpy()\n array([[ 2., 2., 10.],\n [ 1., 1., 10.]], dtype=float32)\n\nBroadcasting fails, intentionally, if you would expand the dimensions of `t` \n\n t = tf.constant([[-1, 0., 10.], [-1, 0, 10]])\n clip_min = [[[2, 1]]] # Has a third axis\n t4 = tf.clip_by_value(t, clip_value_min=clip_min, clip_value_max=100)\n Traceback (most recent call last):\n\n InvalidArgumentError: Incompatible shapes: [2,3] vs. [1,1,2]\n\nIt throws a `TypeError` if you try to clip an `int` to a `float` value\n([`tf.cast`](../tf/cast) the input to `float` first). \n\n t = tf.constant([[1, 2], [3, 4]], dtype=tf.int32)\n t5 = tf.clip_by_value(t, clip_value_min=-3.1, clip_value_max=3.1)\n Traceback (most recent call last):\n\n TypeError: Cannot convert ...\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------|---------------------------------------------------------------------------------------------------|\n| `t` | A `Tensor` or `IndexedSlices`. |\n| `clip_value_min` | The minimum value to clip to. A scalar `Tensor` or one that is broadcastable to the shape of `t`. |\n| `clip_value_max` | The maximum value to clip to. A scalar `Tensor` or one that is broadcastable to the shape of `t`. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A clipped `Tensor` or `IndexedSlices`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|-----------------------------------------------------------------------------------------------------|\n| [`tf.errors.InvalidArgumentError`](../tf/errors/InvalidArgumentError): If the clip tensors would trigger array broadcasting that would make the returned tensor larger than the input. ||\n| `TypeError` | If dtype of the input is `int32` and dtype of the `clip_value_min` or `clip_value_max` is `float32` |\n\n\u003cbr /\u003e"]]