crf

crf module

class mindnlp.modules.crf.CRF(num_tags: int, batch_first: bool = False, reduction: str = 'sum')[source]

Bases: Cell

Conditional random field.

This module implements a conditional random field [LMP01]. The forward computation of this class computes the log likelihood of the given sequence of tags and emission score tensor. This class also has ~CRF.decode method which finds the best tag sequence given an emission score tensor using Viterbi algorithm.

Parameters:
  • num_tags – Number of tags.

  • batch_first – Whether the first dimension corresponds to the size of a minibatch.

  • reduction – Specifies the reduction to apply to the output: none|sum|mean|token_mean. none: no reduction will be applied. sum: the output will be summed over batches. mean: the output will be averaged over batches. token_mean: the output will be averaged over tokens.

start_transitions

Start transition score tensor of size (num_tags,).

Type:

~Parameter

end_transitions

End transition score tensor of size (num_tags,).

Type:

~Parameter

transitions

Transition score tensor of size (num_tags, num_tags).

Type:

~Parameter

[LMP01]

Lafferty, J., McCallum, A., Pereira, F. (2001). “Conditional random fields: Probabilistic models for segmenting and labeling sequence data”. Proc. 18th International Conf. on Machine Learning. Morgan Kaufmann. pp. 282–289.

construct(emissions, tags=None, seq_length=None)[source]

Defines the computation to be performed. This method must be overridden by all subclasses.

Note

It is not supported currently that inputs contain both tuple and non-tuple types at same time.

Parameters:
  • args (tuple) – Tuple of variable parameters.

  • kwargs (dict) – Dictionary of variable keyword parameters.

Returns:

Tensor, returns the computed result.

static post_decode(score, history, seq_length)[source]

Trace back the best tag sequence based on the score and history tensors.

mindnlp.modules.crf.sequence_mask(seq_length, max_length, batch_first=False)[source]

generate mask matrix by seq_length