# allennlp.modules.conditional_random_field¶

Conditional random field

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

Parameters
num_tagsint, required

The number of tags.

constraintsList[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_transitionsbool, optional (default: True)

Whether to include the start and end transition parameters.

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

Computes the log likelihood.

viterbi_tags(self, logits:torch.Tensor, mask:torch.Tensor) → List[Tuple[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, labels:Dict[int, str]) → List[Tuple[int, int]][source]

Given labels 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_typestr, required

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

labelsDict[int, str], required

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

Returns
List[Tuple[int, int]]

The allowed transitions (from_label_id, to_label_id).

allennlp.modules.conditional_random_field.is_transition_allowed(constraint_type:str, from_tag:str, from_entity:str, to_tag:str, to_entity:str)[source]

Given a constraint type and strings from_tag and to_tag that represent the origin and destination of the transition, return whether the transition is allowed under the given constraint type.

Parameters
constraint_typestr, required

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

from_tagstr, required

The tag that the transition originates from. For example, if the label is I-PER, the from_tag is I.

from_entity: str, required

The entity corresponding to the from_tag. For example, if the label is I-PER, the from_entity is PER.

to_tagstr, required

The tag that the transition leads to. For example, if the label is I-PER, the to_tag is I.

to_entity: str, required

The entity corresponding to the to_tag. For example, if the label is I-PER, the to_entity is PER.

Returns
bool

Whether the transition is allowed under the given constraint_type.