srl model tweaks
@@ -45,6 +45,8 @@ class SemanticRoleLabeler(Model):
Used to initialize the model parameters.
regularizer : ``RegularizerApplicator``, optional (default=``None``)
If provided, will be used to calculate the regularization penalty during training.
+ label_smoothing : ``float``, optional (default = 0.0)
+ Whether or not to use label smoothing on the labels when computing cross entropy loss.
"""
def __init__(self, vocab: Vocabulary,
text_field_embedder: TextFieldEmbedder,
@@ -52,7 +54,8 @@ class SemanticRoleLabeler(Model):
binary_feature_dim: int,
embedding_dropout: float = 0.0,
initializer: InitializerApplicator = InitializerApplicator(),
- regularizer: Optional[RegularizerApplicator] = None) -> None:
+ regularizer: Optional[RegularizerApplicator] = None,
+ label_smoothing: float = None) -> None:
super(SemanticRoleLabeler, self).__init__(vocab, regularizer)
self.text_field_embedder = text_field_embedder
@@ -68,6 +71,7 @@ class SemanticRoleLabeler(Model):
self.tag_projection_layer = TimeDistributed(Linear(self.encoder.get_output_dim(),
self.num_classes))
self.embedding_dropout = Dropout(p=embedding_dropout)
+ self._label_smoothing = label_smoothing
check_dimensions_match(text_field_embedder.get_output_dim() + binary_feature_dim,
encoder.get_input_dim(),
@@ -130,7 +134,10 @@ class SemanticRoleLabeler(Model):
self.num_classes])
output_dict = {"logits": logits, "class_probabilities": class_probabilities}
if tags is not None:
- loss = sequence_cross_entropy_with_logits(logits, tags, mask)
+ loss = sequence_cross_entropy_with_logits(logits,
+ tags,
+ mask,
+ label_smoothing=self._label_smoothing)
self.span_metric(class_probabilities, tags, mask)
output_dict["loss"] = loss
@@ -166,15 +173,10 @@ class SemanticRoleLabeler(Model):
def get_metrics(self, reset: bool = False):
metric_dict = self.span_metric.get_metric(reset=reset)
- if self.training:
# This can be a lot of metrics, as there are 3 per class.
- # During training, we only really care about the overall
- # metrics, so we filter for them here.
- # TODO(Mark): This is fragile and should be replaced with some verbosity level in Trainer.
+ # we only really care about the overall metrics, so we filter for them here.
return {x: y for x, y in metric_dict.items() if "overall" in x}
- return metric_dict
-
def get_viterbi_pairwise_potentials(self):
"""
Generate a matrix of pairwise transition potentials for the BIO labels.
@@ -206,6 +208,7 @@ class SemanticRoleLabeler(Model):
text_field_embedder = TextFieldEmbedder.from_params(vocab, embedder_params)
encoder = Seq2SeqEncoder.from_params(params.pop("encoder"))
binary_feature_dim = params.pop_int("binary_feature_dim")
+ label_smoothing = params.pop_float("label_smoothing", None)
initializer = InitializerApplicator.from_params(params.pop('initializer', []))
regularizer = RegularizerApplicator.from_params(params.pop('regularizer', []))
@@ -215,7 +218,8 @@ class SemanticRoleLabeler(Model):
encoder=encoder,
binary_feature_dim=binary_feature_dim,
initializer=initializer,
- regularizer=regularizer)
+ regularizer=regularizer,
+ label_smoothing=label_smoothing)
def write_to_conll_eval_file(prediction_file: TextIO,
gold_file: TextIO,
