dirichlet.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import torch
  2. from torch.autograd import Function
  3. from torch.autograd.function import once_differentiable
  4. from torch.distributions import constraints
  5. from torch.distributions.exp_family import ExponentialFamily
  6. # This helper is exposed for testing.
  7. def _Dirichlet_backward(x, concentration, grad_output):
  8. total = concentration.sum(-1, True).expand_as(concentration)
  9. grad = torch._dirichlet_grad(x, concentration, total)
  10. return grad * (grad_output - (x * grad_output).sum(-1, True))
  11. class _Dirichlet(Function):
  12. @staticmethod
  13. def forward(ctx, concentration):
  14. x = torch._sample_dirichlet(concentration)
  15. ctx.save_for_backward(x, concentration)
  16. return x
  17. @staticmethod
  18. @once_differentiable
  19. def backward(ctx, grad_output):
  20. x, concentration = ctx.saved_tensors
  21. return _Dirichlet_backward(x, concentration, grad_output)
  22. class Dirichlet(ExponentialFamily):
  23. r"""
  24. Creates a Dirichlet distribution parameterized by concentration :attr:`concentration`.
  25. Example::
  26. >>> m = Dirichlet(torch.tensor([0.5, 0.5]))
  27. >>> m.sample() # Dirichlet distributed with concentrarion concentration
  28. tensor([ 0.1046, 0.8954])
  29. Args:
  30. concentration (Tensor): concentration parameter of the distribution
  31. (often referred to as alpha)
  32. """
  33. arg_constraints = {'concentration': constraints.independent(constraints.positive, 1)}
  34. support = constraints.simplex
  35. has_rsample = True
  36. def __init__(self, concentration, validate_args=None):
  37. if concentration.dim() < 1:
  38. raise ValueError("`concentration` parameter must be at least one-dimensional.")
  39. self.concentration = concentration
  40. batch_shape, event_shape = concentration.shape[:-1], concentration.shape[-1:]
  41. super(Dirichlet, self).__init__(batch_shape, event_shape, validate_args=validate_args)
  42. def expand(self, batch_shape, _instance=None):
  43. new = self._get_checked_instance(Dirichlet, _instance)
  44. batch_shape = torch.Size(batch_shape)
  45. new.concentration = self.concentration.expand(batch_shape + self.event_shape)
  46. super(Dirichlet, new).__init__(batch_shape, self.event_shape, validate_args=False)
  47. new._validate_args = self._validate_args
  48. return new
  49. def rsample(self, sample_shape=()):
  50. shape = self._extended_shape(sample_shape)
  51. concentration = self.concentration.expand(shape)
  52. return _Dirichlet.apply(concentration)
  53. def log_prob(self, value):
  54. if self._validate_args:
  55. self._validate_sample(value)
  56. return ((torch.log(value) * (self.concentration - 1.0)).sum(-1) +
  57. torch.lgamma(self.concentration.sum(-1)) -
  58. torch.lgamma(self.concentration).sum(-1))
  59. @property
  60. def mean(self):
  61. return self.concentration / self.concentration.sum(-1, True)
  62. @property
  63. def mode(self):
  64. concentrationm1 = (self.concentration - 1).clamp(min=0.)
  65. mode = concentrationm1 / concentrationm1.sum(-1, True)
  66. mask = (self.concentration < 1).all(axis=-1)
  67. mode[mask] = torch.nn.functional.one_hot(mode[mask].argmax(axis=-1), concentrationm1.shape[-1]).to(mode)
  68. return mode
  69. @property
  70. def variance(self):
  71. con0 = self.concentration.sum(-1, True)
  72. return self.concentration * (con0 - self.concentration) / (con0.pow(2) * (con0 + 1))
  73. def entropy(self):
  74. k = self.concentration.size(-1)
  75. a0 = self.concentration.sum(-1)
  76. return (torch.lgamma(self.concentration).sum(-1) - torch.lgamma(a0) -
  77. (k - a0) * torch.digamma(a0) -
  78. ((self.concentration - 1.0) * torch.digamma(self.concentration)).sum(-1))
  79. @property
  80. def _natural_params(self):
  81. return (self.concentration, )
  82. def _log_normalizer(self, x):
  83. return x.lgamma().sum(-1) - torch.lgamma(x.sum(-1))