本文整理汇总了Python中torch.distributions.TransformedDistribution方法的典型用法代码示例。如果您正苦于以下问题:Python distributions.TransformedDistribution方法的具体用法?Python distributions.TransformedDistribution怎么用?Python distributions.TransformedDistribution使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.distributions
的用法示例。
在下文中一共展示了distributions.TransformedDistribution方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _iterate_distribution
# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import TransformedDistribution [as 别名]
def _iterate_distribution(d: Distribution) -> Tuple[Distribution, ...]:
"""
Helper method for iterating over distributions.
:param d: The distribution
"""
res = tuple()
if not isinstance(d, TransformedDistribution):
res += tuple(_find_types(d, torch.Tensor).values())
for sd in _find_types(d, Distribution).values():
res += _iterate_distribution(sd)
else:
res += _iterate_distribution(d.base_dist)
for t in d.transforms:
res += tuple(_find_types(t, torch.Tensor).values())
return res
示例2: transformed_dist
# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import TransformedDistribution [as 别名]
def transformed_dist(self):
"""
Returns the unconstrained distribution.
"""
if not self.trainable:
raise ValueError('Is not of `Distribution` instance!')
return TransformedDistribution(self._prior, [self.bijection.inv])
示例3: _define_transdist
# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import TransformedDistribution [as 别名]
def _define_transdist(loc: torch.Tensor, scale: torch.Tensor, inc_dist: Distribution, ndim: int):
loc, scale = torch.broadcast_tensors(loc, scale)
shape = loc.shape[:-ndim] if ndim > 0 else loc.shape
return TransformedDistribution(
inc_dist.expand(shape), AffineTransform(loc, scale, event_dim=ndim)
)
示例4: __init__
# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import TransformedDistribution [as 别名]
def __init__(self, dim, blocks, flow_length, final_block=None, density=None, amortized='none'):
""" Initialize normalizing flow """
super().__init__()
biject = []
self.n_params = []
# Start density (z0)
if density is None:
density = MultivariateNormal(torch.zeros(dim), torch.eye(dim))
self.base_density = density
for f in range(flow_length-1):
for b_flow in blocks:
cur_block = b_flow(dim, amortized=amortized)
self.n_params.append(cur_block.n_parameters())
biject.append(cur_block)
# Add only first block last
cur_block = blocks[0](dim, amortized=amortized)
self.n_params.append(cur_block.n_parameters())
biject.append(cur_block)
if (final_block is not None):
cur_block = final_block
self.n_params.append(cur_block.n_parameters())
biject.append(cur_block)
# Full set of transforms
self.transforms = transform.ComposeTransform(biject)
self.bijectors = nn.ModuleList(biject)
# Final density (zK) defined as transformed distribution
self.final_density = distrib.TransformedDistribution(density, self.transforms)
self.amortized = amortized
# Handle different amortizations
if amortized in ('self', 'input'):
self.amortized_seed = torch.ones(1, dim).detach()
self.amortized_params = self.parameters_network(dim, self.n_parameters())
self.log_det = []
self.dim = dim
示例5: logistic_distribution
# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import TransformedDistribution [as 别名]
def logistic_distribution(loc, log_scale):
scale = torch.exp(log_scale) + 1e-5
base_distribution = distributions.Uniform(torch.zeros_like(loc), torch.ones_like(loc))
transforms = [LogisticTransform(), distributions.AffineTransform(loc=loc, scale=scale)]
logistic = distributions.TransformedDistribution(base_distribution, transforms)
return logistic