perplexity¶
-
class
Perplexity
(name='Perplexity', *args, **kwargs)[source]¶ Bases:
paddle.metric.metrics.Metric
Perplexity is a metric used to judge how good a language model is. We can define perplexity as the inverse probability of the test set, normalised by the number of the words in the test set. Perplexity is calculated using cross entropy. It supports both padding data and no padding data.
If data is not padded, users should provide
seq_len
forMetric
initialization. If data is padded, your label should containseq_mask
, which indicates the actual length of samples.This Perplexity requires that the output of your network is prediction, label and sequence length (optional). If the Perplexity here doesn’t meet your needs, you could override the
compute
orupdate
method for calculating Perplexity.- Parameters
seq_len (int) – Sequence length of each sample, it must be provided while data is not padded. Defaults to 20.
name (str) – Name of
Metric
instance. Defaults to ‘Perplexity’.
Example
import paddle from paddlenlp.transformers import BertTokenizer from paddlenlp.metrics import Perplexity paddle.seed(2021) tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') batch_size, seq_len, vocab_size = 1, 4, tokenizer.vocab_size logits = paddle.rand([batch_size, seq_len, vocab_size]) labels= paddle.to_tensor([[1,0,1,1]]) perplexity = Perplexity() correct = perplexity.compute(logits,labels) perplexity.update(correct.numpy()) res = perplexity.accumulate() print(res) # 48263.528820122105
-
compute
(pred, label, seq_mask=None)[source]¶ Computes cross entropy loss.
- Parameters
pred (Tensor) – Predictor tensor, and its dtype is float32 or float64, and has a shape of [batch_size, sequence_length, vocab_size].
label (Tensor) – Label tensor, and its dtype is int64, and has a shape of [batch_size, sequence_length, 1] or [batch_size, sequence_length].
seq_mask (Tensor, optional) – Sequence mask tensor, and its type could be float32, float64, int32 or int64, and has a shape of [batch_size, sequence_length]. It’s used to calculate loss. Defaults to None.
- Returns
Returns tuple (
ce, word_num
) ifseq_mask
is not None. Otherwise, returns tensorce
.ce
it the cross entropy loss, its shape is [batch_size, sequence_length] and its data type should be float32.- Return type
tuple or Tensor