Builds flat_values tensor for a tiled `RaggedTensor`. Returns a tensor that repeats the values in `rt_input.flat_values` in the appropriate pattern to construct a `RaggedTensor` that tiles `rt_input` as specified by `multiples`. Args: rt_input: The `RaggedTensor` whose values should
(rt_input, multiples, const_multiples=None)
| 249 | |
| 250 | |
| 251 | def _tile_ragged_values(rt_input, multiples, const_multiples=None): |
| 252 | """Builds flat_values tensor for a tiled `RaggedTensor`. |
| 253 | |
| 254 | Returns a tensor that repeats the values in |
| 255 | `rt_input.flat_values` in the |
| 256 | appropriate pattern to construct a `RaggedTensor` that tiles `rt_input` as |
| 257 | specified by `multiples`. |
| 258 | |
| 259 | Args: |
| 260 | rt_input: The `RaggedTensor` whose values should be repeated. |
| 261 | multiples: A 1-D integer `tensor`, indicating how many times each dimension |
| 262 | should be repeated. |
| 263 | const_multiples: Optional constant value for multiples. Used to skip tiling |
| 264 | dimensions where `multiples=1`. |
| 265 | |
| 266 | Returns: |
| 267 | A `Tensor` with the same type and rank as `rt_input.flat_values`. |
| 268 | |
| 269 | #### Example: |
| 270 | ```python |
| 271 | >>> rt = tf.ragged.constant([[1, 2], [3]]) |
| 272 | >>> _tile_ragged_values(rt, [3, 2]) |
| 273 | [1, 2, 1, 2, 3, 3, 1, 2, 1, 2, 3, 3, 1, 2, 1, 2, 3, 3] |
| 274 | ``` |
| 275 | """ |
| 276 | ragged_rank = rt_input.ragged_rank |
| 277 | nested_splits = rt_input.nested_row_splits |
| 278 | |
| 279 | # Pointers to the values in `rt_input.flat_values`. |
| 280 | inner_value_ids = math_ops.range(nested_splits[-1][-1]) |
| 281 | |
| 282 | # For each ragged dimension (working from the innermost to outermost), |
| 283 | # expand `inner_value_ids` as necessary to tile that dimension. |
| 284 | prev_splits = None |
| 285 | for axis in range(ragged_rank, 0, -1): |
| 286 | # Ragged splits for this dimension. |
| 287 | splits = nested_splits[axis - 1] |
| 288 | |
| 289 | # Adjust splits so they point into `inner_value_ids` (instead of just |
| 290 | # pointing into the next dimension's values). |
| 291 | if prev_splits is not None: # Not the first pass through the loop. |
| 292 | splits = array_ops.gather(prev_splits * multiples[axis + 1], splits) |
| 293 | |
| 294 | # Repeat each element in this ragged dimension `multiples[axis]` times. |
| 295 | if const_multiples is None or const_multiples[axis] != 1: |
| 296 | inner_value_ids = ragged_util.repeat_ranges(inner_value_ids, splits, |
| 297 | multiples[axis]) |
| 298 | |
| 299 | prev_splits = splits |
| 300 | |
| 301 | # Gather the tiled inner values. |
| 302 | ragged_tiled_values = array_ops.gather(rt_input.flat_values, inner_value_ids) |
| 303 | |
| 304 | # Tile the flat_values for the uniform dimensions (i.e., for `axis=0` plus |
| 305 | # `axis=range(ragged_rank, rank)`). |
| 306 | inner_repeats = array_ops.concat([multiples[:1], multiples[ragged_rank + 1:]], |
| 307 | axis=0) |
| 308 | return array_ops.tile(ragged_tiled_values, inner_repeats) |