Extract nonzero coordinates
You need the locations of entries satisfying a mask in a tensor.
TensorFlow · Function
Returns nonzero-element coordinates, or selects element values from x and y using condition as a mask. The function supports broadcasting across the condition and both branches.
tf.where has two forms. With only condition, it returns the row-major coordinates of nonzero elements as an int64 index tensor. With condition, x, and y, it broadcasts their shapes and selects from x where the condition is nonzero, otherwise from y.
tf.where(condition, x=None, y=None, name=None)| Name | Type | Description |
|---|---|---|
condition | Tensor | A boolean or numeric tensor used either to locate nonzero entries or to choose between branches. |
x | Tensor, optional | The values selected where condition is nonzero; must be provided together with y for selection mode. |
y | Tensor, optional | The values selected where condition is zero; must be provided together with x for selection mode. |
name | string, optional | An optional name for the operation. |
With only condition, a 2-D int64 tensor of nonzero-element coordinates. With x and y, a tensor whose shape is the broadcasted shape of condition, x, and y, containing values selected from the two branches.
int64 indices, with one coordinate per row.import tensorflow as tf
condition = tf.constant([[True, False], [False, True]])
values = tf.where(condition, tf.ones_like(condition, dtype=tf.int32), tf.zeros_like(condition, dtype=tf.int32))
print(values.numpy())
print(tf.where(condition).numpy())Selects one integer tensor element where the mask is true and the other where it is false, then prints the true-element coordinates.
You need the locations of entries satisfying a mask in a tensor.
You need elementwise conditional values without Python branching.
Your condition and branch tensors have compatible but different shapes.
tf.where(condition) return?With only condition, tf.where returns a two-dimensional int64 tensor containing the coordinates of its nonzero elements. Its shape is [number of nonzero elements, rank of condition], and coordinates are emitted in row-major order.
Follow-up: What happens for a scalar condition? The result still represents coordinates, with the second dimension determined by the scalar rank, so the coordinate tensor has zero-width coordinate rows when the scalar is nonzero.
tf.where select between two tensors?When condition, x, and y are all supplied, the condition acts as a mask. Each output element comes from x where the condition is nonzero and from y where it is zero.
Follow-up: How can differently shaped inputs work? Their shapes are broadcast together, so exact shape equality is unnecessary when the three shapes are mutually broadcastable.
The function accepts a boolean or numeric condition. With both branches present, the resulting shape is obtained by broadcasting condition, x, and y together.
Follow-up: What should be checked when a call fails? Verify that the three inputs have compatible broadcast shapes and that both x and y are supplied for selection mode.
tf.boolean_masktf.gather_ndtf.math.count_nonzerotf.ones_liketf.zeros_like