perplexity

class Perplexity(name='Perplexity', *args, **kwargs)[源代码]

基类:paddle.metric.metrics.Metric

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 for Metric initialization. If data is padded, your label should contain seq_mask, which indicates the actual length of samples.

This Perplexity requires that the output of your network is prediction, label and sequence length (opitonal). If the Perplexity here doesn't meet your needs, you could override the compute or update method for caculating Perplexity.

参数
  • 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'.

compute(pred, label, seq_mask=None)[源代码]

Computes cross entropy loss.

参数
  • 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.

update(ce, word_num=None)[源代码]

Updates metric states.

参数
  • ce (numpy.ndarray) -- Cross entropy loss, it's calculated by compute and converted to numpy.ndarray.

  • word_num (numpy.ndarray) -- The number of words of sequence, it's calculated by compute and converted to numpy.ndarray. Defaults to None.

reset()[源代码]

Resets all metric states.

accumulate()[源代码]

Calculates and returns the value of perplexity.

返回

Calculation results.

返回类型

perplexity

name()[源代码]

Returns name of the metric instance.

返回

The name of the metric instance.

返回类型

str