shredx.utils.scaling.min_max_scale#

shredx.utils.scaling.min_max_scale(tensor: Float[Tensor, '... dim'], feature_range: tuple[float, float] = (0, 1), scaler: tuple[float, float] | None = None) tuple[Float[Tensor, '... dim'], tuple[float, float]]#

Scale a tensor to a given feature range using linear min-max normalization.

The input tensor must contain at least two elements. By default, the tensor is scaled to the (0, 1) interval. Optionally, a precomputed (min, max) pair can be passed via scaler for consistent scaling across different tensors.

Parameters:
tensorFloat[torch.Tensor, “… dim”]

Input tensor to be scaled.

feature_rangetuple[float, float], optional

Desired range of transformed data, by default (0, 1).

scalertuple[float, float] or None, optional

Tuple (min, max) used for scaling, typically obtained from a previous call to this function. If None, the minimum and maximum are computed from tensor.

Returns:
scaledFloat[torch.Tensor, “… dim”]

Tensor scaled to the specified feature_range.

min_maxtuple[float, float]

Tuple (min, max) values used for scaling, either computed from tensor or passed in via scaler.

Raises:
ValueError

If feature_range is not a tuple of two elements.

ValueError

If tensor has fewer than two elements.