laplace.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from numbers import Number
  2. import torch
  3. from torch.distributions import constraints
  4. from torch.distributions.distribution import Distribution
  5. from torch.distributions.utils import broadcast_all
  6. class Laplace(Distribution):
  7. r"""
  8. Creates a Laplace distribution parameterized by :attr:`loc` and :attr:`scale`.
  9. Example::
  10. >>> m = Laplace(torch.tensor([0.0]), torch.tensor([1.0]))
  11. >>> m.sample() # Laplace distributed with loc=0, scale=1
  12. tensor([ 0.1046])
  13. Args:
  14. loc (float or Tensor): mean of the distribution
  15. scale (float or Tensor): scale of the distribution
  16. """
  17. arg_constraints = {'loc': constraints.real, 'scale': constraints.positive}
  18. support = constraints.real
  19. has_rsample = True
  20. @property
  21. def mean(self):
  22. return self.loc
  23. @property
  24. def mode(self):
  25. return self.loc
  26. @property
  27. def variance(self):
  28. return 2 * self.scale.pow(2)
  29. @property
  30. def stddev(self):
  31. return (2 ** 0.5) * self.scale
  32. def __init__(self, loc, scale, validate_args=None):
  33. self.loc, self.scale = broadcast_all(loc, scale)
  34. if isinstance(loc, Number) and isinstance(scale, Number):
  35. batch_shape = torch.Size()
  36. else:
  37. batch_shape = self.loc.size()
  38. super(Laplace, self).__init__(batch_shape, validate_args=validate_args)
  39. def expand(self, batch_shape, _instance=None):
  40. new = self._get_checked_instance(Laplace, _instance)
  41. batch_shape = torch.Size(batch_shape)
  42. new.loc = self.loc.expand(batch_shape)
  43. new.scale = self.scale.expand(batch_shape)
  44. super(Laplace, new).__init__(batch_shape, validate_args=False)
  45. new._validate_args = self._validate_args
  46. return new
  47. def rsample(self, sample_shape=torch.Size()):
  48. shape = self._extended_shape(sample_shape)
  49. finfo = torch.finfo(self.loc.dtype)
  50. if torch._C._get_tracing_state():
  51. # [JIT WORKAROUND] lack of support for .uniform_()
  52. u = torch.rand(shape, dtype=self.loc.dtype, device=self.loc.device) * 2 - 1
  53. return self.loc - self.scale * u.sign() * torch.log1p(-u.abs().clamp(min=finfo.tiny))
  54. u = self.loc.new(shape).uniform_(finfo.eps - 1, 1)
  55. # TODO: If we ever implement tensor.nextafter, below is what we want ideally.
  56. # u = self.loc.new(shape).uniform_(self.loc.nextafter(-.5, 0), .5)
  57. return self.loc - self.scale * u.sign() * torch.log1p(-u.abs())
  58. def log_prob(self, value):
  59. if self._validate_args:
  60. self._validate_sample(value)
  61. return -torch.log(2 * self.scale) - torch.abs(value - self.loc) / self.scale
  62. def cdf(self, value):
  63. if self._validate_args:
  64. self._validate_sample(value)
  65. return 0.5 - 0.5 * (value - self.loc).sign() * torch.expm1(-(value - self.loc).abs() / self.scale)
  66. def icdf(self, value):
  67. term = value - 0.5
  68. return self.loc - self.scale * (term).sign() * torch.log1p(-2 * term.abs())
  69. def entropy(self):
  70. return 1 + torch.log(2 * self.scale)