partial_tagger.training module#

class partial_tagger.training.Trainer(collator: BaseCollator, encoder_factory: BaseEncoderFactory)[source]#

A trainer for fitting the parameters of a tagger based on a given dataset.

Parameters:
  • collator – Any instance of the classes that inherit BaseCollator.

  • encoder_factory – An encoder factory for creating encoders.

__call__(train_dataset: list[tuple[str, SequenceLabel]], validation_dataset: list[tuple[str, SequenceLabel]], device: torch.device, batch_size: int = 15, num_epochs: int = 20, learning_rate: float = 2e-05, gradient_clip_value: float = 5.0, target_entity_ratio: float = 0.15, entity_ratio_margin: float = 0.05, balancing_coefficient: int = 10, padding_index: int = -1, logger: Logger | None = None) Recognizer[source]#

Trains an instance of SequenceTagger.

Parameters:
  • train_dataset – A list of training data tuples containing text and tags.

  • validation_dataset – A list of validation data tuples containing text and tags.

  • batch_size – An integer representing a batch size for training. Defaults to 15.

  • num_epochs – An integer representing the number of epochs for training. Defaults to 20.

  • learning_rate – A float representing a learning rate for optimization. Defaults to 2e-5.

  • gradient_clip_value – A float representing a maximum gradient value for clipping. Defaults to 5.0.

  • target_entity_ratio – A float representing a target entity ratio for training. Defaults to 0.15.

  • entity_ratio_margin – A float representing a margin for the entity ratio. Defaults to 0.05.

  • balancing_coefficient – An integer representing a balancing coefficient for the loss function. Defaults to 10.

  • padding_index – An integer representing an index for padding. Defaults to -1.

  • device – A device to be used for training.

  • logger – A logger for logging training progress. Defaults to None.

Returns:

An instance of Recognizer which predicts character-based tags from a given text.

partial_tagger.training.compute_partially_supervised_loss(crf_distribution: BaseCrfDistribution, tag_bitmap: torch.Tensor, outside_index: int, target_entity_ratio: float = 0.15, entity_ratio_margin: float = 0.05, balancing_coefficient: int = 10) torch.Tensor[source]#

Computes the loss proposed in Effland and Collins. ‘21.

Parameters:
  • log_potentials – A [batch_size, sequence_length, num_tag, num_tag] float tensor representing log potentials.

  • tag_bitmap – A [batch_size, sequence_length, num_tag] boolean tensor indicating all active tags at each index.

  • outside_index – An integer representing a non-entity index.

  • target_entity_ratio – A float representing a target entity ratio for training. Defaults to 0.15.

  • entity_ratio_margin – A float representing a margin for the entity ratio. Defaults to 0.05.

  • balancing_coefficient – An integer representing a balancing coefficient for the loss function. Defaults to 10.

Returns:

A float representing loss.