Loss Entropy#
entropy
#
| CLASS | DESCRIPTION |
|---|---|
EntropyLoss |
Entropy-based loss for probability distributions with optional weights. |
| FUNCTION | DESCRIPTION |
|---|---|
entropy_loss |
Computes a loss based on Shannon entropy with optional sample weights. |
Classes#
EntropyLoss(reduce: Literal['mean', 'sum'] = 'mean', sign: float = 1.0, eps: float = torch.finfo(torch.float32).eps, context_prefix: str | None = None, **kwargs: dict)
#
Bases: Loss
Entropy-based loss for probability distributions with optional weights.
Computes a loss based on Shannon entropy \(H(x) = -\sum_k w_k z_k \log(z_k)\), where \(z_k\) is a probability distribution at sample \(k\) and \(w_k\) are optional sample weights.
| PARAMETER | DESCRIPTION |
|---|---|
reduce
|
Reduction method.
TYPE:
|
sign
|
Loss sign (1.0 for minimizing entropy, -1.0 for maximizing).
TYPE:
|
eps
|
Small constant to avoid log(0).
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
forward |
Compute entropy loss. |
Source code in spectre/loss/entropy.py
Functions#
forward(context: dict, **kwargs) -> torch.Tensor
#
Compute entropy loss.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Context dictionary.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Entropy loss. |
Source code in spectre/loss/entropy.py
Functions#
entropy_loss(Z: torch.Tensor, weights: Optional[torch.Tensor] = None, reduce: str = 'mean', sign: float = 1.0, eps: float = torch.finfo(torch.float32).eps) -> torch.Tensor
#
Computes a loss based on Shannon entropy with optional sample weights.
Computes \(H(x) = -\sum_k w_k z_k \log(z_k)\), where \(z_k\) is a probability distribution at sample \(k\) and \(w_k\) are optional sample weights.
| PARAMETER | DESCRIPTION |
|---|---|
Z
|
Probability distribution (1D tensor). Should contain non-negative values that sum to 1.
TYPE:
|
weights
|
Sample weights of shape
TYPE:
|
reduce
|
Reduction method.
TYPE:
|
sign
|
Loss sign (1.0 for minimizing entropy, -1.0 for maximizing).
TYPE:
|
eps
|
Small constant to avoid log(0).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Entropy loss. |