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 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.

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

update(ce, word_num=None)[source]

Update states for metric

Inputs of update is the outputs of Metric.compute, if compute is not defined, the inputs of update will be flatten arguments of output of mode and label from data: update(output1, output2, ..., label1, label2,...)

see Metric.compute

reset()[source]

Reset states and result

accumulate()[source]

Accumulates statistics, computes and returns the metric value

name()[source]

Returns metric name