Searches input tensor for values on the innermost dimension. A 2-D example: ``` sorted_sequence = [[0, 3, 9, 9, 10], [1, 2, 3, 4, 5]] values = [[2, 4, 9], [0, 2, 6]] result = searchsorted(sorted_sequence, values, side="left") result == [[1
(sorted_sequence,
values,
side="left",
out_type=dtypes.int32,
name=None)
| 4558 | |
| 4559 | @tf_export("searchsorted") |
| 4560 | def searchsorted(sorted_sequence, |
| 4561 | values, |
| 4562 | side="left", |
| 4563 | out_type=dtypes.int32, |
| 4564 | name=None): |
| 4565 | """Searches input tensor for values on the innermost dimension. |
| 4566 | |
| 4567 | A 2-D example: |
| 4568 | |
| 4569 | ``` |
| 4570 | sorted_sequence = [[0, 3, 9, 9, 10], |
| 4571 | [1, 2, 3, 4, 5]] |
| 4572 | values = [[2, 4, 9], |
| 4573 | [0, 2, 6]] |
| 4574 | |
| 4575 | result = searchsorted(sorted_sequence, values, side="left") |
| 4576 | |
| 4577 | result == [[1, 2, 2], |
| 4578 | [0, 1, 5]] |
| 4579 | |
| 4580 | result = searchsorted(sorted_sequence, values, side="right") |
| 4581 | |
| 4582 | result == [[1, 2, 4], |
| 4583 | [0, 2, 5]] |
| 4584 | ``` |
| 4585 | |
| 4586 | Args: |
| 4587 | sorted_sequence: N-D `Tensor` containing a sorted sequence. |
| 4588 | values: N-D `Tensor` containing the search values. |
| 4589 | side: 'left' or 'right'; 'left' corresponds to lower_bound and 'right' to |
| 4590 | upper_bound. |
| 4591 | out_type: The output type (`int32` or `int64`). Default is `tf.int32`. |
| 4592 | name: Optional name for the operation. |
| 4593 | |
| 4594 | Returns: |
| 4595 | An N-D `Tensor` the size of values containing the result of applying either |
| 4596 | lower_bound or upper_bound (depending on side) to each value. The result |
| 4597 | is not a global index to the entire `Tensor`, but the index in the last |
| 4598 | dimension. |
| 4599 | |
| 4600 | Raises: |
| 4601 | ValueError: If the last dimension of `sorted_sequence >= 2^31-1` elements. |
| 4602 | If the total size of values exceeds `2^31 - 1` elements. |
| 4603 | If the first `N-1` dimensions of the two tensors don't match. |
| 4604 | """ |
| 4605 | sequence_size = shape_internal(sorted_sequence)[-1] |
| 4606 | values_size = shape_internal(values)[-1] |
| 4607 | sorted_sequence_2d = reshape(sorted_sequence, [-1, sequence_size]) |
| 4608 | values_2d = reshape(values, [-1, values_size]) |
| 4609 | if side == "right": |
| 4610 | output = gen_array_ops.upper_bound(sorted_sequence_2d, values_2d, out_type, |
| 4611 | name) |
| 4612 | elif side == "left": |
| 4613 | output = gen_array_ops.lower_bound(sorted_sequence_2d, values_2d, out_type, |
| 4614 | name) |
| 4615 | else: |
| 4616 | raise ValueError("side must be either 'right' or 'left'. Saw: %s." % side) |
| 4617 | return reshape(output, shape_internal(values)) |
nothing calls this directly
no test coverage detected