• 文档 >
  • 模块代码 >
  • torch >
  • torch.distributions.uniform
快捷键

源代码 for torch.distributions.uniform

# mypy: 允许未类型化定义
导入 火炬
from 火炬 导入 纳尼, 张量
from torch.distributions 导入 约束
from torch.distributions.distribution 导入 分布
from torch.distributions.utils 导入 广播全部
from torch.types 导入 , _大小


全部 = [统一]


[文档] 均匀分布(分发): r"" 生成半开区间的均匀分布随机样本 [低, 高) 示例:: >>> m = Uniform(torch.tensor([0.0]), torch.tensor([5.0])) >>> m.sample() # 均匀分布在 [0.0, 5.0) 范围内 >>> # xdoctest: +SKIP tensor([ 2.3418]) 参数: low (float 或 Tensor): 下限范围(包含)。 高(浮点数或张量):上界(不包括)。 "" # TODO 允许(loc,scale)参数化以允许独立约束。 约束参数 = { : 约束.依赖的(是否离散=错误, 事件维度=0), : 约束.依赖的(是否离散=错误, 事件维度=0), } has_rsample = 真实 @property def 均值(self) 翻译 张量: 返回 (self. + self.) / 2 @property def 模式(self) 翻译 张量: 返回 纳尼 * self. @property def 标准差(self) 翻译 张量: 返回 (self. - self.) / 12**0.5 @property def 方差(self) 翻译 张量: 返回 (self. - self.).pow(2) / 12 def __init__(self, , , 验证参数=): self., self. = 广播全部(, ) 如果 isinstance(, ) isinstance(, ): 批量形状 = 火炬.尺寸() else: 批量形状 = self..尺寸() 超级().__init__(批量形状, 验证参数=验证参数) 如果 self.验证参数 不是 火炬.lt(self., self.).所有(): 提升 ValueError("当 low>= high 时,均匀未定义")
[文档] def expand(self, batch_shape, _instance=None): new = self._get_checked_instance(Uniform, _instance) batch_shape = torch.Size(batch_shape) new.low = self.low.expand(batch_shape) new.high = self.high.expand(batch_shape) super(Uniform, new).__init__(batch_shape, validate_args=False) new._validate_args = self._validate_args return new
@constraints.依赖属性
(是否离散=错误, 事件维度=0) def 支持(self): 返回 约束.区间(self., self.)
[文档] def rsample(self, sample_shape: _size = torch.Size()) -> Tensor: shape = self._extended_shape(sample_shape) rand = torch.rand(shape, dtype=self.low.dtype, device=self.low.device) return self.low + rand * (self.high - self.low)
[文档] def log_prob(self, value): if self._validate_args: self._validate_sample(value) lb = self.low.le(value).type_as(self.low) ub = self.high.gt(value).type_as(self.low) return torch.log(lb.mul(ub)) - torch.log(self.high - self.low)
[文档] def cdf(self, value): if self._validate_args: self._validate_sample(value) result = (值 - self.low) / (self.high - self.low) return result.clamp(min=0, max=1)
[文档] def icdf(self, value): result = value * (self.high - self.low) + self.low 返回结果
[文档] def 熵(self): return torch.log(self.high - self.low)

© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源,获取您的疑问解答

查看资源