tf.where
Stay organized with collections
Save and categorize content based on your preferences.
Return the elements where condition
is True
(multiplexing x
and y
).
tf.where(
condition, x=None, y=None, name=None
)
This operator has two modes: in one mode both x
and y
are provided, in
another mode neither are provided. condition
is always expected to be a
tf.Tensor
of type bool
.
Retrieving indices of True
elements
If x
and y
are not provided (both are None):
tf.where
will return the indices of condition
that are True
, in
the form of a 2-D tensor with shape (n, d).
(Where n is the number of matching indices in condition
,
and d is the number of dimensions in condition
).
Indices are output in row-major order.
tf.where([True, False, False, True])
<tf.Tensor: shape=(2, 1), dtype=int64, numpy=
array([[0],
[3]])>
tf.where([[True, False], [False, True]])
<tf.Tensor: shape=(2, 2), dtype=int64, numpy=
array([[0, 0],
[1, 1]])>
tf.where([[[True, False], [False, True], [True, True]]])
<tf.Tensor: shape=(4, 3), dtype=int64, numpy=
array([[0, 0, 0],
[0, 1, 1],
[0, 2, 0],
[0, 2, 1]])>
Multiplexing between x
and y
If x
and y
are provided (both have non-None values):
tf.where
will choose an output shape from the shapes of condition
, x
,
and y
that all three shapes are
broadcastable to.
The condition
tensor acts as a mask that chooses whether the corresponding
element / row in the output should be taken from x
(if the element in condition is True) or
y` (if it is false).
tf.where([True, False, False, True], [1,2,3,4], [100,200,300,400])
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([ 1, 200, 300, 4],
dtype=int32)>
tf.where([True, False, False, True], [1,2,3,4], [100])
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([ 1, 100, 100, 4],
dtype=int32)>
tf.where([True, False, False, True], [1,2,3,4], 100)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([ 1, 100, 100, 4],
dtype=int32)>
tf.where([True, False, False, True], 1, 100)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([ 1, 100, 100, 1],
dtype=int32)>
tf.where(True, [1,2,3,4], 100)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4],
dtype=int32)>
tf.where(False, [1,2,3,4], 100)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([100, 100, 100, 100],
dtype=int32)>
Args |
condition
|
A tf.Tensor of type bool
|
x
|
If provided, a Tensor which is of the same type as y , and has a shape
broadcastable with condition and y .
|
y
|
If provided, a Tensor which is of the same type as y , and has a shape
broadcastable with condition and x .
|
name
|
A name of the operation (optional).
|
Returns |
If x and y are provided:
A Tensor with the same type as x and y , and shape that
is broadcast from condition , x , and y .
Otherwise, a Tensor with shape (num_true, dim_size(condition)) .
|
Raises |
ValueError
|
When exactly one of x or y is non-None, or the shapes
are not all broadcastable.
|
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.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.where\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/where) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/ops/array_ops.py#L4368-L4463) |\n\nReturn the elements where `condition` is `True` (multiplexing `x` and `y`).\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.where_v2`](/api_docs/python/tf/where)\n\n\u003cbr /\u003e\n\n tf.where(\n condition, x=None, y=None, name=None\n )\n\nThis operator has two modes: in one mode both `x` and `y` are provided, in\nanother mode neither are provided. `condition` is always expected to be a\n[`tf.Tensor`](../tf/Tensor) of type `bool`.\n\n#### Retrieving indices of `True` elements\n\nIf `x` and `y` are not provided (both are None):\n\n[`tf.where`](../tf/where) will return the indices of `condition` that are `True`, in\nthe form of a 2-D tensor with shape (n, d).\n(Where n is the number of matching indices in `condition`,\nand d is the number of dimensions in `condition`).\n\nIndices are output in row-major order. \n\n tf.where([True, False, False, True])\n \u003ctf.Tensor: shape=(2, 1), dtype=int64, numpy=\n array([[0],\n [3]])\u003e\n\n tf.where([[True, False], [False, True]])\n \u003ctf.Tensor: shape=(2, 2), dtype=int64, numpy=\n array([[0, 0],\n [1, 1]])\u003e\n\n tf.where([[[True, False], [False, True], [True, True]]])\n \u003ctf.Tensor: shape=(4, 3), dtype=int64, numpy=\n array([[0, 0, 0],\n [0, 1, 1],\n [0, 2, 0],\n [0, 2, 1]])\u003e\n\n#### Multiplexing between `x` and `y`\n\nIf `x` and `y` are provided (both have non-None values):\n\n[`tf.where`](../tf/where) will choose an output shape from the shapes of `condition`, `x`,\nand `y` that all three shapes are\n[broadcastable](https://docs.scipy.org/doc/numpy/reference/ufuncs.html) to.\n\nThe `condition` tensor acts as a mask that chooses whether the corresponding\nelement / row in the output should be taken from `x`\n(if the element in `condition is True) or`y\\` (if it is false). \n\n tf.where([True, False, False, True], [1,2,3,4], [100,200,300,400])\n \u003ctf.Tensor: shape=(4,), dtype=int32, numpy=array([ 1, 200, 300, 4],\n dtype=int32)\u003e\n tf.where([True, False, False, True], [1,2,3,4], [100])\n \u003ctf.Tensor: shape=(4,), dtype=int32, numpy=array([ 1, 100, 100, 4],\n dtype=int32)\u003e\n tf.where([True, False, False, True], [1,2,3,4], 100)\n \u003ctf.Tensor: shape=(4,), dtype=int32, numpy=array([ 1, 100, 100, 4],\n dtype=int32)\u003e\n tf.where([True, False, False, True], 1, 100)\n \u003ctf.Tensor: shape=(4,), dtype=int32, numpy=array([ 1, 100, 100, 1],\n dtype=int32)\u003e\n\n tf.where(True, [1,2,3,4], 100)\n \u003ctf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4],\n dtype=int32)\u003e\n tf.where(False, [1,2,3,4], 100)\n \u003ctf.Tensor: shape=(4,), dtype=int32, numpy=array([100, 100, 100, 100],\n dtype=int32)\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|-----------------------------------------------------------------------------------------------------------------|\n| `condition` | A [`tf.Tensor`](../tf/Tensor) of type `bool` |\n| `x` | If provided, a Tensor which is of the same type as `y`, and has a shape broadcastable with `condition` and `y`. |\n| `y` | If provided, a Tensor which is of the same type as `y`, and has a shape broadcastable with `condition` and `x`. |\n| `name` | A name of the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| If `x` and `y` are provided: A `Tensor` with the same type as `x` and `y`, and shape that is broadcast from `condition`, `x`, and `y`. Otherwise, a `Tensor` with shape `(num_true, dim_size(condition))`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|--------------------------------------------------------------------------------------|\n| `ValueError` | When exactly one of `x` or `y` is non-None, or the shapes are not all broadcastable. |\n\n\u003cbr /\u003e"]]