-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from QData/metric-module
Fix metric-module Issue#532
- Loading branch information
Showing
7 changed files
with
164 additions
and
81 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
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,58 @@ | ||
def test_perplexity(): | ||
from textattack.attack_results import SuccessfulAttackResult | ||
from textattack.goal_function_results.classification_goal_function_result import ( | ||
ClassificationGoalFunctionResult, | ||
) | ||
from textattack.metrics.quality_metrics import Perplexity | ||
from textattack.shared.attacked_text import AttackedText | ||
|
||
sample_text = "hide new secretions from the parental units " | ||
sample_atck_text = "Ehide enw secretions from the parental units " | ||
|
||
results = [ | ||
SuccessfulAttackResult( | ||
ClassificationGoalFunctionResult( | ||
AttackedText(sample_text), None, None, None, None, None, None | ||
), | ||
ClassificationGoalFunctionResult( | ||
AttackedText(sample_atck_text), None, None, None, None, None, None | ||
), | ||
) | ||
] | ||
ppl = Perplexity(model_name="distilbert-base-uncased").calculate(results) | ||
|
||
assert int(ppl["avg_original_perplexity"]) == int(81.95) | ||
|
||
|
||
def test_use(): | ||
import transformers | ||
|
||
from textattack import AttackArgs, Attacker | ||
from textattack.attack_recipes import DeepWordBugGao2018 | ||
from textattack.datasets import HuggingFaceDataset | ||
from textattack.metrics.quality_metrics import USEMetric | ||
from textattack.models.wrappers import HuggingFaceModelWrapper | ||
|
||
model = transformers.AutoModelForSequenceClassification.from_pretrained( | ||
"distilbert-base-uncased-finetuned-sst-2-english" | ||
) | ||
tokenizer = transformers.AutoTokenizer.from_pretrained( | ||
"distilbert-base-uncased-finetuned-sst-2-english" | ||
) | ||
model_wrapper = HuggingFaceModelWrapper(model, tokenizer) | ||
attack = DeepWordBugGao2018.build(model_wrapper) | ||
dataset = HuggingFaceDataset("glue", "sst2", split="train") | ||
attack_args = AttackArgs( | ||
num_examples=1, | ||
log_to_csv="log.csv", | ||
checkpoint_interval=5, | ||
checkpoint_dir="checkpoints", | ||
disable_stdout=True, | ||
) | ||
attacker = Attacker(attack, dataset, attack_args) | ||
|
||
results = attacker.attack_dataset() | ||
|
||
usem = USEMetric().calculate(results) | ||
|
||
assert usem["avg_attack_use_score"] == 0.76 |
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