espnet2.gan_codec.shared.quantizer.modules.core_vq.laplace_smoothing
Less than 1 minute
espnet2.gan_codec.shared.quantizer.modules.core_vq.laplace_smoothing
espnet2.gan_codec.shared.quantizer.modules.core_vq.laplace_smoothing(x, n_categories: int, epsilon: float = 1e-05)
Apply Laplace smoothing to a given tensor.
This function performs Laplace smoothing on the input tensor x to prevent zero probabilities in categorical distributions. The smoothing technique adds a small constant (epsilon) to each element of x and normalizes by the total sum adjusted for the number of categories.
- Parameters:
- x (Tensor) – The input tensor representing counts or frequencies for each category.
- n_categories (int) – The total number of categories to be considered for smoothing.
- epsilon (float , optional) – A small value added to each count for numerical stability. Defaults to 1e-5.
- Returns: A tensor of the same shape as x, containing the smoothed probabilities.
- Return type: Tensor
Examples
>>> import torch
>>> counts = torch.tensor([0, 2, 3])
>>> smoothed_probs = laplace_smoothing(counts, n_categories=3)
>>> print(smoothed_probs)
tensor([0.1667, 0.3333, 0.5000])
NOTE
This function is particularly useful in scenarios involving categorical data where some categories may not have any observations, thereby resulting in zero probabilities.