掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
OpenAttack是一款專為文本對抗攻擊設(shè)計的開源工具套件,該工具基于Python開發(fā),可以處理文本對抗攻擊的整個過程,包括預(yù)處理文本、訪問目標(biāo)用戶模型、生成對抗示例和評估攻擊模型等等。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信平臺小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了涉縣免費建站歡迎大家使用!
OpenAttack支持以下幾種功能:
OpenAttack的使用范圍非常廣,其中包括但不限于:
我們可以使用pip安裝,或者克隆該項目源碼來安裝OpenAttack。
使用pip安裝(推薦):
- pip install OpenAttack
克隆代碼庫:
- git clone https://github.com/thunlp/OpenAttack.git
- cd OpenAttack
- python setup.py install
安裝完成之后,我們可以嘗試運行“demo.py”來檢測OpenAttack是否能夠正常工作:
(1) 基礎(chǔ)使用:使用內(nèi)置攻擊模型
OpenAttack內(nèi)置了一些常用的文本分類模型,如LSTM和BERT,以及用于情感分析的SST和用于自然語言推理的SNLI等數(shù)據(jù)集。
以下代碼段顯示了如何使用基于遺傳算法的攻擊模型攻擊SST數(shù)據(jù)集上的BERT:
- import OpenAttack as oa
- # choose a trained victim classification model
- victim = oa.DataManager.load("Victim.BERT.SST")
- # choose an evaluation dataset
- dataset = oa.DataManager.load("Dataset.SST.sample")
- # choose Genetic as the attacker and initialize it with default parameters
- attacker = oa.attackers.GeneticAttacker()
- # prepare for attacking
- attack_eval = oa.attack_evals.DefaultAttackEval(attacker, victim)
- # launch attacks and print attack results
- attack_eval.eval(dataset, visualize=True)
(2) 高級使用:攻擊自定義目標(biāo)用戶模型
下面的代碼段顯示了如何使用基于遺傳算法的攻擊模型攻擊SST上的自定義情緒分析模型:
- import OpenAttack as oa
- import numpy as np
- from nltk.sentiment.vader import SentimentIntensityAnalyzer
- # configure access interface of the customized victim model
- class MyClassifier(oa.Classifier):
- def __init__(self):
- self.model = SentimentIntensityAnalyzer()
- # access to the classification probability scores with respect input sentences
- def get_prob(self, input_):
- rt = []
- for sent in input_:
- rs = self.model.polarity_scores(sent)
- prob = rs["pos"] / (rs["neg"] + rs["pos"])
- rt.append(np.array([1 - prob, prob]))
- return np.array(rt)
- # choose the costomized classifier as the victim model
- victim = MyClassifier()
- # choose an evaluation dataset
- dataset = oa.DataManager.load("Dataset.SST.sample")
- # choose Genetic as the attacker and initialize it with default parameters
- attacker = oa.attackers.GeneticAttacker()
- # prepare for attacking
- attack_eval = oa.attack_evals.DefaultAttackEval(attacker, victim)
- # launch attacks and print attack results
- attack_eval.eval(dataset, visualize=True)
OpenAttack:【GitHub傳送門】

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流