|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import torch |
| 4 | +from torch.distributions import ( |
| 5 | + Normal, |
| 6 | + Transform, |
| 7 | + TransformedDistribution, |
| 8 | + constraints, |
| 9 | +) |
| 10 | +from typing import Any |
| 11 | + |
| 12 | + |
| 13 | +def symlog(x: torch.Tensor) -> torch.Tensor: |
| 14 | + """ |
| 15 | + Applies the symlog function element-wise. |
| 16 | +
|
| 17 | + symlog(x) = sign(x) * log(1 + |x|) |
| 18 | + """ |
| 19 | + return torch.sign(x) * torch.log(1 + torch.abs(x)) |
| 20 | + |
| 21 | + |
| 22 | +def symexp(x: torch.Tensor) -> torch.Tensor: |
| 23 | + """ |
| 24 | + Applies the symexp function element-wise. |
| 25 | +
|
| 26 | + symexp(x) = sign(x) * (exp(|x|) - 1) |
| 27 | + """ |
| 28 | + return torch.sign(x) * (torch.exp(torch.abs(x)) - 1) |
| 29 | + |
| 30 | + |
| 31 | +class SymexpTransform(Transform): |
| 32 | + """ |
| 33 | + A bijective transform implementing the symexp function. |
| 34 | +
|
| 35 | + This transform is its own inverse, applying symlog. It is used to warp a |
| 36 | + base distribution into a symlog-space. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self) -> None: |
| 40 | + super().__init__() |
| 41 | + self.bijective = True |
| 42 | + self.domain = constraints.real |
| 43 | + self.codomain = constraints.real |
| 44 | + |
| 45 | + def _call(self, x: torch.Tensor) -> torch.Tensor: |
| 46 | + return symexp(x) |
| 47 | + |
| 48 | + def _inverse(self, y: torch.Tensor) -> torch.Tensor: |
| 49 | + return symlog(y) |
| 50 | + |
| 51 | + def log_abs_det_jacobian(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 52 | + # For y = symexp(x), dy/dx = exp(|x|) |
| 53 | + # log|dy/dx| = log(exp(|x|)) = |x| |
| 54 | + return torch.abs(x) |
| 55 | + |
| 56 | + @property |
| 57 | + def sign(self) -> int: |
| 58 | + """The sign of the transform (always positive for symexp).""" |
| 59 | + return 1 |
| 60 | + |
| 61 | + |
| 62 | +class SymLogDistribution(TransformedDistribution): |
| 63 | + """ |
| 64 | + A distribution that transforms a Normal distribution with a symexp transform. |
| 65 | +
|
| 66 | + This distribution is useful for modeling data with a wide dynamic range, |
| 67 | + where the data can be both positive and negative, and can have values |
| 68 | + close to zero. The symlog transform compresses large values and expands |
| 69 | + small values, making the distribution more stable for optimization. |
| 70 | +
|
| 71 | + Args: |
| 72 | + loc (torch.Tensor): The mean of the base Normal distribution. |
| 73 | + scale (torch.Tensor): The standard deviation of the base Normal distribution. |
| 74 | + validate_args (bool, optional): Whether to validate the arguments. |
| 75 | + Defaults to None. |
| 76 | + """ |
| 77 | + |
| 78 | + arg_constraints = {"loc": constraints.real, "scale": constraints.positive} |
| 79 | + |
| 80 | + def __init__( |
| 81 | + self, |
| 82 | + loc: torch.Tensor, |
| 83 | + scale: torch.Tensor, |
| 84 | + validate_args: bool | None = None, |
| 85 | + ) -> None: |
| 86 | + self._loc = loc |
| 87 | + self._scale = scale |
| 88 | + base_dist = Normal(loc, scale) |
| 89 | + super().__init__(base_dist, SymexpTransform(), validate_args=validate_args) |
| 90 | + |
| 91 | + @property |
| 92 | + def loc(self) -> torch.Tensor: |
| 93 | + return self._loc |
| 94 | + |
| 95 | + @property |
| 96 | + def scale(self) -> torch.Tensor: |
| 97 | + return self._scale |
| 98 | + |
| 99 | + @property |
| 100 | + def mean(self) -> torch.Tensor: |
| 101 | + """Approximated by mode for now, as per instructions.""" |
| 102 | + return self.mode |
| 103 | + |
| 104 | + @property |
| 105 | + def mode(self) -> torch.Tensor: |
| 106 | + """The mode of the distribution.""" |
| 107 | + return symexp(self._loc) |
| 108 | + |
| 109 | + def expand( |
| 110 | + self, batch_shape: Any, _instance: SymLogDistribution | None = None |
| 111 | + ) -> SymLogDistribution: |
| 112 | + """ |
| 113 | + Returns a new distribution instance with expanded batch shape. |
| 114 | +
|
| 115 | + Args: |
| 116 | + batch_shape (Any): The new batch shape. |
| 117 | + _instance (SymLogDistribution, optional): The instance to expand. |
| 118 | + Defaults to None. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + SymLogDistribution: The expanded distribution. |
| 122 | + """ |
| 123 | + new = self._get_checked_instance(SymLogDistribution, _instance) |
| 124 | + batch_shape = torch.Size(batch_shape) |
| 125 | + new._loc = self._loc.expand(batch_shape) |
| 126 | + new._scale = self._scale.expand(batch_shape) |
| 127 | + base_dist = Normal(new._loc, new._scale) |
| 128 | + super(SymLogDistribution, new).__init__( |
| 129 | + base_dist, SymexpTransform(), validate_args=False |
| 130 | + ) |
| 131 | + new._validate_args = self._validate_args |
| 132 | + return new |
0 commit comments