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

torch.distributions.geometric 的源代码

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


全部 = [几何分布]


[文档] 几何的(分发): 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:`probs`参数化的几何分布 其中 :attr:`probs` 是伯努利试验成功的概率。 .. math:: P(X=k) = (1-p)^{k} p, k = 0, 1, ... .. 注意:: func:`torch.distributions.geometric.Geometric` 第 (k+1) 次试验是第一次成功 因此在 :math:`\{0, 1, \ldots\}` 中抽取样本。 第 k 次试验首次成功,因此从集合 {1, 2, ...} 中抽取样本。 示例:: >>> # xdoctest: +IGNORE_WANT("非确定性") >>> m = Geometric(torch.tensor([0.3])) >>> m.sample() # 基础伯努利分布有 30% 的概率为 1;70% 的概率为 0 tensor([ 2.]) 参数: probs (数字,张量):采样 `1` 的概率。必须在范围 (0, 1] 内 logits (数字,张量):采样 `1` 的对数几率。 """ 约束参数 = {"概率": 约束.单位区间, "logits": 约束.真实} 支持 = 约束.非负整数 定义 __init__(自我, 概率=, logits=, 验证参数=): 如果 (概率 is ) == (logits is ): 抛出 值错误( "必须指定 `probs` 或 `logits` 中的一个,但不能同时指定。" ) 如果 概率 is 不是 : (自我.概率,) = 广播全部(概率) else: (自我.logits,) = 广播全部(logits) probs_or_logits = 概率 如果 概率 is 不是 否则 logits 如果 isinstance(probs_or_logits, ): 批量形状 = 火炬.尺寸() else: 批量形状 = 概率或对数.大小() 超级().__init__(批量形状, 验证参数=验证参数) 如果 自我.验证参数 概率 is 不是 : 在单位区间之外添加额外的检查 = 自我.概率 有效的 = > 0 如果 不是 有效的.所有(): 无效值 = .数据[~有效的] 抛出 值错误( "期望参数概率 " f“(”{类型().__name__}形状的{元组(.形状)}) f分布的{表示(自我)} " f"要保持积极但发现无效值:"输入文本翻译为简体中文为:\n{invalid_value}" )
[文档] def expand(self, batch_shape, _instance=None): 新实例 = self._get_checked_instance(Geometric, _instance) batch_shape = torch.Size(batch_shape) if "probs" in self.__dict__: new.probs = self.probs.expand(batch_shape) if "logits" in self.__dict__: new.logits = self.logits.expand(batch_shape) super(Geometric, new).__init__(batch_shape, validate_args=False) new._validate_args = self._validate_args return new
@property 定义
均值(自我) 翻译 张量: 返回 1.0 / 自我.概率 - 1.0 @property 定义 模式(自我) 翻译 张量: 返回 火炬.等于零的(自我.概率) @property 定义 方差(自我) 翻译 张量: 返回 (1.0 / 自我.概率 - 1.0) / 自我.概率 @lazy_property 定义 logits(自我) 翻译 张量: 返回 概率转对数概率(自我.概率, 是否二进制=) @lazy_property 定义 概率(自我) 翻译 张量: 返回 logits 转概率(自我.logits, 是否二进制=)
[文档] def sample(self, sample_shape=torch.Size()): shape = self._extended_shape(sample_shape) tiny = torch.finfo(self.probs.dtype).tiny with torch.no_grad(): if torch._C._get_tracing_state(): # [JIT WORKAROUND] 缺少对 .uniform_() 的支持 u = torch.rand(shape, dtype=self.probs.dtype, device=self.probs.device) u = u.clamp(min=tiny) else: u = self.probs.new(shape).uniform_(tiny, 1) return (u.log() / (-self.probs).log1p()).floor()
[文档] def log_prob(self, value): if self._validate_args: self._validate_sample(value) value, probs = broadcast_all(value, self.probs) probs = probs.clone(memory_format=torch.contiguous_format) probs[(probs == 1) & (value == 0)] = 0 return value * (-probs).log1p() + self.probs.log()
[docs] def entropy(self): return ( binary_cross_entropy_with_logits(self.logits, self.probs, reduction="none") / self.probs )

© 版权所有 PyTorch 贡献者。

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

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源