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.
See, e.g. http://www.cs.columbia.edu/~mcollins/fb.pdf
- 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 affectforward()
. 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.
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_type
str
, required Indicates which constraint to apply. Current choices are “BIO”, “IOB1”, “BIOUL”, and “BMES”.
- labels
Dict[int, str]
, required A mapping {label_id -> label}. Most commonly this would be the value from Vocabulary.get_index_to_token_vocabulary()
- constraint_type
- 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
andto_tag
that represent the origin and destination of the transition, return whether the transition is allowed under the given constraint type.- Parameters
- constraint_type
str
, required Indicates which constraint to apply. Current choices are “BIO”, “IOB1”, “BIOUL”, and “BMES”.
- from_tag
str
, required The tag that the transition originates from. For example, if the label is
I-PER
, thefrom_tag
isI
.- from_entity: ``str``, required
The entity corresponding to the
from_tag
. For example, if the label isI-PER
, thefrom_entity
isPER
.- to_tag
str
, required The tag that the transition leads to. For example, if the label is
I-PER
, theto_tag
isI
.- to_entity: ``str``, required
The entity corresponding to the
to_tag
. For example, if the label isI-PER
, theto_entity
isPER
.
- constraint_type
- Returns
bool
Whether the transition is allowed under the given
constraint_type
.