-
Notifications
You must be signed in to change notification settings - Fork 401
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 #635 from QData/hard-label-attack
hard label classification
- Loading branch information
Showing
2 changed files
with
42 additions
and
4 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
41 changes: 41 additions & 0 deletions
41
textattack/goal_functions/classification/hardlabel_classification.py
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,41 @@ | ||
""" | ||
Determine if an attack has been successful in Hard Label Classficiation. | ||
---------------------------------------------------- | ||
""" | ||
|
||
|
||
from .classification_goal_function import ClassificationGoalFunction | ||
|
||
|
||
class HardLabelClassification(ClassificationGoalFunction): | ||
"""An hard label attack on classification models which attempts to maximize | ||
the semantic similarity of the label such that the target is outside of the | ||
decision boundary. | ||
Args: | ||
target_max_score (float): If set, goal is to reduce model output to | ||
below this score. Otherwise, goal is to change the overall predicted | ||
class. | ||
""" | ||
|
||
def __init__(self, *args, target_max_score=None, **kwargs): | ||
self.target_max_score = target_max_score | ||
super().__init__(*args, **kwargs) | ||
|
||
def _is_goal_complete(self, model_output, _): | ||
if self.target_max_score: | ||
return model_output[self.ground_truth_output] < self.target_max_score | ||
elif (model_output.numel() == 1) and isinstance( | ||
self.ground_truth_output, float | ||
): | ||
return abs(self.ground_truth_output - model_output.item()) >= 0.5 | ||
else: | ||
return model_output.argmax() != self.ground_truth_output | ||
|
||
def _get_score(self, model_output, _): | ||
# If the model outputs a single number and the ground truth output is | ||
# a float, we assume that this is a regression task. | ||
if (model_output.numel() == 1) and isinstance(self.ground_truth_output, float): | ||
return max(model_output.item(), self.ground_truth_output) | ||
else: | ||
return 1 - model_output[self.ground_truth_output] |