[docs]classIndependent(Distribution):r""" Reinterprets some of the batch dims of a distribution as event dims. This is mainly useful for changing the shape of the result of :meth:`log_prob`. For example to create a diagonal Normal distribution with the same shape as a Multivariate Normal distribution (so they are interchangeable), you can:: >>> from torch.distributions.multivariate_normal import MultivariateNormal >>> from torch.distributions.normal import Normal >>> loc = torch.zeros(3) >>> scale = torch.ones(3) >>> mvn = MultivariateNormal(loc, scale_tril=torch.diag(scale)) >>> [mvn.batch_shape, mvn.event_shape] [torch.Size([]), torch.Size([3])] >>> normal = Normal(loc, scale) >>> [normal.batch_shape, normal.event_shape] [torch.Size([3]), torch.Size([])] >>> diagn = Independent(normal, 1) >>> [diagn.batch_shape, diagn.event_shape] [torch.Size([]), torch.Size([3])] Args: base_distribution (torch.distributions.distribution.Distribution): a base distribution reinterpreted_batch_ndims (int): the number of batch dims to reinterpret as event dims """arg_constraints:Dict[str,constraints.Constraint]={}def__init__(self,base_distribution,reinterpreted_batch_ndims,validate_args=None):ifreinterpreted_batch_ndims>len(base_distribution.batch_shape):raiseValueError("Expected reinterpreted_batch_ndims <= len(base_distribution.batch_shape), "f"actual {reinterpreted_batch_ndims} vs {len(base_distribution.batch_shape)}")shape=base_distribution.batch_shape+base_distribution.event_shapeevent_dim=reinterpreted_batch_ndims+len(base_distribution.event_shape)batch_shape=shape[:len(shape)-event_dim]event_shape=shape[len(shape)-event_dim:]self.base_dist=base_distributionself.reinterpreted_batch_ndims=reinterpreted_batch_ndimssuper().__init__(batch_shape,event_shape,validate_args=validate_args)
[docs]defenumerate_support(self,expand=True):ifself.reinterpreted_batch_ndims>0:raiseNotImplementedError("Enumeration over cartesian product is not implemented")returnself.base_dist.enumerate_support(expand=expand)
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.