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

torch.distributions.negative_binomial 的源代码

# mypy: 允许未类型化定义
导入 火炬
导入 torch.nn.functional  F
from 火炬 导入 张量
from torch.distributions 导入 约束
from torch.distributions.distribution 导入 分布
from torch.distributions.gamma 导入 伽马分布
from torch.distributions.utils 导入 (
    广播全部,
    懒属性,
    logits 转概率,
    概率转对数概率,
)


全部 = [负二项式]


[文档] 负二项分布(分发): r```python # 假设输入文本为: input_text = """Immersive Translate""" # 翻译函数(此处仅为示例,实际翻译功能需要调用真实的翻译 API) def translate_to_simplified_chinese(text): # 这里应该调用真实的翻译 API 进行翻译 # 由于示例中不使用真实的 API,以下为模拟翻译结果 return text # 假设翻译结果与原文相同 # 输出翻译结果 translated_text = translate_to_simplified_chinese(input_text) print(translated_text) ``` 输出: ``` Immersive Translate ``` 创建一个负二项式分布,即分布 独立且相同的伯努利试验中成功次数的 在达到:attr:`total_count`次失败之前。每次伯努利试验成功的概率是:attr:`probs`。 total_count(浮点数或张量):非负的负伯努利 参数: total_count(浮点数或张量):非负的负伯努利试验次数。 尝试停止,尽管分布对于实际情况仍然有效 有价值的计数 probs (Tensor):成功事件在半开区间[0, 1)中的概率 logits (Tensor):成功概率的对数优势 """ 约束参数 = { 总数: 约束.大于等于(0), "概率": 约束.半开区间(0.0, 1.0), "logits": 约束.真实, } 支持 = 约束.非负整数 def __init__(self, 总数, 概率=, logits=, 验证参数=): 如果 (概率 is ) == (logits is ): 提升 ValueError( "必须指定 `probs` 或 `logits` 中的一个,但不能同时指定。" ) 如果 概率 is 不是 : ( self.总数, self.概率, ) = 广播全部(总数, 概率) self.总数 = self.总数.转换为类型(self.概率) else: ( self.总数, self.logits, ) = 广播全部(总数, logits) self.总数 = self.总数.转换为类型(self.logits) self._参数 = self.概率 如果 概率 is 不是 否则 self.logits 批量形状 = self._param.大小() 超级().__init__(批量形状, 验证参数=验证参数)
[文档] def expand(self, batch_shape, _instance=None): new = self._get_checked_instance(NegativeBinomial, _instance) batch_shape = torch.Size(batch_shape) new.total_count = self.total_count.expand(batch_shape) 如果 "probs" 在 self.__dict__ 中: new.probs = self.probs.expand(batch_shape) new._param = new.probs 如果 "logits" 在 self.__dict__ 中: new.logits = self.logits.expand(batch_shape) new._param = new.logits super(NegativeBinomial, new).__init__(batch_shape, validate_args=False) new._validate_args = self._validate_args 返回新的
def
(self, *参数, **kwargs): 返回 self._param.(*参数, **kwargs) @property def 均值(self) 翻译 张量: 返回 self.总数 * 火炬.exp(self.logits) @property def 模式(self) 翻译 张量: 返回 ((self.总数 - 1) * self.logits.exp()).向下取整().卡钳(最小值=0.0) @property def 方差(self) 翻译 张量: 返回 self.平均值 / 火炬.Sigmoid 函数(-self.logits) @lazy_property def logits(self) 翻译 张量: 返回 概率转对数概率(self.概率, 是否二进制=) @lazy_property def 概率(self) 翻译 张量: 返回 logits 转概率(self.logits, 是否二进制=) @property def 参数形状(self) 翻译 火炬.尺寸: 返回 self._param.大小() @lazy_property def _伽马(self) 翻译 伽马: # 注意我们避免验证,因为 self.total_count 可以是零。 返回 伽马( 浓度=self.总数, rate=火炬.exp(-self.logits), 验证参数=错误, )
[文档] def sample(self, sample_shape=torch.Size()): with torch.no_grad(): rate = self._gamma.sample(sample_shape=sample_shape) return torch.poisson(rate)
[文档] def log_prob(self, value): if self._validate_args: self._validate_sample(value) log_unnormalized_prob = self.total_count * F.logsigmoid( -self.logits ) + value * F.logsigmoid(self.logits) log_normalization = ( -torch.lgamma(self.total_count + value) + torch.lgamma(1.0 + value) + torch.lgamma(self.total_count) ) # 当 self.total_count 等于 0 且值等于 0 时,概率为 1,但 # lgamma(0) 是无穷大。使用函数单独处理此情况。 那不会修改张量以允许 Jit 编译。 log_normalization = log_normalization.masked_fill( self.total_count + value == 0.0, 0.0 ) 返回 log_unnormalized_prob - log_normalization

© 版权所有 PyTorch 贡献者。

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

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源