List of Normalizer Layers
All normalizer layers scale and shift tensors, but do not have learnable parameters.
GaussianNormalizer
- class GaussianNormalizer(x, encoding=True, epsilon=1e-08, dim=None)[source]
Normalize a tensor to have mean of zero and standard deviation of one.
Note
Due to machine percision, mean and standard deviation may have errors on the order of \(10^{-8}\). Using different data types may affect percision of results.
- Parameters:
x (
Tensor) – Tensor of any shapeencoding (
bool) – IfTrue, scale and shift a tensor to have mean of zero and standard deviation of one. IfFalse, scale and shift from a mean of zero and std of one to the original mean and std ofx. Default:Trueepsilon (
float) – Small positive value added to standard deviation for numerical stabilitydim (
Union[int,tuple,None]) – Dimensions to be reduced in \(\text{mean}(x), \text{std}(x)\). IfNone, reduce all dimensions for scalar min and max. Default: None
- Raises:
RuntimeError – If
torch.mean(x)ortorch.std(x)contains infinite (inf) or not-a-number (nan) over a dimension to be reduced
Shape:
Input: Any shape
Output: Any shape (same shape as input)
Example:
>>> # use one instance and change mode >>> torch.manual_seed(0) # for reproducibility >>> x = torch.rand(100, 100) >>> normalizer = sml.GaussianNormalizer(x) >>> y = normalizer(x) >>> normalizer.decode() # equivalent to normalizer.encode(False) >>> x_reconstruction = normalizer(y) >>> print(x.mean(), y.mean(), x_reconstruction.mean()) >>> print(x.std(), y.std(), x_reconstruction.std()) tensor(0.5003) tensor(-6.5804e-09) tensor(0.5003) tensor(0.2879) tensor(1.) tensor(0.2879)
>>> # use two instances with different modes >>> torch.manual_seed(0) # for reproducibility >>> x = torch.rand(100, 100) >>> encoder = sml.GaussianNormalizer(x) >>> decoder = sml.GaussianNormalizer(x, encoding=False) >>> y = encoder(x) >>> x_reconstruction = decoder(y) >>> print(x.mean(), y.mean(), x_reconstruction.mean()) >>> print(x.std(), y.std(), x_reconstruction.std()) tensor(0.5003) tensor(-6.5804e-09) tensor(0.5003) tensor(0.2879) tensor(1.) tensor(0.2879)
- forward(x)[source]
Scale
xto have a new mean and standard deviation.If
self.encodingisTrue, return \(\frac{x - \text{mean}}{\text{std} + \epsilon}\). Ifself.encodingisFalse, return \((x \times (\text{std} + \epsilon)) + \text{mean}\)- Parameters:
x (
Tensor) – Tensor of any shape- Return type:
Tensor- Returns:
Tensor of same shape as
x
Attributes
- GaussianNormalizer.encoding
- GaussianNormalizer.mean: torch.Tensor
Original mean of the initialization tensor
- GaussianNormalizer.std: torch.Tensor
Original standard deviation of the initialization tensor
RangeNormalizer
- class RangeNormalizer(x, encoding=True, low=0.0, high=1.0, dim=None)[source]
Normalize a tensor to fall within the range \([\text{low}, \text{high}]\)
Note
Due to machine precision, normalized values may be outside of range by errors on the order of \(10^{-8}\).
- Parameters:
x (
Tensor) – Tensor of any shapeencoding (
bool) – IfTrue, scale and shift a tensor to be within \([\text{low}, \text{high}]\). IfFalse, scale and shift from \([\text{low}, \text{high}]\) to the original range ofx. Default:Truelow (
Union[int,float]) – Lower bound of the normalized rangehigh (
Union[int,float]) – Upper bound of the normalized rangedim (
Union[int,tuple,None]) – Dimensions to be reduced in \(\min(x), \max(x)\). IfNone, reduce all dimensions for scalar min and max. Default: None
- Raises:
ValueError – If
lowgreater or equal tohighRuntimeError – If \(\min(x)\) equals \(\max(x)\) over any dimension to be reduced. This is to prevent a
ZeroDivisionErrorfrom occuring in the computation ofscale
Shape:
Input: Any shape
Output: Any shape (same shape as input)
Example:
>>> # use one instance and change mode >>> torch.manual_seed(0) # for reproducibility >>> x = torch.normal(0, 1, (100, 100)) >>> normalizer = sml.RangeNormalizer(x) >>> y = normalizer(x) >>> normalizer.decode() # equivalent to normalizer.encode(False) >>> x_reconstruction = normalizer(y) >>> print(x.min(), y.min(), x_reconstruction.min()) >>> print(x.max(), y.max(), x_reconstruction.max()) tensor(-4.3433) tensor(0.) tensor(-4.3433) tensor(4.1015) tensor(1.) tensor(4.1015)
>>> # use two instances with different modes >>> torch.manual_seed(0) # for reproducibility >>> x = torch.normal(0, 1, (100, 100)) >>> encoder = sml.RangeNormalizer(x) >>> decoder = sml.RangeNormalizer(x, encoding=False) >>> y = encoder(x) >>> x_reconstruction = decoder(y) >>> print(x.min(), y.min(), x_reconstruction.min()) >>> print(x.max(), y.max(), x_reconstruction.max()) tensor(-4.3433) tensor(0.) tensor(-4.3433) tensor(4.1015) tensor(1.) tensor(4.1015)
- forward(x)[source]
Scale and shift
xto fall within a new range.If
self.encodingisTrue, return \((x \times \text{scale}) + \text{shift}\). Ifself.encodingisFalse, return \(\frac{x - \text{shift}}{\text{scale}}\)- Parameters:
x (
Tensor) – Tensor of any shape- Return type:
Tensor- Returns:
Tensor of same shape as
x
Attributes
- RangeNormalizer.encoding
- RangeNormalizer.scale: torch.Tensor
Multiplicative factor to rescale range of x to interval width
- RangeNormalizer.shift: torch.Tensor
Additive factor to make interval start at
self.low