9 Reinterprets some of the batch dims of a distribution as event dims. 11 This is mainly useful for changing the shape of the result of 12 :meth:`log_prob`. For example to create a diagonal Normal distribution with 13 the same shape as a Multivariate Normal distribution (so they are 14 interchangeable), you can:: 16 >>> loc = torch.zeros(3) 17 >>> scale = torch.ones(3) 18 >>> mvn = MultivariateNormal(loc, scale_tril=torch.diag(scale)) 19 >>> [mvn.batch_shape, mvn.event_shape] 20 [torch.Size(()), torch.Size((3,))] 21 >>> normal = Normal(loc, scale) 22 >>> [normal.batch_shape, normal.event_shape] 23 [torch.Size((3,)), torch.Size(())] 24 >>> diagn = Independent(normal, 1) 25 >>> [diagn.batch_shape, diagn.event_shape] 26 [torch.Size(()), torch.Size((3,))] 29 base_distribution (torch.distributions.distribution.Distribution): a 31 reinterpreted_batch_ndims (int): the number of batch dims to 32 reinterpret as event dims 36 def __init__(self, base_distribution, reinterpreted_batch_ndims, validate_args=None):
37 if reinterpreted_batch_ndims > len(base_distribution.batch_shape):
38 raise ValueError(
"Expected reinterpreted_batch_ndims <= len(base_distribution.batch_shape), " 39 "actual {} vs {}".format(reinterpreted_batch_ndims,
40 len(base_distribution.batch_shape)))
41 shape = base_distribution.batch_shape + base_distribution.event_shape
42 event_dim = reinterpreted_batch_ndims + len(base_distribution.event_shape)
43 batch_shape = shape[:len(shape) - event_dim]
44 event_shape = shape[len(shape) - event_dim:]
47 super(Independent, self).__init__(batch_shape, event_shape, validate_args=validate_args)
49 def expand(self, batch_shape, _instance=None):
51 batch_shape = torch.Size(batch_shape)
52 new.base_dist = self.base_dist.expand(batch_shape +
55 super(Independent, new).__init__(batch_shape, self.
event_shape, validate_args=
False)
60 def has_rsample(self):
61 return self.base_dist.has_rsample
64 def has_enumerate_support(self):
67 return self.base_dist.has_enumerate_support
69 @constraints.dependent_property
71 return self.base_dist.support
75 return self.base_dist.mean
79 return self.base_dist.variance
81 def sample(self, sample_shape=torch.Size()):
82 return self.base_dist.sample(sample_shape)
84 def rsample(self, sample_shape=torch.Size()):
85 return self.base_dist.rsample(sample_shape)
87 def log_prob(self, value):
88 log_prob = self.base_dist.log_prob(value)
92 entropy = self.base_dist.entropy()
95 def enumerate_support(self, expand=True):
97 raise NotImplementedError(
"Enumeration over cartesian product is not implemented")
98 return self.base_dist.enumerate_support(expand=expand)
def _get_checked_instance(self, cls, _instance=None)
reinterpreted_batch_ndims