Models

Named Entity Recognizer

class forte.models.ner.conditional_random_field.ConditionalRandomField(num_tags: int, constraints: Optional[List[Tuple[int, int]]] = None, include_start_end_transitions: bool = True)[source]

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) – The number of tags.
  • constraints (List[Tuple[int, int]]) – 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) – Whether to include the start and end transition parameters.
forward(inputs: torch.Tensor, tags: torch.Tensor, mask: Optional[torch.ByteTensor] = None) → torch.Tensor[source]

Computes the log likelihood.

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

Semantic Role Labeling

class forte.models.srl.model.LabeledSpanGraphNetwork(word_vocab: texar.torch.data.vocabulary.Vocab, char_vocab: texar.torch.data.vocabulary.Vocab, hparams=None)[source]
output_size

This module is supposed to be the last layer so we will not return an informative output_size Returns:

class ReturnType[source]
forward(inputs: texar.torch.data.data.dataset_utils.Batch) → ReturnType[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

decode(inputs: texar.torch.data.data.dataset_utils.Batch, enforce_constraint: bool = False) → List[Dict[int, List[forte.models.srl.data.Span]]][source]

Performs optimal decoding with dynamic programming.

Returns:A nested structure of SRL spans, representing the (inner) list of spans for each predicate (middle dict) and for each example in the batch (outer list).