torcheval.metrics.functional.perplexity¶
- torcheval.metrics.functional.perplexity(input: Tensor, target: Tensor, ignore_index: int | None = None) Tensor ¶
困惑度衡量模型预测样本数据的程度。其计算公式为
困惑度 = exp (负对数似然之和 / 标记数量)
其类版本为
torcheval.metrics.text.Perplexity
。- 参数:
**input** (Tensor) – 每个标记的预测未归一化分数(即 logits),形状为 (n_samples, seq_len, vocab_size)
**target** (Tensor) – 地真词典索引张量,形状为 (n_samples, seq_len)。
**ignore_index** (Tensor) – 如果指定,则计算困惑度时将忽略带有 'ignore_index' 的目标类。默认值为 None。
- 返回值:
输入和目标的困惑度。
- 返回类型:
(Tensor)
示例
>>> import torch >>> from torcheval.metrics.functional.text import perplexity
>>> input = torch.tensor([[[0.3659, 0.7025, 0.3104], [0.0097, 0.6577, 0.1947]]]) >>> target = torch.tensor([[2, 1]]) >>> perplexity(input, target) tensor(2.7593, dtype=torch.float64)
>>> input = torch.tensor([[[0.3, 0.7, 0.3, 0.1], [0.5, 0.4, 0.1, 0.4],[0.1, 0.1, 0.2, 0.5]], [[0.1, 0.6, 0.1, 0.5], [0.3, 0.7, 0.3, 0.4], [0.3, 0.7, 0.3, 0.4]]]) >>> target = torch.tensor([[2, 1, 3], [1, 0, 1]]) >>> perplexity(input, target) tensor(3.6216, dtype=torch.float64)
>>> input = torch.tensor([[[0.3659, 0.7025, 0.3104], [0.0097, 0.6577, 0.1947]]]) >>> target = torch.tensor([[2, 1]]) >>> perplexity(input, target, ignore_index = 1) tensor(3.5372, dtype=torch.float64)