torchoutil.nn.functional.multilabel module

Helper functions for conversion between classes indices, multihot, multi-names and probabilities for multilabel classification.

indices_to_multihot(
indices: Sequence[Sequence[int] | Tensor | ndarray] | Tensor | ndarray,
num_classes: int,
*,
padding_idx: int | None = None,
device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
dtype: dtype | None | Literal['default'] | str | DTypeEnum = torch.bool,
) Tensor[source]

Convert indices of labels to multihot boolean encoding for multilabel classification.

Args:

indices: List of list of label indices. Values should be integers in range [0..num_classes-1] num_classes: Number maximal of unique classes. padding_idx: Optional pad value to ignore. device: PyTorch device of the output tensor. dtype: PyTorch DType of the output tensor.

indices_to_multinames(
indices: Sequence[Sequence[int] | Tensor | ndarray] | Tensor | ndarray,
idx_to_name: Mapping[int, T_Name] | Sequence[T_Name],
*,
padding_idx: int | None = None,
) List[List[T_Name]][source]

Convert indices of labels to names using a mapping for multilabel classification.

Args:

indices: List of list of label indices. idx_to_name: Mapping to convert a class index to its name. padding_idx: Optional pad value to ignore.

multihot_to_indices(
multihot: Tensor | ndarray | Sequence[Tensor | ndarray] | Sequence[Sequence[bool]] | Sequence,
*,
keep_tensor: bool = False,
padding_idx: int | None = None,
dim: int = -1,
) List | LongTensor[source]

Convert multihot boolean encoding to indices of labels for multilabel classification.

Args:

multihot: Multihot labels encoded as 2D matrix. Must be convertible to Tensor. keep_tensor: If True, output will be converted to a tensor if possible. defaults to False. padding_idx: Class index fill value. When none, output will not be padded. defaults to None. dim: Dimension of classes. defaults to -1.

multihot_to_multinames(
multihot: Tensor | ndarray | Sequence[Tensor | ndarray] | Sequence[Sequence[bool]],
idx_to_name: Mapping[int, T_Name] | Sequence[T_Name],
*,
dim: int = -1,
) List[List[T_Name]][source]

Convert multihot boolean encoding to names using a mapping for multilabel classification.

Args:

multihot: Multihot labels encoded as 2D matrix. idx_to_name: Mapping to convert a class index to its name. dim: Dimension of classes. defaults to -1.

multinames_to_indices(
names: List[List[T_Name]],
idx_to_name: Mapping[int, T_Name] | Sequence[T_Name],
) List[List[int]][source]

Convert names to indices of labels for multilabel classification.

Args:

names: List of list of label names. idx_to_name: Mapping to convert a class index to its name.

multinames_to_multihot(
names: List[List[T_Name]],
idx_to_name: Mapping[int, T_Name] | Sequence[T_Name],
*,
device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
dtype: dtype | None | Literal['default'] | str | DTypeEnum = torch.bool,
) Tensor[source]

Convert names to multihot boolean encoding for multilabel classification.

Args:

names: List of list of label names. idx_to_name: Mapping to convert a class index to its name. device: PyTorch device of the output tensor. dtype: PyTorch DType of the output tensor.

probs_to_indices(
probs: Tensor | ndarray,
threshold: float | Sequence[float] | Tensor | ndarray,
*,
padding_idx: int | None = None,
dim: int = -1,
) List | LongTensor[source]

Convert matrix of probabilities to indices of labels for multilabel classification.

Args:

probs: Output probabilities for each classes. threshold: Threshold(s) to binarize probabilities. Can be a scalar or a sequence of (num_classes,) thresholds. padding_idx: Class index fill value. When none, output will not be padded. defaults to None. dim: Dimension of classes. defaults to -1.

probs_to_multihot(
probs: Tensor | ndarray,
threshold: float | Sequence[float] | Tensor | ndarray,
*,
dim: int = -1,
device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
dtype: dtype | None | Literal['default'] | str | DTypeEnum = torch.bool,
) Tensor[source]

Convert matrix of probabilities to multihot boolean encoding for multilabel classification.

Args:

probs: Output probabilities for each class. threshold: Threshold(s) to binarize probabilities. Can be a scalar or a sequence of (num_classes,) thresholds. dim: Dimension of classes. defaults to -1. device: PyTorch device of the output tensor. dtype: PyTorch DType of the output tensor.

probs_to_multinames(
probs: Tensor | ndarray,
threshold: float | Sequence[float] | Tensor | ndarray,
idx_to_name: Mapping[int, T_Name] | Sequence[T_Name],
) List[List[T_Name]][source]

Convert matrix of probabilities to labels names for multilabel classification.

Args:

probs: Output probabilities for each classes. threshold: Threshold(s) to binarize probabilities. Can be a scalar or a sequence of (num_classes,) thresholds. idx_to_name: Mapping to convert a class index to its name.