Repeats each range of `params` (as specified by `splits`) `repeats` times. Let the `i`th range of `params` be defined as `params[splits[i]:splits[i + 1]]`. Then this function returns a tensor containing range 0 repeated `repeats[0]` times, followed by range 1 repeated `repeats[1]`, ..., fo
(params, splits, repeats)
| 68 | |
| 69 | |
| 70 | def repeat_ranges(params, splits, repeats): |
| 71 | """Repeats each range of `params` (as specified by `splits`) `repeats` times. |
| 72 | |
| 73 | Let the `i`th range of `params` be defined as |
| 74 | `params[splits[i]:splits[i + 1]]`. Then this function returns a tensor |
| 75 | containing range 0 repeated `repeats[0]` times, followed by range 1 repeated |
| 76 | `repeats[1]`, ..., followed by the last range repeated `repeats[-1]` times. |
| 77 | |
| 78 | Args: |
| 79 | params: The `Tensor` whose values should be repeated. |
| 80 | splits: A splits tensor indicating the ranges of `params` that should be |
| 81 | repeated. |
| 82 | repeats: The number of times each range should be repeated. Supports |
| 83 | broadcasting from a scalar value. |
| 84 | |
| 85 | Returns: |
| 86 | A `Tensor` with the same rank and type as `params`. |
| 87 | |
| 88 | #### Example: |
| 89 | ```python |
| 90 | >>> repeat_ranges(['a', 'b', 'c'], [0, 2, 3], 3) |
| 91 | ['a', 'b', 'a', 'b', 'a', 'b', 'c', 'c', 'c'] |
| 92 | ``` |
| 93 | """ |
| 94 | # Divide `splits` into starts and limits, and repeat them `repeats` times. |
| 95 | if repeats.shape.ndims != 0: |
| 96 | repeated_starts = repeat(splits[:-1], repeats, axis=0) |
| 97 | repeated_limits = repeat(splits[1:], repeats, axis=0) |
| 98 | else: |
| 99 | # Optimization: we can just call repeat once, and then slice the result. |
| 100 | repeated_splits = repeat(splits, repeats, axis=0) |
| 101 | n_splits = array_ops.shape(repeated_splits, out_type=repeats.dtype)[0] |
| 102 | repeated_starts = repeated_splits[:n_splits - repeats] |
| 103 | repeated_limits = repeated_splits[repeats:] |
| 104 | |
| 105 | # Get indices for each range from starts to limits, and use those to gather |
| 106 | # the values in the desired repetition pattern. |
| 107 | one = array_ops.ones((), repeated_starts.dtype) |
| 108 | offsets = gen_ragged_math_ops.ragged_range( |
| 109 | repeated_starts, repeated_limits, one) |
| 110 | return array_ops.gather(params, offsets.rt_dense_values) |