Conditional random field

class allennlp.modules.conditional_random_field.ConditionalRandomField(num_tags: int, constraints: typing.List[typing.Tuple[int, int]] = None) → 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.


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

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

Computes the log likelihood.

viterbi_tags(logits: torch.autograd.variable.Variable, mask: torch.autograd.variable.Variable) → typing.List[typing.List[int]][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.


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()


List[Tuple[int, int]]

The allowed transitions (from_token_id, to_token_id)