Returns the specified piece of this RaggedTensor. Supports multidimensional indexing and slicing, with one restriction: indexing into a ragged inner dimension is not allowed. This case is problematic because the indicated value may exist in some rows but not others. In such cases, it's no
(self, key)
| 29 | |
| 30 | |
| 31 | def ragged_tensor_getitem(self, key): |
| 32 | """Returns the specified piece of this RaggedTensor. |
| 33 | |
| 34 | Supports multidimensional indexing and slicing, with one restriction: |
| 35 | indexing into a ragged inner dimension is not allowed. This case is |
| 36 | problematic because the indicated value may exist in some rows but not |
| 37 | others. In such cases, it's not obvious whether we should (1) report an |
| 38 | IndexError; (2) use a default value; or (3) skip that value and return a |
| 39 | tensor with fewer rows than we started with. Following the guiding |
| 40 | principles of Python ("In the face of ambiguity, refuse the temptation to |
| 41 | guess"), we simply disallow this operation. |
| 42 | |
| 43 | Any dimensions added by `array_ops.newaxis` will be ragged if the following |
| 44 | dimension is ragged. |
| 45 | |
| 46 | Args: |
| 47 | self: The RaggedTensor to slice. |
| 48 | key: Indicates which piece of the RaggedTensor to return, using standard |
| 49 | Python semantics (e.g., negative values index from the end). `key` |
| 50 | may have any of the following types: |
| 51 | |
| 52 | * `int` constant |
| 53 | * Scalar integer `Tensor` |
| 54 | * `slice` containing integer constants and/or scalar integer |
| 55 | `Tensor`s |
| 56 | * `Ellipsis` |
| 57 | * `tf.newaxis` |
| 58 | * `tuple` containing any of the above (for multidimentional indexing) |
| 59 | |
| 60 | Returns: |
| 61 | A `Tensor` or `RaggedTensor` object. Values that include at least one |
| 62 | ragged dimension are returned as `RaggedTensor`. Values that include no |
| 63 | ragged dimensions are returned as `Tensor`. See above for examples of |
| 64 | expressions that return `Tensor`s vs `RaggedTensor`s. |
| 65 | |
| 66 | Raises: |
| 67 | ValueError: If `key` is out of bounds. |
| 68 | ValueError: If `key` is not supported. |
| 69 | TypeError: If the indices in `key` have an unsupported type. |
| 70 | |
| 71 | Examples: |
| 72 | |
| 73 | ```python |
| 74 | >>> # A 2-D ragged tensor with 1 ragged dimension. |
| 75 | >>> rt = ragged.constant([['a', 'b', 'c'], ['d', 'e'], ['f'], ['g']]) |
| 76 | >>> rt[0].eval().tolist() # First row (1-D `Tensor`) |
| 77 | ['a', 'b', 'c'] |
| 78 | >>> rt[:3].eval().tolist() # First three rows (2-D RaggedTensor) |
| 79 | [['a', 'b', 'c'], ['d', 'e'], '[f'], [g']] |
| 80 | >>> rt[3, 0].eval().tolist() # 1st element of 4th row (scalar) |
| 81 | 'g' |
| 82 | |
| 83 | >>> # A 3-D ragged tensor with 2 ragged dimensions. |
| 84 | >>> rt = ragged.constant([[[1, 2, 3], [4]], |
| 85 | ... [[5], [], [6]], |
| 86 | ... [[7]], |
| 87 | ... [[8, 9], [10]]]) |
| 88 | >>> rt[1].eval().tolist() # Second row (2-D RaggedTensor) |
nothing calls this directly
no test coverage detected