torchoutil.nn.functional.multiclass module

Helper functions for conversion between classes indices, onehot, names and probabilities for multiclass classification.

index_to_name(index: ~typing.Sequence[int] | ~torch.Tensor | ~numpy.ndarray | ~typing.Sequence, idx_to_name: ~typing.Mapping[int, ~torchoutil.nn.functional.multiclass.T_Name] | ~typing.Sequence[~torchoutil.nn.functional.multiclass.T_Name], *, is_number_fn: ~typing.Callable[[~typing.Any], bool] = <function is_number_like>) List[T_Name][source]

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

Args:

indices: List of list of label indices. idx_to_name: Mapping to convert a class index to its name. is_number_fn: Type guard to check if a value is a scalar number. defaults to is_number_like.

index_to_onehot(
index: Sequence[int] | Tensor | ndarray | Sequence,
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 onehot boolean encoding for multiclass classification.

Args:
indices: List label indices.

Can be a nested list of indices, but it should be convertible to Tensor.

num_classes: Number maximal of unique classes. padding_idx: Class index to ignore. Output will contains only zeroes for this value. defaults to None. device: PyTorch device of the output tensor. dtype: PyTorch DType of the output tensor.

name_to_index(
name: List[T_Name],
idx_to_name: Mapping[int, T_Name] | Sequence[T_Name],
) Tensor[source]

Convert names to indices of labels for multiclass classification.

Args:

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

name_to_onehot(
name: 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 onehot boolean encoding for multiclass 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.

one_hot(
tensor: Sequence[int] | Tensor | ndarray | Sequence,
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]

Alias of index_to_onehot().

onehot_to_index(
onehot: Tensor,
*,
padding_idx: int | None = None,
dim: int = -1,
) LongTensor[source]

Convert onehot boolean encoding to indices of labels for multiclass classification.

Args:

onehot: Onehot labels encoded as 2D matrix. padding_idx: Class index placeholder when input contains only zeroes. defaults to None. dim: Dimension of classes. defaults to -1.

onehot_to_name(
onehot: Tensor,
idx_to_name: Mapping[int, T_Name] | Sequence[T_Name],
*,
dim: int = -1,
) List[T_Name][source]

Convert onehot boolean encoding to names using a mapping for multiclass classification.

Args:

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

probs_to_index(
probs: Tensor,
*,
dim: int = -1,
) LongTensor[source]

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

Args:

probs: Output probabilities for each classes. dim: Dimension of classes. defaults to -1.

probs_to_name(
probs: Tensor,
idx_to_name: Mapping[int, T_Name] | Sequence[T_Name],
*,
dim: int = -1,
) List[T_Name][source]

Convert matrix of probabilities to labels names for multiclass classification.

Args:

probs: Output probabilities for each classes. idx_to_name: Mapping to convert a class index to its name. dim: Dimension of classes. defaults to -1.

probs_to_onehot(
probs: Tensor,
*,
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 onehot boolean encoding for multiclass classification.

Args:

probs: Output probabilities for each classes. dim: Dimension of classes. defaults to -1. device: PyTorch device of the output tensor. dtype: PyTorch DType of the output tensor.