Return the elements, either from `x` or `y`, depending on the `condition`. If both `x` and `y` are None, then this operation returns the coordinates of true elements of `condition`. The coordinates are returned in a 2-D tensor where the first dimension (rows) represents the number of true el
(condition, x=None, y=None, name=None)
| 3712 | "which has the same broadcast rule as np.where") |
| 3713 | @dispatch.add_dispatch_support |
| 3714 | def where(condition, x=None, y=None, name=None): |
| 3715 | """Return the elements, either from `x` or `y`, depending on the `condition`. |
| 3716 | |
| 3717 | If both `x` and `y` are None, then this operation returns the coordinates of |
| 3718 | true elements of `condition`. The coordinates are returned in a 2-D tensor |
| 3719 | where the first dimension (rows) represents the number of true elements, and |
| 3720 | the second dimension (columns) represents the coordinates of the true |
| 3721 | elements. Keep in mind, the shape of the output tensor can vary depending on |
| 3722 | how many true values there are in input. Indices are output in row-major |
| 3723 | order. |
| 3724 | |
| 3725 | If both non-None, `x` and `y` must have the same shape. |
| 3726 | The `condition` tensor must be a scalar if `x` and `y` are scalar. |
| 3727 | If `x` and `y` are tensors of higher rank, then `condition` must be either a |
| 3728 | vector with size matching the first dimension of `x`, or must have the same |
| 3729 | shape as `x`. |
| 3730 | |
| 3731 | The `condition` tensor acts as a mask that chooses, based on the value at each |
| 3732 | element, whether the corresponding element / row in the output should be taken |
| 3733 | from `x` (if true) or `y` (if false). |
| 3734 | |
| 3735 | If `condition` is a vector and `x` and `y` are higher rank matrices, then it |
| 3736 | chooses which row (outer dimension) to copy from `x` and `y`. If `condition` |
| 3737 | has the same shape as `x` and `y`, then it chooses which element to copy from |
| 3738 | `x` and `y`. |
| 3739 | |
| 3740 | Args: |
| 3741 | condition: A `Tensor` of type `bool` |
| 3742 | x: A Tensor which may have the same shape as `condition`. If `condition` is |
| 3743 | rank 1, `x` may have higher rank, but its first dimension must match the |
| 3744 | size of `condition`. |
| 3745 | y: A `tensor` with the same shape and type as `x`. |
| 3746 | name: A name of the operation (optional) |
| 3747 | |
| 3748 | Returns: |
| 3749 | A `Tensor` with the same type and shape as `x`, `y` if they are non-None. |
| 3750 | Otherwise, a `Tensor` with shape `(num_true, rank(condition))`. |
| 3751 | |
| 3752 | Raises: |
| 3753 | ValueError: When exactly one of `x` or `y` is non-None. |
| 3754 | """ |
| 3755 | if x is None and y is None: |
| 3756 | with ops.name_scope(name, "Where", [condition]) as name: |
| 3757 | condition = ops.convert_to_tensor( |
| 3758 | condition, preferred_dtype=dtypes.bool, name="condition") |
| 3759 | return gen_array_ops.where(condition=condition, name=name) |
| 3760 | elif x is not None and y is not None: |
| 3761 | return gen_math_ops.select(condition=condition, x=x, y=y, name=name) |
| 3762 | else: |
| 3763 | raise ValueError("x and y must both be non-None or both be None.") |
| 3764 | |
| 3765 | |
| 3766 | @tf_export("where", v1=["where_v2"]) |
no test coverage detected