/
usr
/
local
/
lib64
/
python3.6
/
site-packages
/
torch
/
distributions
/
/usr/local/lib64/python3.6/site-packages/torch/distributions
mkdir
upload
Name
Size
Mode
Actions
__pycache__/
-
0755
rm
bernoulli.py
3904
0644
edit
dl
rm
beta.py
3406
0644
edit
dl
rm
binomial.py
5179
0644
edit
dl
rm
categorical.py
5488
0644
edit
dl
rm
cauchy.py
2714
0644
edit
dl
rm
chi2.py
909
0644
edit
dl
rm
constraints.py
17288
0644
edit
dl
rm
constraint_registry.py
10234
0644
edit
dl
rm
continuous_bernoulli.py
8532
0644
edit
dl
rm
dirichlet.py
3584
0644
edit
dl
rm
distribution.py
11735
0644
edit
dl
rm
exponential.py
2525
0644
edit
dl
rm
exp_family.py
2275
0644
edit
dl
rm
fishersnedecor.py
3152
0644
edit
dl
rm
gamma.py
3121
0644
edit
dl
rm
geometric.py
4266
0644
edit
dl
rm
gumbel.py
2528
0644
edit
dl
rm
half_cauchy.py
2257
0644
edit
dl
rm
half_normal.py
2058
0644
edit
dl
rm
independent.py
4361
0644
edit
dl
rm
kl.py
29998
0644
edit
dl
rm
kumaraswamy.py
2927
0644
edit
dl
rm
laplace.py
3054
0644
edit
dl
rm
lkj_cholesky.py
6124
0644
edit
dl
rm
logistic_normal.py
1983
0644
edit
dl
rm
log_normal.py
1772
0644
edit
dl
rm
lowrank_multivariate_normal.py
9930
0644
edit
dl
rm
mixture_same_family.py
8636
0644
edit
dl
rm
multinomial.py
4776
0644
edit
dl
rm
multivariate_normal.py
10548
0644
edit
dl
rm
negative_binomial.py
4091
0644
edit
dl
rm
normal.py
3351
0644
edit
dl
rm
one_hot_categorical.py
4375
0644
edit
dl
rm
pareto.py
2057
0644
edit
dl
rm
poisson.py
2066
0644
edit
dl
rm
relaxed_bernoulli.py
5360
0644
edit
dl
rm
relaxed_categorical.py
5202
0644
edit
dl
rm
studentT.py
3550
0644
edit
dl
rm
transformed_distribution.py
8270
0644
edit
dl
rm
transforms.py
38408
0644
edit
dl
rm
uniform.py
3112
0644
edit
dl
rm
utils.py
6196
0644
edit
dl
rm
von_mises.py
5091
0644
edit
dl
rm
weibull.py
2854
0644
edit
dl
rm
__init__.py
5884
0644
edit
dl
rm
Edit:
/usr/local/lib64/python3.6/site-packages/torch/distributions/transformed_distribution.py
(8270B)
import torch from torch.distributions import constraints from torch.distributions.distribution import Distribution from torch.distributions.independent import Independent from torch.distributions.transforms import ComposeTransform, Transform from torch.distributions.utils import _sum_rightmost from typing import Dict class TransformedDistribution(Distribution): r""" Extension of the Distribution class, which applies a sequence of Transforms to a base distribution. Let f be the composition of transforms applied:: X ~ BaseDistribution Y = f(X) ~ TransformedDistribution(BaseDistribution, f) log p(Y) = log p(X) + log |det (dX/dY)| Note that the ``.event_shape`` of a :class:`TransformedDistribution` is the maximum shape of its base distribution and its transforms, since transforms can introduce correlations among events. An example for the usage of :class:`TransformedDistribution` would be:: # Building a Logistic Distribution # X ~ Uniform(0, 1) # f = a + b * logit(X) # Y ~ f(X) ~ Logistic(a, b) base_distribution = Uniform(0, 1) transforms = [SigmoidTransform().inv, AffineTransform(loc=a, scale=b)] logistic = TransformedDistribution(base_distribution, transforms) For more examples, please look at the implementations of :class:`~torch.distributions.gumbel.Gumbel`, :class:`~torch.distributions.half_cauchy.HalfCauchy`, :class:`~torch.distributions.half_normal.HalfNormal`, :class:`~torch.distributions.log_normal.LogNormal`, :class:`~torch.distributions.pareto.Pareto`, :class:`~torch.distributions.weibull.Weibull`, :class:`~torch.distributions.relaxed_bernoulli.RelaxedBernoulli` and :class:`~torch.distributions.relaxed_categorical.RelaxedOneHotCategorical` """ arg_constraints: Dict[str, constraints.Constraint] = {} def __init__(self, base_distribution, transforms, validate_args=None): if isinstance(transforms, Transform): self.transforms = [transforms, ] elif isinstance(transforms, list): if not all(isinstance(t, Transform) for t in transforms): raise ValueError("transforms must be a Transform or a list of Transforms") self.transforms = transforms else: raise ValueError("transforms must be a Transform or list, but was {}".format(transforms)) # Reshape base_distribution according to transforms. base_shape = base_distribution.batch_shape + base_distribution.event_shape base_event_dim = len(base_distribution.event_shape) transform = ComposeTransform(self.transforms) domain_event_dim = transform.domain.event_dim if len(base_shape) < domain_event_dim: raise ValueError("base_distribution needs to have shape with size at least {}, but got {}." .format(domain_event_dim, base_shape)) shape = transform.forward_shape(base_shape) expanded_base_shape = transform.inverse_shape(shape) if base_shape != expanded_base_shape: base_batch_shape = expanded_base_shape[:len(expanded_base_shape) - base_event_dim] base_distribution = base_distribution.expand(base_batch_shape) reinterpreted_batch_ndims = domain_event_dim - base_event_dim if reinterpreted_batch_ndims > 0: base_distribution = Independent(base_distribution, reinterpreted_batch_ndims) self.base_dist = base_distribution # Compute shapes. event_dim = transform.codomain.event_dim + max(base_event_dim - domain_event_dim, 0) assert len(shape) >= event_dim cut = len(shape) - event_dim batch_shape = shape[:cut] event_shape = shape[cut:] super(TransformedDistribution, self).__init__(batch_shape, event_shape, validate_args=validate_args) def expand(self, batch_shape, _instance=None): new = self._get_checked_instance(TransformedDistribution, _instance) batch_shape = torch.Size(batch_shape) shape = batch_shape + self.event_shape for t in reversed(self.transforms): shape = t.inverse_shape(shape) base_batch_shape = shape[:len(shape) - len(self.base_dist.event_shape)] new.base_dist = self.base_dist.expand(base_batch_shape) new.transforms = self.transforms super(TransformedDistribution, new).__init__(batch_shape, self.event_shape, validate_args=False) new._validate_args = self._validate_args return new @constraints.dependent_property(is_discrete=False) def support(self): if not self.transforms: return self.base_dist.support support = self.transforms[-1].codomain if len(self.event_shape) > support.event_dim: support = constraints.independent(support, len(self.event_shape) - support.event_dim) return support @property def has_rsample(self): return self.base_dist.has_rsample def sample(self, sample_shape=torch.Size()): """ Generates a sample_shape shaped sample or sample_shape shaped batch of samples if the distribution parameters are batched. Samples first from base distribution and applies `transform()` for every transform in the list. """ with torch.no_grad(): x = self.base_dist.sample(sample_shape) for transform in self.transforms: x = transform(x) return x def rsample(self, sample_shape=torch.Size()): """ Generates a sample_shape shaped reparameterized sample or sample_shape shaped batch of reparameterized samples if the distribution parameters are batched. Samples first from base distribution and applies `transform()` for every transform in the list. """ x = self.base_dist.rsample(sample_shape) for transform in self.transforms: x = transform(x) return x def log_prob(self, value): """ Scores the sample by inverting the transform(s) and computing the score using the score of the base distribution and the log abs det jacobian. """ if self._validate_args: self._validate_sample(value) event_dim = len(self.event_shape) log_prob = 0.0 y = value for transform in reversed(self.transforms): x = transform.inv(y) event_dim += transform.domain.event_dim - transform.codomain.event_dim log_prob = log_prob - _sum_rightmost(transform.log_abs_det_jacobian(x, y), event_dim - transform.domain.event_dim) y = x log_prob = log_prob + _sum_rightmost(self.base_dist.log_prob(y), event_dim - len(self.base_dist.event_shape)) return log_prob def _monotonize_cdf(self, value): """ This conditionally flips ``value -> 1-value`` to ensure :meth:`cdf` is monotone increasing. """ sign = 1 for transform in self.transforms: sign = sign * transform.sign if isinstance(sign, int) and sign == 1: return value return sign * (value - 0.5) + 0.5 def cdf(self, value): """ Computes the cumulative distribution function by inverting the transform(s) and computing the score of the base distribution. """ for transform in self.transforms[::-1]: value = transform.inv(value) if self._validate_args: self.base_dist._validate_sample(value) value = self.base_dist.cdf(value) value = self._monotonize_cdf(value) return value def icdf(self, value): """ Computes the inverse cumulative distribution function using transform(s) and computing the score of the base distribution. """ value = self._monotonize_cdf(value) if self._validate_args: self.base_dist._validate_sample(value) value = self.base_dist.icdf(value) for transform in self.transforms: value = transform(value) return value
Save
cmd:
run