QData / TextAttack

TextAttack 🐙 is a Python framework for adversarial attacks, data augmentation, and model training in NLP https://textattack.readthedocs.io/en/master/
https://textattack.readthedocs.io/en/master/
MIT License
2.98k stars 397 forks source link

Issue with using AttackArgs class outside of the command line (in my Python script) #728

Open sabrinagoellner opened 1 year ago

sabrinagoellner commented 1 year ago

Hello,

I am experiencing an issue when trying to use the AttackArgs class from the TextAttack library outside of the command line. I'm attempting to use the class programmatically in my Python script, but the arguments I provide are not correctly assigned.

Here's a code snippet demonstrating my problem:

from textattack.attack_args import AttackArgs

attack_args = AttackArgs(
num_examples=100, 
num_successful_examples=2, 
log_to_csv="text_attack_results.csv"
)

print(attack_args)

When I run the code, the num_examples values are not updated and remain at their default values. However, it seems that other arguments like num_successful_examples or log_to_csv are being correctly assigned.

I suspect this might be related to the class implementation, specifically the _add_parser_args method, which seems to be designed for command-line usage. Is there a way to use the AttackArgs class programmatically in a Python script without relying on the command line interface? Any guidance or suggestions would be appreciated.

Thank you for your assistance.

System Information:

sabrinagoellner commented 1 year ago

I discovered that the issue with num_examples being set to None is due to the following code snippet in the AttackArgs class:

def __post_init__(self): if self.num_successful_examples: self.num_examples = None

This is causing num_examples to be set to None when a value is assigned to num_successful_examples.

I would like to understand if this behavior is intentional and, if so, the rationale behind it. Is it to prevent users from specifying both num_examples and num_successful_examples simultaneously? If this is a design choice, could you please provide guidance on how to use these options properly in cases where we want to limit the total number of examples while also specifying the number of successful adversarial examples desired? If this is a bug, can you suggest a workaround or provide an update to fix the issue?