perplexity¶
-
class
Perplexity
(name='Perplexity', *args, **kwargs)[source]¶ Bases:
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
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 (opitonal). If the Perplexity here doesn’t meet your needs, you could override the
compute
orupdate
method for caculating Perplexity.- Parameters
seq_len (int) – Sequence length of each sample, it must be provided while data is not padded. Default: 20.
name (str) – Name of
Metric
instance. Default: ‘Perplexity’.
-
compute
(pred, label, seq_mask=None)[source]¶ This API is advanced usage to accelerate metric calculating, calulations from outputs of model to the states which should be updated by Metric can be defined here, where Paddle OPs is also supported. Outputs of this API will be the inputs of “Metric.update”.
If
compute
is defined, it will be called with outputs of model and labels from data as arguments, all outputs and labels will be concatenated and flatten and each filed as a separate argument as follows:compute(output1, output2, ..., label1, label2,...)
If
compute
is not defined, default behaviour is to pass input to output, so output format will be:return output1, output2, ..., label1, label2,...
see
Metric.update