Gather slices from `params` using `n`-dimensional indices. This operation is similar to `gather`, but it uses the innermost dimension of `indices` to define a slice into `params`. In particular, if: * `indices` has shape `[A1...AN, I]` * `params` has shape `[B1...BM]` Then: * `resul
(params, indices, batch_dims=0, name=None)
| 125 | # ragged.gather_nd |
| 126 | #=============================================================================== |
| 127 | def gather_nd(params, indices, batch_dims=0, name=None): |
| 128 | """Gather slices from `params` using `n`-dimensional indices. |
| 129 | |
| 130 | This operation is similar to `gather`, but it uses the innermost dimension |
| 131 | of `indices` to define a slice into `params`. In particular, if: |
| 132 | |
| 133 | * `indices` has shape `[A1...AN, I]` |
| 134 | * `params` has shape `[B1...BM]` |
| 135 | |
| 136 | Then: |
| 137 | |
| 138 | * `result` has shape `[A1...AN, B_{I+1}...BM]`. |
| 139 | * `result[a1...aN] = params[indices[a1...aN, :]]` |
| 140 | |
| 141 | Args: |
| 142 | params: A potentially ragged tensor with shape `[A1...AN, I]`. |
| 143 | indices: A potentially ragged tensor with shape `[B1...BM]`. |
| 144 | batch_dims: Must be zero. |
| 145 | name: A name for the operation (optional). |
| 146 | |
| 147 | Returns: |
| 148 | A potentially ragged tensor with shape `[A1...AN, B_{I+1}...BM]`. |
| 149 | |
| 150 | #### Examples: |
| 151 | ```python |
| 152 | >>> params = tf.compat.v1.ragged.constant_value( |
| 153 | ... [ [ ['000', '001'], ['010' ] ], |
| 154 | ... [ ['100' ], ['110', '111', '112'], ['120'] ], |
| 155 | ... [ [ ], ['210' ] ] ]) |
| 156 | |
| 157 | >>> # Gather 2D slices from a 3D tensor |
| 158 | >>> ragged.gather_nd(params, [[2], [0]]) |
| 159 | [ [ [ ], ['210'] ] |
| 160 | [ ['000', '001'], ['010'] ] ] |
| 161 | |
| 162 | >>> # Gather 1D slices from a 3D tensor |
| 163 | >>> ragged.gather_nd(params, [[2, 1], [0, 0]]) |
| 164 | [['210'], ['000', '001']] |
| 165 | |
| 166 | >>> # Gather scalars from a 3D tensor |
| 167 | >>> ragged.gather_nd(params, [[0, 0, 1], [1, 1, 2]]) |
| 168 | ['001', '112'] |
| 169 | ``` |
| 170 | """ |
| 171 | if not isinstance(batch_dims, int) or batch_dims != 0: |
| 172 | raise ValueError('batch_dims != 0 is not supported for ragged gather yet.') |
| 173 | if not (ragged_tensor.is_ragged(params) or ragged_tensor.is_ragged(indices)): |
| 174 | return array_ops.gather_nd(params, indices, name) |
| 175 | |
| 176 | with ops.name_scope(name, 'RaggedGatherNd', [params, indices]): |
| 177 | |
| 178 | params = ragged_tensor.convert_to_tensor_or_ragged_tensor( |
| 179 | params, name='params') |
| 180 | indices = ragged_tensor.convert_to_tensor_or_ragged_tensor( |
| 181 | indices, name='indices') |
| 182 | params, indices = ragged_tensor.match_row_splits_dtypes(params, indices) |
| 183 | indices_shape = indices.shape |
| 184 | indices_ndims = indices_shape.ndims |
nothing calls this directly
no test coverage detected