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

torch.distributions.gamma 的源代码

# mypy: 允许未类型化定义
导入 火炬
from 火炬 导入 张量
from torch.distributions 导入 约束
from torch.distributions.exp_family 导入 指数族
from torch.distributions.utils 导入 广播全部
from torch.types 导入 数, _大小


全部 = [伽马]


定义 _standard_gamma(浓度):
    返回 torch._标准伽马(浓度)


[文档]类 伽马(指数族): r""" 创建一个由形状 :attr:`concentration` 和 :attr:`rate` 参数化的伽马分布。 示例:: >>> # xdoctest: +IGNORE_WANT("非确定性") >>> m = Gamma(torch.tensor([1.0]), torch.tensor([1.0])) >>> m.sample() # 伽马分布,浓度=1,速率=1 tensor([ 0.1046]) 参数: 浓度(浮点数或张量):分布的形状参数 (通常称为 alpha) 比率(浮点数或张量):分布的比率参数 (通常称为贝塔),比率 = 1 / 尺度 """ 约束参数 = { 浓度: 约束.正的, 率: 约束.正的, } 支持 = 约束.非负 has_rsample = 真实 _平均载波测量 = 0 @property 定义 均值(我) -> 张量: 返回 我.注意力 / 我.速率 @property 定义 模式(我) -> 张量: 返回 ((我.注意力 - 1) / 我.率).卡钳(最小值=0) @property 定义 方差(我) -> 张量: 返回 我.注意力 / 我.率.pow(2) 定义 __init__(我, 浓度, 率, 验证参数=无): 我.浓度, 我.rate = 广播全部(浓度, 率) if isinstance(浓度, 数) 和 isinstance(率, 数): 批量形状 = torch.尺寸() else: 批量形状 = 我.浓度.大小() 超级().__init__(批量形状, 验证参数=验证参数)
[文档] def expand(self, batch_shape, _instance=None): new = self._get_checked_instance(Gamma, _instance) batch_shape = torch.Size(batch_shape) new.concentration = self.concentration.expand(batch_shape) new.rate = self.rate.expand(batch_shape) super(Gamma, new).__init__(batch_shape, validate_args=False) new._validate_args = self._validate_args 返回新的
[文档] def rsample(self, sample_shape: _size = torch.Size()) -> Tensor: shape = self._extended_shape(sample_shape) value = _standard_gamma(self.concentration.expand(shape)) / self.rate.expand( 形状 ) value.detach().clamp_() min=torch.finfo(value.dtype).tiny # 不记录在自动求导图中 返回值
[文档] def log_prob(self, value): value = torch.as_tensor(value, dtype=self.rate.dtype, device=self.rate.device) if self._validate_args: self._validate_sample(value) return ( torch.xlogy(self.concentration, self.rate) + torch.xlogy(self.concentration - 1, value) - self.rate * value - torch.lgamma(self.concentration) )
[文档] def 熵(self): return ( self.浓度 - torch.log(self.速率) + torch.lgamma(self.concentration) + (1.0 - self.concentration) * torch.digamma(self.concentration) )
@property 定义
自然参数(我) -> 元组[张量, 张量] 返回 (我.concentration - 1, -我.率) 定义 对数归一化器(我, x, y): 返回 torch.lgamma(x + 1) + (x + 1) * torch.日志(-y.相互())
[文档] def cdf(self, value): if self._validate_args: self._validate_sample(value) return torch.special.gammainc(self.concentration, self.rate * value)

© 版权所有 PyTorch 贡献者。

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

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源