half_normal.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import math
  2. import torch
  3. from torch import inf
  4. from torch.distributions import constraints
  5. from torch.distributions.transforms import AbsTransform
  6. from torch.distributions.normal import Normal
  7. from torch.distributions.transformed_distribution import TransformedDistribution
  8. __all__ = ['HalfNormal']
  9. class HalfNormal(TransformedDistribution):
  10. r"""
  11. Creates a half-normal distribution parameterized by `scale` where::
  12. X ~ Normal(0, scale)
  13. Y = |X| ~ HalfNormal(scale)
  14. Example::
  15. >>> # xdoctest: +IGNORE_WANT("non-deterinistic")
  16. >>> m = HalfNormal(torch.tensor([1.0]))
  17. >>> m.sample() # half-normal distributed with scale=1
  18. tensor([ 0.1046])
  19. Args:
  20. scale (float or Tensor): scale of the full Normal distribution
  21. """
  22. arg_constraints = {'scale': constraints.positive}
  23. support = constraints.nonnegative
  24. has_rsample = True
  25. def __init__(self, scale, validate_args=None):
  26. base_dist = Normal(0, scale, validate_args=False)
  27. super().__init__(base_dist, AbsTransform(), validate_args=validate_args)
  28. def expand(self, batch_shape, _instance=None):
  29. new = self._get_checked_instance(HalfNormal, _instance)
  30. return super().expand(batch_shape, _instance=new)
  31. @property
  32. def scale(self):
  33. return self.base_dist.scale
  34. @property
  35. def mean(self):
  36. return self.scale * math.sqrt(2 / math.pi)
  37. @property
  38. def mode(self):
  39. return torch.zeros_like(self.scale)
  40. @property
  41. def variance(self):
  42. return self.scale.pow(2) * (1 - 2 / math.pi)
  43. def log_prob(self, value):
  44. if self._validate_args:
  45. self._validate_sample(value)
  46. log_prob = self.base_dist.log_prob(value) + math.log(2)
  47. log_prob = torch.where(value >= 0, log_prob, -inf)
  48. return log_prob
  49. def cdf(self, value):
  50. if self._validate_args:
  51. self._validate_sample(value)
  52. return 2 * self.base_dist.cdf(value) - 1
  53. def icdf(self, prob):
  54. return self.base_dist.icdf((prob + 1) / 2)
  55. def entropy(self):
  56. return self.base_dist.entropy() - math.log(2)