ytm.utils.norm_asymmetric#

ytm.utils.norm_asymmetric(arr: ndarray, eps: float = 1e-9) → ndarray#

Zero-anchored per-sample normalization. Scales output to [-1, 1].

For each sample independently, negative values are scaled by that sample’s own |min| so the most negative value maps to -1. Positive values are scaled by that sample’s own max so the most positive value maps to 1. Zero stays zero.

Required to properly scale interpretation maps of the TM, especially for images.

Parameters:
arrndarray of shape (N, …)

Input array. The first axis is the sample axis.

epsfloat, default=1e-9

Added to each divisor to avoid division by zero.

Returns:
ndarray of shape (N, …)

Normalized copy of arr. The input is not changed.

Examples

>>> norm_asymmetric(np.array([[-2.0, 0.0, 1.0, 4.0]]))
array([[-1.  ,  0.  ,  0.25,  1.  ]])