neural_lam.utils.tensor#
Numerically stable inverses of common activation functions.
Functions#
|
Inverse of |
|
Inverse of |
Module Contents#
- neural_lam.utils.tensor.inverse_sigmoid(x: torch.Tensor) torch.Tensor[source]#
Inverse of
torch.sigmoidwith clamping for numerical stability.Sigmoid output takes values in
[0, 1]; we clamp the input slightly within that open interval before applyinglog(x / (1 - x)).Note that
torch.clampwill make gradients 0 near the bounds, but this is not a problem as values of x that are this close to 0 or 1 have gradients of 0 anyhow.- Parameters:
x (torch.Tensor) – Input tensor assumed to contain logits after a sigmoid.
- Returns:
Tensor containing
log(x / (1 - x))after clamping away from the saturation limits.- Return type:
Notes
torch.clampzeroes gradients for values at the bounds, but values this close to 0 or 1 already have negligible gradients.
- neural_lam.utils.tensor.inverse_softplus(x: torch.Tensor, beta: float = 1.0, threshold: float = 20.0) torch.Tensor[source]#
Inverse of
torch.nn.functional.softplus().For most inputs this function is exact up to numerical precision. The input is clamped to ensure numerical stability: values above
threshold / betaare treated as linear (which is exact in that regime), and values near zero are clamped to avoidlogof non-positive numbers. Only near the lower clamping bound does the result deviate from the true inverse.- Parameters:
x (torch.Tensor) – Input tensor whose softplus inverse should be computed.
beta (float, optional) – Softplus
betaparameter that controls the sharpness. Default1.threshold (float, optional) – Threshold above which the function is treated as linear for numerical stability. Default
20.
- Returns:
Tensor containing the inverse-softplus values.
- Return type:
Notes
torch.clampwill zero the gradients near the bounds, but values this close to zero orthreshold / betaalready have negligible gradients.