mindformers.core.PerplexityMetric
- class mindformers.core.PerplexityMetric[源代码]
困惑度定义为模型对测试集中每个词分配的负对数概率的指数平均值。对于一个词序列
,困惑度 (PP) 可以表示为:其中,
表示该序列在模型下的概率。在实际应用中,困惑度可以重写为:
该公式表明,较低的困惑度意味着语言模型性能更好,因为这表示模型对实际的词序赋予了更高的概率。
样例:
>>> import numpy as np >>> from mindspore import Tensor >>> from mindformers.core.metric.metric import PerplexityMetric >>> x = Tensor(np.array([[[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]])) >>> y = Tensor(np.array([[1, 0, 1]])) >>> mask = Tensor(np.array([[1, 1, 1]])) >>> metric = PerplexityMetric() >>> metric.clear() >>> metric.update(x, y, mask) >>> perplexity = metric.eval() >>> print(perplexity) 'loss': 0.8262470960617065, 'PPL': 2.284728265028813}