pytorch
129 строк · 4.6 Кб
1import torch2from torch.distributions import constraints3from torch.distributions.categorical import Categorical4from torch.distributions.distribution import Distribution5
6__all__ = ["OneHotCategorical", "OneHotCategoricalStraightThrough"]7
8
9class OneHotCategorical(Distribution):10r"""11Creates a one-hot categorical distribution parameterized by :attr:`probs` or
12:attr:`logits`.
13
14Samples are one-hot coded vectors of size ``probs.size(-1)``.
15
16.. note:: The `probs` argument must be non-negative, finite and have a non-zero sum,
17and it will be normalized to sum to 1 along the last dimension. :attr:`probs`
18will return this normalized value.
19The `logits` argument will be interpreted as unnormalized log probabilities
20and can therefore be any real number. It will likewise be normalized so that
21the resulting probabilities sum to 1 along the last dimension. :attr:`logits`
22will return this normalized value.
23
24See also: :func:`torch.distributions.Categorical` for specifications of
25:attr:`probs` and :attr:`logits`.
26
27Example::
28
29>>> # xdoctest: +IGNORE_WANT("non-deterministic")
30>>> m = OneHotCategorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
31>>> m.sample() # equal probability of 0, 1, 2, 3
32tensor([ 0., 0., 0., 1.])
33
34Args:
35probs (Tensor): event probabilities
36logits (Tensor): event log probabilities (unnormalized)
37"""
38arg_constraints = {"probs": constraints.simplex, "logits": constraints.real_vector}39support = constraints.one_hot40has_enumerate_support = True41
42def __init__(self, probs=None, logits=None, validate_args=None):43self._categorical = Categorical(probs, logits)44batch_shape = self._categorical.batch_shape45event_shape = self._categorical.param_shape[-1:]46super().__init__(batch_shape, event_shape, validate_args=validate_args)47
48def expand(self, batch_shape, _instance=None):49new = self._get_checked_instance(OneHotCategorical, _instance)50batch_shape = torch.Size(batch_shape)51new._categorical = self._categorical.expand(batch_shape)52super(OneHotCategorical, new).__init__(53batch_shape, self.event_shape, validate_args=False54)55new._validate_args = self._validate_args56return new57
58def _new(self, *args, **kwargs):59return self._categorical._new(*args, **kwargs)60
61@property62def _param(self):63return self._categorical._param64
65@property66def probs(self):67return self._categorical.probs68
69@property70def logits(self):71return self._categorical.logits72
73@property74def mean(self):75return self._categorical.probs76
77@property78def mode(self):79probs = self._categorical.probs80mode = probs.argmax(axis=-1)81return torch.nn.functional.one_hot(mode, num_classes=probs.shape[-1]).to(probs)82
83@property84def variance(self):85return self._categorical.probs * (1 - self._categorical.probs)86
87@property88def param_shape(self):89return self._categorical.param_shape90
91def sample(self, sample_shape=torch.Size()):92sample_shape = torch.Size(sample_shape)93probs = self._categorical.probs94num_events = self._categorical._num_events95indices = self._categorical.sample(sample_shape)96return torch.nn.functional.one_hot(indices, num_events).to(probs)97
98def log_prob(self, value):99if self._validate_args:100self._validate_sample(value)101indices = value.max(-1)[1]102return self._categorical.log_prob(indices)103
104def entropy(self):105return self._categorical.entropy()106
107def enumerate_support(self, expand=True):108n = self.event_shape[0]109values = torch.eye(n, dtype=self._param.dtype, device=self._param.device)110values = values.view((n,) + (1,) * len(self.batch_shape) + (n,))111if expand:112values = values.expand((n,) + self.batch_shape + (n,))113return values114
115
116class OneHotCategoricalStraightThrough(OneHotCategorical):117r"""118Creates a reparameterizable :class:`OneHotCategorical` distribution based on the straight-
119through gradient estimator from [1].
120
121[1] Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation
122(Bengio et al, 2013)
123"""
124has_rsample = True125
126def rsample(self, sample_shape=torch.Size()):127samples = self.sample(sample_shape)128probs = self._categorical.probs # cached via @lazy_property129return samples + (probs - probs.detach())130