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

torch.distributions.normal 的源代码

# mypy: 允许未类型化定义
导入 数学

导入 火炬
from 火炬 导入 张量
from torch.distributions 导入 约束
from torch.distributions.exp_family 导入 指数族
from torch.distributions.utils 导入 标准正态, 广播全部
from torch.types 导入 , _大小


全部 = [正态]


[文档] 正态分布(指数族): r"" 创建一个正态(也称为高斯)分布,参数为 attr:`loc` 和 :attr:`scale`。 示例:: >>> # xdoctest: +IGNORE_WANT("非确定性") >>> m = Normal(torch.tensor([0.0]), torch.tensor([1.0])) >>> m.sample() # 正态分布,均值=0,标准差=1 tensor([ 0.1046]) 参数: loc (float or Tensor): 分布的均值(通常称为μ) scale (float or Tensor): 分布的标准差 (通常称为西格玛) "" 约束参数 = {"loc": 约束.真实, "缩放": 约束.正的} 支持 = 约束.真实 has_rsample = 真实 _平均载波测量 = 0 @property def 均值(self) 翻译 张量: 返回 self.定位 @property def 模式(self) 翻译 张量: 返回 self.定位 @property def 标准差(self) 翻译 张量: 返回 self.缩放 @property def 方差(self) 翻译 张量: 返回 self.标准差.pow(2) def __init__(self, 位置, 比例, 验证参数=): self.位置, self.缩放 = 广播全部(位置, 比例) 如果 isinstance(位置, ) isinstance(比例, ): 批量形状 = 火炬.尺寸() else: 批量形状 = self.位置.大小() 超级().__init__(批量形状, 验证参数=验证参数)
[文档] def expand(self, batch_shape, _instance=None): new = self._get_checked_instance(Normal, _instance) batch_shape = torch.Size(batch_shape) new.loc = self.loc.expand(batch_shape) new.scale = self.scale.expand(batch_shape) super(Normal, new).__init__(batch_shape, validate_args=False) new._validate_args = self._validate_args 返回新的
[文档] def sample(self, sample_shape=torch.Size()): shape = self._extended_shape(sample_shape) with torch.no_grad(): return torch.normal(self.loc.expand(shape), self.scale.expand(shape))
[文档] def rsample(self, sample_shape: _size = torch.Size()) shape = self._extended_shape(sample_shape) eps = _standard_normal(shape, dtype=self.loc.dtype, device=self.loc.device) return self.loc + eps * self.scale
[文档] def log_prob(self, value) if self._validate_args: self._validate_sample(value) 计算方差 var = self.scale**2 log_scale = ( math.log(self.scale) 如果 isinstance(self.scale, _Number) 否则 self.scale.log() ) 返回 ( ((值 - 自我.loc) ** 2) / (2 * 变量) 对数尺度 math.log(math.sqrt(2 * math.pi)) )
[文档] def cdf(self, value): if self._validate_args: self._validate_sample(value) return 0.5 * ( 1 + torch.erf((value - self.loc) * self.scale.reciprocal() / math.sqrt(2)) )
[文档] def icdf(self, value): return self.loc + self.scale * torch.erfinv(2 * value - 1) * math.sqrt(2)
[文档] def 熵(self): 返回 0.5 + 0.5 * math.log(2 * math.pi) + torch.log(self.scale)
@property def 自然参数
(self) 翻译 元组[张量, 张量] 返回 (self.定位 / self.比例.pow(2), -0.5 * self.比例.pow(2).相互()) def 对数归一化器(self, x, y): 返回 -0.25 * x.pow(2) / y + 0.5 * 火炬.日志(-数学.圆周率 / y)

© 版权所有 PyTorch 贡献者。

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

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源