1 from numbers
import Number
11 Generates uniformly distributed random samples from the half-open interval 16 >>> m = Uniform(torch.tensor([0.0]), torch.tensor([5.0])) 17 >>> m.sample() # uniformly distributed in the range [0.0, 5.0) 21 low (float or Tensor): lower range (inclusive). 22 high (float or Tensor): upper range (exclusive). 25 arg_constraints = {
'low': constraints.dependent,
'high': constraints.dependent}
30 return (self.
high + self.low) / 2
34 return (self.
high - self.low) / 12**0.5
38 return (self.
high - self.low).pow(2) / 12
40 def __init__(self, low, high, validate_args=None):
41 self.low, self.
high = broadcast_all(low, high)
43 if isinstance(low, Number)
and isinstance(high, Number):
44 batch_shape = torch.Size()
46 batch_shape = self.low.size()
47 super(Uniform, self).__init__(batch_shape, validate_args=validate_args)
50 raise ValueError(
"Uniform is not defined when low>= high")
52 def expand(self, batch_shape, _instance=None):
54 batch_shape = torch.Size(batch_shape)
55 new.low = self.low.expand(batch_shape)
56 new.high = self.high.expand(batch_shape)
57 super(Uniform, new).__init__(batch_shape, validate_args=
False)
61 @constraints.dependent_property
63 return constraints.interval(self.low, self.
high)
65 def rsample(self, sample_shape=torch.Size()):
67 rand = torch.rand(shape, dtype=self.low.dtype, device=self.low.device)
68 return self.low + rand * (self.
high - self.low)
70 def log_prob(self, value):
73 lb = value.ge(self.low).type_as(self.low)
74 ub = value.lt(self.
high).type_as(self.low)
75 return torch.log(lb.mul(ub)) - torch.log(self.
high - self.low)
80 result = (value - self.low) / (self.
high - self.low)
81 return result.clamp(min=0, max=1)
83 def icdf(self, value):
86 result = value * (self.
high - self.low) + self.low
90 return torch.log(self.
high - self.low)
def _get_checked_instance(self, cls, _instance=None)
def _extended_shape(self, sample_shape=torch.Size())
def _validate_sample(self, value)