clarena.utils.transforms

The submodule in utils for transforming tensors.

 1"""The submodule in `utils` for transforming tensors."""
 2
 3__all__ = ["min_max_normalise"]
 4
 5
 6from torch import Tensor
 7
 8
 9def min_max_normalise(
10    tensor: Tensor, dim: int | None = None, epsilon: float = 1e-8
11) -> Tensor:
12    r"""Normalise the tensor using min-max normalisation.
13
14    **Args:**
15    - **tensor** (`Tensor`): the input tensor to normalise.
16    - **dim** (`int` | `None`): the dimension to normalise along. If `None`, normalise the whole tensor.
17    - **epsilon** (`float`): the epsilon value to avoid division by zero.
18
19    **Returns:**
20    - **tensor** (`Tensor`): the normalised tensor.
21    """
22    min_val = (
23        tensor.min(dim=dim, keepdim=True).values if dim is not None else tensor.min()
24    )
25    max_val = (
26        tensor.max(dim=dim, keepdim=True).values if dim is not None else tensor.max()
27    )
28
29    return (tensor - min_val) / (max_val - min_val + epsilon)
def min_max_normalise( tensor: torch.Tensor, dim: int | None = None, epsilon: float = 1e-08) -> torch.Tensor:
10def min_max_normalise(
11    tensor: Tensor, dim: int | None = None, epsilon: float = 1e-8
12) -> Tensor:
13    r"""Normalise the tensor using min-max normalisation.
14
15    **Args:**
16    - **tensor** (`Tensor`): the input tensor to normalise.
17    - **dim** (`int` | `None`): the dimension to normalise along. If `None`, normalise the whole tensor.
18    - **epsilon** (`float`): the epsilon value to avoid division by zero.
19
20    **Returns:**
21    - **tensor** (`Tensor`): the normalised tensor.
22    """
23    min_val = (
24        tensor.min(dim=dim, keepdim=True).values if dim is not None else tensor.min()
25    )
26    max_val = (
27        tensor.max(dim=dim, keepdim=True).values if dim is not None else tensor.max()
28    )
29
30    return (tensor - min_val) / (max_val - min_val + epsilon)

Normalise the tensor using min-max normalisation.

Args:

  • tensor (Tensor): the input tensor to normalise.
  • dim (int | None): the dimension to normalise along. If None, normalise the whole tensor.
  • epsilon (float): the epsilon value to avoid division by zero.

Returns:

  • tensor (Tensor): the normalised tensor.