pytorch
135 строк · 5.3 Кб
1import torch2from torch import inf3from torch.distributions import Categorical, constraints4from torch.distributions.binomial import Binomial5from torch.distributions.distribution import Distribution6from torch.distributions.utils import broadcast_all7
8__all__ = ["Multinomial"]9
10
11class Multinomial(Distribution):12r"""13Creates a Multinomial distribution parameterized by :attr:`total_count` and
14either :attr:`probs` or :attr:`logits` (but not both). The innermost dimension of
15:attr:`probs` indexes over categories. All other dimensions index over batches.
16
17Note that :attr:`total_count` need not be specified if only :meth:`log_prob` is
18called (see example below)
19
20.. note:: The `probs` argument must be non-negative, finite and have a non-zero sum,
21and it will be normalized to sum to 1 along the last dimension. :attr:`probs`
22will return this normalized value.
23The `logits` argument will be interpreted as unnormalized log probabilities
24and can therefore be any real number. It will likewise be normalized so that
25the resulting probabilities sum to 1 along the last dimension. :attr:`logits`
26will return this normalized value.
27
28- :meth:`sample` requires a single shared `total_count` for all
29parameters and samples.
30- :meth:`log_prob` allows different `total_count` for each parameter and
31sample.
32
33Example::
34
35>>> # xdoctest: +SKIP("FIXME: found invalid values")
36>>> m = Multinomial(100, torch.tensor([ 1., 1., 1., 1.]))
37>>> x = m.sample() # equal probability of 0, 1, 2, 3
38tensor([ 21., 24., 30., 25.])
39
40>>> Multinomial(probs=torch.tensor([1., 1., 1., 1.])).log_prob(x)
41tensor([-4.1338])
42
43Args:
44total_count (int): number of trials
45probs (Tensor): event probabilities
46logits (Tensor): event log probabilities (unnormalized)
47"""
48arg_constraints = {"probs": constraints.simplex, "logits": constraints.real_vector}49total_count: int50
51@property52def mean(self):53return self.probs * self.total_count54
55@property56def variance(self):57return self.total_count * self.probs * (1 - self.probs)58
59def __init__(self, total_count=1, probs=None, logits=None, validate_args=None):60if not isinstance(total_count, int):61raise NotImplementedError("inhomogeneous total_count is not supported")62self.total_count = total_count63self._categorical = Categorical(probs=probs, logits=logits)64self._binomial = Binomial(total_count=total_count, probs=self.probs)65batch_shape = self._categorical.batch_shape66event_shape = self._categorical.param_shape[-1:]67super().__init__(batch_shape, event_shape, validate_args=validate_args)68
69def expand(self, batch_shape, _instance=None):70new = self._get_checked_instance(Multinomial, _instance)71batch_shape = torch.Size(batch_shape)72new.total_count = self.total_count73new._categorical = self._categorical.expand(batch_shape)74super(Multinomial, new).__init__(75batch_shape, self.event_shape, validate_args=False76)77new._validate_args = self._validate_args78return new79
80def _new(self, *args, **kwargs):81return self._categorical._new(*args, **kwargs)82
83@constraints.dependent_property(is_discrete=True, event_dim=1)84def support(self):85return constraints.multinomial(self.total_count)86
87@property88def logits(self):89return self._categorical.logits90
91@property92def probs(self):93return self._categorical.probs94
95@property96def param_shape(self):97return self._categorical.param_shape98
99def sample(self, sample_shape=torch.Size()):100sample_shape = torch.Size(sample_shape)101samples = self._categorical.sample(102torch.Size((self.total_count,)) + sample_shape103)104# samples.shape is (total_count, sample_shape, batch_shape), need to change it to105# (sample_shape, batch_shape, total_count)106shifted_idx = list(range(samples.dim()))107shifted_idx.append(shifted_idx.pop(0))108samples = samples.permute(*shifted_idx)109counts = samples.new(self._extended_shape(sample_shape)).zero_()110counts.scatter_add_(-1, samples, torch.ones_like(samples))111return counts.type_as(self.probs)112
113def entropy(self):114n = torch.tensor(self.total_count)115
116cat_entropy = self._categorical.entropy()117term1 = n * cat_entropy - torch.lgamma(n + 1)118
119support = self._binomial.enumerate_support(expand=False)[1:]120binomial_probs = torch.exp(self._binomial.log_prob(support))121weights = torch.lgamma(support + 1)122term2 = (binomial_probs * weights).sum([0, -1])123
124return term1 + term2125
126def log_prob(self, value):127if self._validate_args:128self._validate_sample(value)129logits, value = broadcast_all(self.logits, value)130logits = logits.clone(memory_format=torch.contiguous_format)131log_factorial_n = torch.lgamma(value.sum(-1) + 1)132log_factorial_xs = torch.lgamma(value + 1).sum(-1)133logits[(value == 0) & (logits == -inf)] = 0134log_powers = (logits * value).sum(-1)135return log_factorial_n - log_factorial_xs + log_powers136