jactorch.functional.indexing#
Tensor indexing utils.
Functions
|
Select elements from tensor according to batched_indices. |
|
Iteratively generates the values of tensor where mask is nonzero. |
|
tensor[:, :, index, :] |
|
tensor[:, :, index, ...]. |
|
Inverse a permutation. |
|
Return the smallest nonzero index along the dim axis. |
|
Convert a list of class labels into one-hot representation. |
|
Convert a tensor of class labels into one-hot representation by adding a new dimension indexed at dim. |
|
Convert a tensor of class labels into one-hot representation. |
|
Reverse a tensor along the given dimension. |
|
Return the smallest nonzero index along the dim axis. |
|
tensor[:, :, index, :, :] = value. |
Functions
- batched_index_select(tensor, batched_indices)[source]#
Select elements from tensor according to batched_indices. The first dimension is assumed to be the batch dimension. This operation is equivalent to numpy: tensor[np.arange(len(batched_indices)), batched_indices].
- Parameters:
tensor (torch.Tensor) – input.
batched_indices (torch.Tensor) – the indices to be selected.
- Returns:
the selected elements.
- Return type:
- index_nonzero(tensor, mask)[source]#
Iteratively generates the values of tensor where mask is nonzero. When mask is a 1D tensor, this function is equivalent to:
for i in range(mask.size(0)): if mask[i]: yield tensor[i]
- Parameters:
tensor (torch.Tensor) – input.
mask (torch.Tensor) – the mask.
- Yields:
torch.Tensor – the values of tensor where mask is nonzero.
- index_one_hot(tensor, dim, index)[source]#
tensor[:, :, index, :]
- Parameters:
tensor (torch.Tensor) – input.
dim (int)
index (Tensor) – (torch.Tensor): the tensor containing the indices along the dim dimension.
- Returns:
tensor[:, :, index, :, :].
- Return type:
- index_one_hot_ellipsis(tensor, dim, index)[source]#
tensor[:, :, index, …].
- Parameters:
tensor (torch.Tensor) – input.
dim (int)
index (Tensor) – (torch.Tensor): the tensor containing the indices along the dim dimension.
- Returns:
tensor[:, :, index, …].
- Return type:
- inverse_permutation(perm)[source]#
Inverse a permutation.
Warning
This function does not check the validness of the input. That is, if the input is not a permutation, this function may generate arbitrary output.
- Parameters:
perm (torch.Tensor) – shape (N, ) representing a permutation of 0 ~ N - 1.
- Returns:
the inverse permutation, which satisfies: inv[perm[x]] = x.
- Return type:
- leftmost_nonzero(tensor, dim)[source]#
Return the smallest nonzero index along the dim axis. The tensor should be binary.
- Parameters:
tensor (torch.Tensor) – input.
dim (int) – the dimension.
- Returns:
the smallest nonzero index along the dim axis.
- Return type:
- one_hot(index, nr_classes)[source]#
Convert a list of class labels into one-hot representation.
Note
This function support only one-dimensional input. For high dimensional inputs, use one_hot_nd.
- Parameters:
index (torch.Tensor) – shape (N, ), input class labels.
nr_classes (int) – number of total classes.
- Returns:
shape (N, nr_classes), one-hot representation of the class labels.
- Return type:
- one_hot_dim(index, nr_classes, dim)[source]#
Convert a tensor of class labels into one-hot representation by adding a new dimension indexed at dim.
- Parameters:
index (torch.Tensor) – input class labels.
nr_classes (int) – number of total classes.
dim (int) – dimension of the class label.
- Returns:
one-hot representation of the class labels.
- Return type:
- one_hot_nd(index, nr_classes)[source]#
Convert a tensor of class labels into one-hot representation.
- Parameters:
index (torch.Tensor) – input class labels.
nr_classes (int) – number of total classes.
- Returns:
one-hot representation of the class labels, the label dimension is assumed to be the last one.
- Return type:
- reversed(x, dim=-1)[source]#
Reverse a tensor along the given dimension. For example, if dim=0, it is equivalent to the python notation: x[::-1].
- Parameters:
x (torch.Tensor) – input.
dim (int) – the dimension to be reversed.
- Returns:
of same shape as x, but with the dimension dim reversed.
- Return type:
- rightmost_nonzero(tensor, dim)[source]#
Return the smallest nonzero index along the dim axis. The tensor should be binary.
- Parameters:
tensor (torch.Tensor) – input.
dim (int) – the dimension.
- Returns:
the smallest nonzero index along the dim axis.
- Return type:
- set_index_one_hot_(tensor, dim, index, value)[source]#
tensor[:, :, index, :, :] = value.
- Parameters:
tensor (torch.Tensor) – input.
dim (int)
index (Tensor) – (torch.Tensor): the tensor containing the indices along the dim dimension.
value (torch.Tensor) – the value to be set.
- Return type:
None