def prepare_data(): vocab = {"<UNK>": 0, "<PAD>": 1} train, valid, test = OpenAttack.loadDataset("SST") tp = OpenAttack.text_processors.DefaultTextProcessor() for dataset in [train, valid, test]: for inst in dataset: inst.tokens = list(map(lambda x: x[0], tp.get_tokens(inst.x))) for token in inst.tokens: if token not in vocab: vocab[token] = len(vocab) return train, valid, test, vocab
def main(): dataset = OpenAttack.loadDataset("SST")[0][:5] clsf = OpenAttack.loadVictim("BiLSTM.SST") attackers = get_attackers(dataset, clsf) for attacker in attackers: print(attacker.__class__.__name__) try: print( OpenAttack.attack_evals.DefaultAttackEval( attacker, clsf, progress_bar=False).eval(dataset)) except Exception as e: print(e) print("\n")
def main(): import multiprocessing if multiprocessing.get_start_method() != "spawn": multiprocessing.set_start_method("spawn", force=True) dataset = OpenAttack.loadDataset("SST")[0][:5] clsf = OpenAttack.loadVictim("BiLSTM.SST").to("cuda:0") attackers = get_attackers(dataset, clsf) for attacker in attackers: print(attacker.__class__.__name__) try: print( OpenAttack.attack_evals.MultiProcessAttackEval( attacker, clsf, num_process=2, progress_bar=False).eval(dataset)) except Exception as e: raise e print(e) print("\n")
import OpenAttack dataset = OpenAttack.loadDataset("SST")[0][:5] clsf = OpenAttack.loadVictim("BiLSTM.SST") rules = OpenAttack.attackers.SEAAttacker.get_rules(clsf, dataset) triggers = OpenAttack.attackers.UATAttacker.get_triggers( clsf, dataset, word2id=clsf.config["word2id"], embedding=clsf.config["embedding"]) attackers = [ OpenAttack.attackers.FDAttacker(word2id=clsf.config["word2id"], embedding=clsf.config["embedding"]), OpenAttack.attackers.SEAAttacker(rules=rules), OpenAttack.attackers.UATAttacker(triggers=triggers), OpenAttack.attackers.TextBuggerAttacker(), OpenAttack.attackers.TextFoolerAttacker(), OpenAttack.attackers.VIPERAttacker(), OpenAttack.attackers.DeepWordBugAttacker(), OpenAttack.attackers.GANAttacker(), OpenAttack.attackers.GeneticAttacker(), OpenAttack.attackers.HotFlipAttacker(), OpenAttack.attackers.PWWSAttacker(), OpenAttack.attackers.SCPNAttacker(), OpenAttack.attackers.PSOAttacker(), ] for attacker in attackers: print(attacker.__class__.__name__) try: