Represents a ragged tensor. A `RaggedTensor` is a tensor with one or more *ragged dimensions*, which are dimensions whose slices may have different lengths. For example, the inner (column) dimension of `rt=[[3, 1, 4, 1], [], [5, 9, 2], [6], []]` is ragged, since the column slices (`rt[0, :
| 53 | |
| 54 | @tf_export("RaggedTensor") |
| 55 | class RaggedTensor(composite_tensor.CompositeTensor): |
| 56 | """Represents a ragged tensor. |
| 57 | |
| 58 | A `RaggedTensor` is a tensor with one or more *ragged dimensions*, which are |
| 59 | dimensions whose slices may have different lengths. For example, the inner |
| 60 | (column) dimension of `rt=[[3, 1, 4, 1], [], [5, 9, 2], [6], []]` is ragged, |
| 61 | since the column slices (`rt[0, :]`, ..., `rt[4, :]`) have different lengths. |
| 62 | Dimensions whose slices all have the same length are called *uniform |
| 63 | dimensions*. The outermost dimension of a `RaggedTensor` is always uniform, |
| 64 | since it consists of a single slice (and so there is no possibility for |
| 65 | differing slice lengths). |
| 66 | |
| 67 | The total number of dimensions in a `RaggedTensor` is called its *rank*, |
| 68 | and the number of ragged dimensions in a `RaggedTensor` is called its |
| 69 | *ragged-rank*. A `RaggedTensor`'s ragged-rank is fixed at graph creation |
| 70 | time: it can't depend on the runtime values of `Tensor`s, and can't vary |
| 71 | dynamically for different session runs. |
| 72 | |
| 73 | ### Potentially Ragged Tensors |
| 74 | |
| 75 | Many ops support both `Tensor`s and `RaggedTensor`s. The term "potentially |
| 76 | ragged tensor" may be used to refer to a tensor that might be either a |
| 77 | `Tensor` or a `RaggedTensor`. The ragged-rank of a `Tensor` is zero. |
| 78 | |
| 79 | ### Documenting RaggedTensor Shapes |
| 80 | |
| 81 | When documenting the shape of a RaggedTensor, ragged dimensions can be |
| 82 | indicated by enclosing them in parentheses. For example, the shape of |
| 83 | a 3-D `RaggedTensor` that stores the fixed-size word embedding for each |
| 84 | word in a sentence, for each sentence in a batch, could be written as |
| 85 | `[num_sentences, (num_words), embedding_size]`. The parentheses around |
| 86 | `(num_words)` indicate that dimension is ragged, and that the length |
| 87 | of each element list in that dimension may vary for each item. |
| 88 | |
| 89 | ### Component Tensors |
| 90 | |
| 91 | Internally, a `RaggedTensor` consists of a concatenated list of values that |
| 92 | are partitioned into variable-length rows. In particular, each `RaggedTensor` |
| 93 | consists of: |
| 94 | |
| 95 | * A `values` tensor, which concatenates the variable-length rows into a |
| 96 | flattened list. For example, the `values` tensor for |
| 97 | `[[3, 1, 4, 1], [], [5, 9, 2], [6], []]` is `[3, 1, 4, 1, 5, 9, 2, 6]`. |
| 98 | |
| 99 | * A `row_splits` vector, which indicates how those flattened values are |
| 100 | divided into rows. In particular, the values for row `rt[i]` are stored |
| 101 | in the slice `rt.values[rt.row_splits[i]:rt.row_splits[i+1]]`. |
| 102 | |
| 103 | Example: |
| 104 | |
| 105 | ```python |
| 106 | >>> print(tf.RaggedTensor.from_row_splits( |
| 107 | ... values=[3, 1, 4, 1, 5, 9, 2, 6], |
| 108 | ... row_splits=[0, 4, 4, 7, 8, 8])) |
| 109 | <tf.RaggedTensor [[3, 1, 4, 1], [], [5, 9, 2], [6], []]> |
| 110 | ``` |
| 111 | |
| 112 | ### Alternative Row-Partitioning Schemes |
no outgoing calls