allennlp.modules.conditional_random_field

Conditional random field

class allennlp.modules.conditional_random_field.ConditionalRandomField(num_tags: int, constraints: typing.List[typing.Tuple[int, int]] = None, include_start_end_transitions: bool = True) → None[source]

Bases: torch.nn.modules.module.Module

This module uses the “forward-backward” algorithm to compute the log-likelihood of its inputs assuming a conditional random field model.

See, e.g. http://www.cs.columbia.edu/~mcollins/fb.pdf

Parameters:
num_tags : int, required

The number of tags.

constraints : List[Tuple[int, int]], optional (default: None)

An optional list of allowed transitions (from_tag_id, to_tag_id). These are applied to viterbi_tags() but do not affect forward(). These should be derived from allowed_transitions so that the start and end transitions are handled correctly for your tag type.

include_start_end_transitions : bool, optional (default: True)

Whether to include the start and end transition parameters.

forward(inputs: torch.Tensor, tags: torch.Tensor, mask: torch.ByteTensor = None) → torch.Tensor[source]

Computes the log likelihood.

viterbi_tags(logits: torch.Tensor, mask: torch.Tensor) → typing.List[typing.Tuple[typing.List[int], float]][source]

Uses viterbi algorithm to find most likely tags for the given inputs. If constraints are applied, disallows all other transitions.

allennlp.modules.conditional_random_field.allowed_transitions(constraint_type: str, tokens: typing.Dict[int, str]) → typing.List[typing.Tuple[int, int]][source]

Given tokens and a constraint type, returns the allowed transitions. It will additionally include transitions for the start and end states, which are used by the conditional random field.

Parameters:
constraint_type : str, required

Indicates which constraint to apply. Current choices are “BIO” and “BIOUL”.

tokens : Dict[int, str], required

A mapping {token_id -> token}. Most commonly this would be the value from Vocabulary.get_index_to_token_vocabulary()

Returns:
``List[Tuple[int, int]]``

The allowed transitions (from_token_id, to_token_id).