MCPcopy Index your code
hub / github.com/QData/TextAttack / test_perplexity

Function test_perplexity

tests/test_metric_api.py:1–24  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1def test_perplexity():
2 from textattack.attack_results import SuccessfulAttackResult
3 from textattack.goal_function_results.classification_goal_function_result import (
4 ClassificationGoalFunctionResult,
5 )
6 from textattack.metrics.quality_metrics import Perplexity
7 from textattack.shared.attacked_text import AttackedText
8
9 sample_text = "hide new secretions from the parental units "
10 sample_atck_text = "Ehide enw secretions from the parental units "
11
12 results = [
13 SuccessfulAttackResult(
14 ClassificationGoalFunctionResult(
15 AttackedText(sample_text), None, None, None, None, None, None
16 ),
17 ClassificationGoalFunctionResult(
18 AttackedText(sample_atck_text), None, None, None, None, None, None
19 ),
20 )
21 ]
22 ppl = Perplexity(model_name="distilbert-base-uncased").calculate(results)
23
24 assert int(ppl["avg_original_perplexity"]) == int(81.95)
25
26
27def test_use():

Callers

nothing calls this directly

Calls 5

AttackedTextClass · 0.90
PerplexityClass · 0.90
calculateMethod · 0.45

Tested by

no test coverage detected