nlp_architect.nn.torch.layers package

Submodules

nlp_architect.nn.torch.layers.crf module

class nlp_architect.nn.torch.layers.crf.CRF(num_tags: int, batch_first: bool = False)[source]

Bases: torch.nn.modules.module.Module

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. :param num_tags: Number of tags. :param batch_first: Whether the first dimension corresponds to the size of a minibatch.

start_transitions

Start transition score tensor of size (num_tags,).

Type:~torch.nn.Parameter
end_transitions

End transition score tensor of size (num_tags,).

Type:~torch.nn.Parameter
transitions

Transition score tensor of size (num_tags, num_tags).

Type:~torch.nn.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.
decode(emissions: torch.Tensor, mask: Optional[torch.ByteTensor] = None) → List[List[int]][source]

Find the most likely tag sequence using Viterbi algorithm. :param emissions: Emission score tensor of size

(seq_length, batch_size, num_tags) if batch_first is False, (batch_size, seq_length, num_tags) otherwise.
Parameters:mask (~torch.ByteTensor) – Mask tensor of size (seq_length, batch_size) if batch_first is False, (batch_size, seq_length) otherwise.
Returns:List of list containing the best tag sequence for each batch.
forward(emissions: torch.Tensor, tags: torch.LongTensor, mask: Optional[torch.ByteTensor] = None, reduction: str = 'sum') → torch.Tensor[source]

Compute the conditional log likelihood of a sequence of tags given emission scores. :param emissions: Emission score tensor of size

(seq_length, batch_size, num_tags) if batch_first is False, (batch_size, seq_length, num_tags) otherwise.
Parameters:
  • tags (~torch.LongTensor) – Sequence of tags tensor of size (seq_length, batch_size) if batch_first is False, (batch_size, seq_length) otherwise.
  • mask (~torch.ByteTensor) – Mask tensor of size (seq_length, batch_size) if batch_first is False, (batch_size, seq_length) otherwise.
  • 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.
Returns:

The log likelihood. This will have size (batch_size,) if reduction is none, () otherwise.

Return type:

~torch.Tensor

reset_parameters() → None[source]

Initialize the transition parameters. The parameters will be initialized randomly from a uniform distribution between -0.1 and 0.1.

Module contents