This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of Weighted CRF Tagger (handling unbalanced datasets) (#…
…341) * (rebase) Weighted CRF: scaled emission scores * Added FBetaMeasure to CrfTagger just to test class weights * Added FBetaMeasure2 to CrfTagger. * Fixed bug regarding label_weights in CrfTagger * CrfTagger: using micro and macro average for FBetaMeasure2 * CRF weighting strategies * Weighted CRF: adjustments considering refactoring * Weighted CRF tests * Weighted CRF: tests minor adjustments * CrfTagger: added test regarding FBetaVerboseMeasure * CrfTagger: black formatting * Updated CrfTagger to the new module organization * Update CHANGELOG.md Co-authored-by: Pete <epwalsh10@gmail.com>
- Loading branch information
1 parent
704a244
commit 97df196
Showing
4 changed files
with
253 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
from flaky import flaky | ||
import pytest | ||
|
||
from allennlp.commands.train import train_model_from_file | ||
from allennlp.common.testing import ModelTestCase | ||
from allennlp.common.checks import ConfigurationError | ||
|
||
from tests import FIXTURES_ROOT | ||
|
||
|
||
class CrfTaggerLabelWeightsTest(ModelTestCase): | ||
def setup_method(self): | ||
super().setup_method() | ||
self.set_up_model( | ||
FIXTURES_ROOT / "tagging" / "crf_tagger" / "experiment.json", | ||
FIXTURES_ROOT / "tagging" / "conll2003.txt", | ||
) | ||
|
||
def test_label_weights_effectiveness(self): | ||
training_tensors = self.dataset.as_tensor_dict() | ||
save_dir = self.TEST_DIR / "save_and_load_test" | ||
|
||
# original CRF | ||
output_dict_original = self.model(**training_tensors) | ||
|
||
# weighted CRF | ||
model_weighted = train_model_from_file( | ||
self.param_file, | ||
save_dir, | ||
overrides={"model.label_weights": {"I-ORG": 10.0}}, | ||
force=True, | ||
return_model=True, | ||
) | ||
output_dict_weighted = model_weighted(**training_tensors) | ||
|
||
# assert that logits are substantially different | ||
assert ( | ||
output_dict_weighted["logits"].isclose(output_dict_original["logits"]).sum() | ||
< output_dict_original["logits"].numel() / 2 | ||
) | ||
|
||
def test_label_weights_effectiveness_emission_transition(self): | ||
training_tensors = self.dataset.as_tensor_dict() | ||
save_dir = self.TEST_DIR / "save_and_load_test" | ||
|
||
# original CRF | ||
output_dict_original = self.model(**training_tensors) | ||
|
||
# weighted CRF | ||
model_weighted = train_model_from_file( | ||
self.param_file, | ||
save_dir, | ||
overrides={ | ||
"model.label_weights": {"I-ORG": 10.0}, | ||
"model.weight_strategy": "emission_transition", | ||
}, | ||
force=True, | ||
return_model=True, | ||
) | ||
output_dict_weighted = model_weighted(**training_tensors) | ||
|
||
# assert that logits are substantially different | ||
assert ( | ||
output_dict_weighted["logits"].isclose(output_dict_original["logits"]).sum() | ||
< output_dict_original["logits"].numel() / 2 | ||
) | ||
|
||
def test_label_weights_effectiveness_lannoy(self): | ||
training_tensors = self.dataset.as_tensor_dict() | ||
save_dir = self.TEST_DIR / "save_and_load_test" | ||
|
||
# original CRF | ||
output_dict_original = self.model(**training_tensors) | ||
|
||
# weighted CRF | ||
model_weighted = train_model_from_file( | ||
self.param_file, | ||
save_dir, | ||
overrides={ | ||
"model.label_weights": {"I-ORG": 10.0}, | ||
"model.weight_strategy": "lannoy", | ||
}, | ||
force=True, | ||
return_model=True, | ||
) | ||
output_dict_weighted = model_weighted(**training_tensors) | ||
|
||
# assert that logits are substantially different | ||
assert ( | ||
output_dict_weighted["logits"].isclose(output_dict_original["logits"]).sum() | ||
< output_dict_original["logits"].numel() / 2 | ||
) | ||
|
||
def test_config_error_invalid_label(self): | ||
save_dir = self.TEST_DIR / "save_and_load_test" | ||
with pytest.raises(ConfigurationError): | ||
train_model_from_file( | ||
self.param_file, | ||
save_dir, | ||
overrides={"model.label_weights": {"BLA": 10.0}}, | ||
force=True, | ||
return_model=True, | ||
) | ||
|
||
def test_config_error_strategy_without_weights(self): | ||
save_dir = self.TEST_DIR / "save_and_load_test" | ||
with pytest.raises(ConfigurationError): | ||
train_model_from_file( | ||
self.param_file, | ||
save_dir, | ||
overrides={"model.weight_strategy": "emission"}, | ||
force=True, | ||
return_model=True, | ||
) | ||
|
||
def test_config_error_invalid_strategy(self): | ||
save_dir = self.TEST_DIR / "save_and_load_test" | ||
with pytest.raises(ConfigurationError): | ||
train_model_from_file( | ||
self.param_file, | ||
save_dir, | ||
overrides={ | ||
"model.label_weights": {"I-ORG": 10.0}, | ||
"model.weight_strategy": "invalid", | ||
}, | ||
force=True, | ||
return_model=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters