-
Notifications
You must be signed in to change notification settings - Fork 434
New metric module to improve flexibility and intuitiveness - moved from #475 #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c0e2993
[CODE] Transfer from #475 + new structure
da4ca68
[CODE] Changing metric to resemble constraints structure
46781b1
[CODE] Add code for perplexity
5a88253
[CODE] Fixing init file
4a94be9
[CODE] Add command-line option for quality metrics
5e929f2
[FIX] Import order+metrics import
sanchit97 a1b2c5b
[CODE] New USE metric WIP
sanchit97 0baa502
[FIX] Working USE
sanchit97 10ee24b
[FIX] Change structure of Metric mdl
sanchit97 32c3e43
[CODE] Fix metrics, add tests
sanchit97 559057e
[CODE] Fix black on use
sanchit97 aab7eec
[CODE] Fix isort on use
sanchit97 b5a1209
[CODE] Add new help msg
sanchit97 aa1ad15
[CODE] Add new docs
sanchit97 d44a54c
[CODE] Fix print
sanchit97 eab1cd0
[CODE] Fix black
sanchit97 3e2b16f
[FIX] Fix perplexity precision
sanchit97 f1ef471
[FIX] Fix perplexity escape seq
sanchit97 fa9817a
fix docstring issues..
qiyanjun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
| """ | ||
| """ | ||
|
|
||
| from .metric import Metric | ||
|
|
||
| from .attack_metrics import AttackSuccessRate | ||
| from .attack_metrics import WordsPerturbed | ||
| from .attack_metrics import AttackQueries | ||
|
|
||
| from .quality_metrics import Perplexity |
This file contains hidden or 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,13 @@ | ||
| """ | ||
|
|
||
| attack_metrics: | ||
| ====================== | ||
|
|
||
| TextAttack allows users to use their own metrics on adversarial examples or select common metrics to display. | ||
|
|
||
|
|
||
| """ | ||
|
|
||
| from .attack_queries import AttackQueries | ||
| from .attack_success_rate import AttackSuccessRate | ||
| from .words_perturbed import WordsPerturbed |
This file contains hidden or 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,35 @@ | ||
| import numpy as np | ||
|
|
||
| from textattack.attack_results import SkippedAttackResult | ||
| from textattack.metrics import Metric | ||
|
|
||
|
|
||
| class AttackQueries(Metric): | ||
| """Calculates all metrics related to number of queries in an attack | ||
|
|
||
| Args: | ||
| results (:obj::`list`:class:`~textattack.goal_function_results.GoalFunctionResult`): | ||
| Attack results for each instance in dataset | ||
| """ | ||
|
|
||
| def __init__(self, results): | ||
| self.results = results | ||
|
|
||
| self.all_metrics = {} | ||
|
|
||
| def calculate(self): | ||
| self.num_queries = np.array( | ||
| [ | ||
| r.num_queries | ||
| for r in self.results | ||
| if not isinstance(r, SkippedAttackResult) | ||
| ] | ||
| ) | ||
| self.all_metrics["avg_num_queries"] = self.avg_num_queries() | ||
|
|
||
| return self.all_metrics | ||
|
|
||
| def avg_num_queries(self): | ||
| avg_num_queries = self.num_queries.mean() | ||
| avg_num_queries = round(avg_num_queries, 2) | ||
| return avg_num_queries |
This file contains hidden or 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,67 @@ | ||
| from textattack.attack_results import FailedAttackResult, SkippedAttackResult | ||
| from textattack.metrics import Metric | ||
|
|
||
|
|
||
| class AttackSuccessRate(Metric): | ||
| """Calculates all metrics related to number of succesful, failed and skipped results in an attack | ||
|
|
||
| Args: | ||
| results (:obj::`list`:class:`~textattack.goal_function_results.GoalFunctionResult`): | ||
| Attack results for each instance in dataset | ||
| """ | ||
|
|
||
| def __init__(self, results): | ||
| self.results = results | ||
| self.failed_attacks = 0 | ||
| self.skipped_attacks = 0 | ||
| self.successful_attacks = 0 | ||
| self.total_attacks = len(self.results) | ||
|
|
||
| self.all_metrics = {} | ||
|
|
||
| def calculate(self): | ||
| for i, result in enumerate(self.results): | ||
| if isinstance(result, FailedAttackResult): | ||
| self.failed_attacks += 1 | ||
| continue | ||
| elif isinstance(result, SkippedAttackResult): | ||
| self.skipped_attacks += 1 | ||
| continue | ||
| else: | ||
| self.successful_attacks += 1 | ||
|
|
||
| # Calculated numbers | ||
| self.all_metrics["successful_attacks"] = self.successful_attacks | ||
| self.all_metrics["failed_attacks"] = self.failed_attacks | ||
| self.all_metrics["skipped_attacks"] = self.skipped_attacks | ||
|
|
||
| # Percentages wrt the calculations | ||
| self.all_metrics["original_accuracy"] = self.original_accuracy_perc() | ||
| self.all_metrics["attack_accuracy_perc"] = self.attack_accuracy_perc() | ||
| self.all_metrics["attack_success_rate"] = self.attack_success_rate_perc() | ||
|
|
||
| return self.all_metrics | ||
|
|
||
| def original_accuracy_perc(self): | ||
| original_accuracy = ( | ||
| (self.total_attacks - self.skipped_attacks) * 100.0 / (self.total_attacks) | ||
| ) | ||
| original_accuracy = round(original_accuracy, 2) | ||
| return original_accuracy | ||
|
|
||
| def attack_accuracy_perc(self): | ||
| accuracy_under_attack = (self.failed_attacks) * 100.0 / (self.total_attacks) | ||
| accuracy_under_attack = round(accuracy_under_attack, 2) | ||
| return accuracy_under_attack | ||
|
|
||
| def attack_success_rate_perc(self): | ||
| if self.successful_attacks + self.failed_attacks == 0: | ||
| attack_success_rate = 0 | ||
| else: | ||
| attack_success_rate = ( | ||
| self.successful_attacks | ||
| * 100.0 | ||
| / (self.successful_attacks + self.failed_attacks) | ||
| ) | ||
| attack_success_rate = round(attack_success_rate, 2) | ||
| return attack_success_rate |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.