The same as tf.gather_nd but batched gather is not supported yet. indices is an k-dimensional integer tensor, best thought of as a (k-1)-dimensional tensor of indices into params, where each element defines a slice of params: output[\\(i_0, ..., i_{k-2}\\)] = params[indices[\\(i_0, ..., i_
(params, indices)
| 216 | |
| 217 | |
| 218 | def gather_nd(params, indices): |
| 219 | """ The same as tf.gather_nd but batched gather is not supported yet. |
| 220 | indices is an k-dimensional integer tensor, best thought of as a (k-1)-dimensional tensor of indices into params, where each element defines a slice of params: |
| 221 | |
| 222 | output[\\(i_0, ..., i_{k-2}\\)] = params[indices[\\(i_0, ..., i_{k-2}\\)]] |
| 223 | |
| 224 | Args: |
| 225 | params (Tensor): "n" dimensions. shape: [x_0, x_1, x_2, ..., x_{n-1}] |
| 226 | indices (Tensor): "k" dimensions. shape: [y_0,y_2,...,y_{k-2}, m]. m <= n. |
| 227 | |
| 228 | Returns: gathered Tensor. |
| 229 | shape [y_0,y_2,...y_{k-2}] + params.shape[m:] |
| 230 | |
| 231 | """ |
| 232 | orig_shape = list(indices.shape) |
| 233 | num_samples = np.prod(orig_shape[:-1]) |
| 234 | m = orig_shape[-1] |
| 235 | n = len(params.shape) |
| 236 | |
| 237 | if m <= n: |
| 238 | out_shape = orig_shape[:-1] + list(params.shape)[m:] |
| 239 | else: |
| 240 | raise ValueError(f'the last dimension of indices must less or equal to the rank of params. ' |
| 241 | f'Got indices:{indices.shape}, params:{params.shape}. {m} > {n}') |
| 242 | |
| 243 | indices = indices.reshape((num_samples, m)).transpose(0, 1).tolist() |
| 244 | output = params[indices] # (num_samples, ...) |
| 245 | return output.reshape(out_shape).contiguous() |
| 246 | |
| 247 | |
| 248 | class FrameSimilarity(nn.Module): |