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

torch.distributions.multinomial 源代码

# mypy: 允许未类型化定义
导入 火炬
from 火炬 导入 无穷, 张量
from torch.distributions 导入 离散型随机变量分布, 约束
from torch.distributions.binomial 导入 二项式
from torch.distributions.distribution 导入 分布
from torch.distributions.utils 导入 广播全部


全部 = [多项式]


[文档] 多项分布(分发): 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` 和参数化的多项分布 either :attr:`probs` 或 :attr:`logits`(但不能同时使用)。最内层维度 `:attr:`probs` 在类别上索引。所有其他维度在批次上索引。 请注意:如果只使用 :meth:`log_prob`,则无需指定 :attr:`total_count` 被称为(见以下示例) .. 注意:: `probs` 参数必须是非负的、有限的,并且总和不为零, 它将在最后一个维度上归一化,使其总和为 1。:attr:`probs` 将返回这个归一化值。 `logits` 参数将被解释为未归一化的对数概率 因此可以是任何实数。它同样会被归一化,以便 结果概率在最后一个维度上求和为 1。:attr:`logits` 将返回这个归一化值。 - :meth:`sample` 需要所有参数和样本共享单个 `total_count` 参数。 - :meth:`log_prob` 允许每个参数有不同的 `total_count` 样本。 示例:: >>> # xdoctest: +SKIP("FIXME: 发现无效值") >>> m = Multinomial(100, torch.tensor([ 1., 1., 1., 1.])) >>> x = m.sample() # 等概率为 0, 1, 2, 3 tensor([21., 24., 30., 25.]) >>> Multinomial(probs=torch.tensor([1., 1., 1., 1.])).log_prob(x) tensor([-4.1338]) 参数: total_count (int): number of trials probs (Tensor):事件概率 logits (Tensor):事件对数概率(未归一化) """ 约束参数 = {"概率": 约束.单形, "logits": 约束.实向量} 总数: 整型 @property def 均值(self) 翻译 张量: 返回 self.概率 * self.总数 @property def 方差(self) 翻译 张量: 返回 self.总数 * self.概率 * (1 - self.概率) def __init__(self, 总数=1, 概率=, logits=, 验证参数=): 如果 不是 isinstance(总数, int): 提升 不支持的操作异常("非均匀总计数不支持") self.总数 = 总数 self._分类 = 离散型随机变量分布(概率=概率, logits=logits) self._二项式 = 二项分布(总数=总数, 概率=self.概率) 批量形状 = self._分类.批量形状 事件形状 = self._分类.参数形状[-1] 超级().__init__(批量形状, event_shape, 验证参数=验证参数)
[文档] def expand(self, batch_shape, _instance=None): new = self._get_checked_instance(Multinomial, _instance) batch_shape = torch.Size(batch_shape) new.total_count = self.total_count new._categorical = self._categorical.expand(batch_shape) super(Multinomial, new).__init__( batch_shape, self.event_shape, validate_args=False ) new._validate_args = self._validate_args return new
def
(self, *参数, **kwargs): 返回 self._categorical.(*参数, **kwargs) @constraints.依赖属性(是否离散=, 事件维度=1) def 支持(self): 返回 约束.多项式(self.总数) @property def logits(self) 翻译 张量: 返回 self._分类.logits @property def 概率(self) 翻译 张量: 返回 self._分类.概率 @property def 参数形状(self) 翻译 火炬.尺寸: 返回 self._分类.参数形状
[文档] def sample(self, sample_shape=torch.Size()): sample_shape = torch.Size(sample_shape) samples = self._categorical.sample( torch.Size((self.total_count,)) + sample_shape ) # samples.shape 是(total_count, sample_shape, batch_shape),需要将其改为 # (sample_shape, batch_shape, total_count) shifted_idx = list(range(samples.dim())) shifted_idx.append(shifted_idx.pop(0)) samples = samples.permute(*shifted_idx) counts = samples.new(self._extended_shape(sample_shape)).zero_() counts.scatter_add_(-1, samples, torch.ones_like(samples)) 返回 counts.type_as(self.probs)
[文档] def entropy(self): n = torch.tensor(self.total_count) cat_entropy = self._categorical.entropy() term1 = n * cat_entropy - torch.lgamma(n + 1) support = self._binomial.enumerate_support(expand=False)[1:] binomial_probs = torch.exp(self._binomial.log_prob(support)) weights = torch.lgamma(support + 1) term2 = (binomial_probs * weights).sum([0, -1]) return term1 + term2
[文档] def log_prob(self, value): if self._validate_args: self._validate_sample(value) logits, value = broadcast_all(self.logits, value) logits = logits.clone(memory_format=torch.contiguous_format) log_factorial_n = torch.lgamma(value.sum(-1) + 1) log_factorial_xs = torch.lgamma(value + 1).sum(-1) logits[(value == 0) & (logits == -inf)] = 0 log_powers = (logits * value).sum(-1) return log_factorial_n - log_factorial_xs + log_powers

© 版权所有 PyTorch 贡献者。

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

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源