2 from numbers
import Number
12 Creates a normal (also called Gaussian) distribution parameterized by 13 :attr:`loc` and :attr:`scale`. 17 >>> m = Normal(torch.tensor([0.0]), torch.tensor([1.0])) 18 >>> m.sample() # normally distributed with loc=0 and scale=1 22 loc (float or Tensor): mean of the distribution (often referred to as mu) 23 scale (float or Tensor): standard deviation of the distribution 24 (often referred to as sigma) 26 arg_constraints = {
'loc': constraints.real,
'scale': constraints.positive}
27 support = constraints.real
29 _mean_carrier_measure = 0
41 return self.stddev.pow(2)
43 def __init__(self, loc, scale, validate_args=None):
44 self.loc, self.
scale = broadcast_all(loc, scale)
45 if isinstance(loc, Number)
and isinstance(scale, Number):
46 batch_shape = torch.Size()
48 batch_shape = self.loc.size()
49 super(Normal, self).__init__(batch_shape, validate_args=validate_args)
51 def expand(self, batch_shape, _instance=None):
53 batch_shape = torch.Size(batch_shape)
54 new.loc = self.loc.expand(batch_shape)
55 new.scale = self.scale.expand(batch_shape)
56 super(Normal, new).__init__(batch_shape, validate_args=
False)
60 def sample(self, sample_shape=torch.Size()):
63 return torch.normal(self.loc.expand(shape), self.scale.expand(shape))
65 def rsample(self, sample_shape=torch.Size()):
67 eps = _standard_normal(shape, dtype=self.loc.dtype, device=self.loc.device)
68 return self.loc + eps * self.
scale 70 def log_prob(self, value):
74 var = (self.
scale ** 2)
75 log_scale = math.log(self.
scale)
if isinstance(self.
scale, Number)
else self.scale.log()
76 return -((value - self.loc) ** 2) / (2 * var) - log_scale - math.log(math.sqrt(2 * math.pi))
81 return 0.5 * (1 + torch.erf((value - self.loc) * self.scale.reciprocal() / math.sqrt(2)))
83 def icdf(self, value):
86 return self.loc + self.
scale * torch.erfinv(2 * value - 1) * math.sqrt(2)
89 return 0.5 + 0.5 * math.log(2 * math.pi) + torch.log(self.
scale)
92 def _natural_params(self):
93 return (self.loc / self.scale.pow(2), -0.5 * self.scale.pow(2).reciprocal())
95 def _log_normalizer(self, x, y):
96 return -0.25 * x.pow(2) / y + 0.5 * torch.log(-math.pi / y)
def _get_checked_instance(self, cls, _instance=None)
def _extended_shape(self, sample_shape=torch.Size())
def _validate_sample(self, value)