mpls工作原理通俗解釋_用這兩種方法向最終用戶解釋NLP模型的工作原理還是不錯的...

點擊上方關注,All in AI中國

上周,我看了一個關于“NLP的實踐特性工程”的演講。主要是關于LIME和SHAP在文本分類可解釋性方面是如何工作的。

我決定寫一篇關于它們的文章,因為它們很有趣、易于使用,而且視覺上很吸引人。

所有的機器學習模型都是在更高的維度上運行的,而不是在人腦可以直接看到的維度上運行的,這些機器學習模型都可以被稱為黑盒模型,它可以歸結為模型的可解釋性。特別是在NLP領域中,特征的維數往往很大,說明特征的重要性變得越來越復雜。

LIME & SHAP不僅幫助我們向最終用戶解釋NLP模型的工作原理,而且幫助我們自己解釋NLP模型是如何工作的。

利用 Stack Overflow 問題標簽分類數據集,我們將構建一個多類文本分類模型,然后分別應用LIME和SHAP對模型進行解釋。由于我們之前已經做過多次文本分類,所以我們將快速構建NLP模型,并著重于模型的可解釋性。

數據預處理、特征工程和邏輯回歸

 import pandas as pdimport numpy as npimport sklearnimport sklearn.ensembleimport sklearn.metricsfrom sklearn.utils import shufflefrom __future__ import print_functionfrom io import StringIOimport refrom bs4 import BeautifulSoupfrom nltk.corpus import stopwordsfrom sklearn.model_selection import train_test_splitfrom sklearn.feature_extraction.text import CountVectorizerfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import accuracy_score, f1_score, precision_score, recall_scoreimport limefrom lime import lime_textfrom lime.lime_text import LimeTextExplainerfrom sklearn.pipeline import make_pipelinedf = pd.read_csv('stack-overflow-data.csv')df = df[pd.notnull(df['tags'])]df = df.sample(frac=0.5, random_state=99).reset_index(drop=True)df = shuffle(df, random_state=22)df = df.reset_index(drop=True)df['class_label'] = df['tags'].factorize()[0]class_label_df = df[['tags', 'class_label']].drop_duplicates().sort_values('class_label')label_to_id = dict(class_label_df.values)id_to_label = dict(class_label_df[['class_label', 'tags']].values)REPLACE_BY_SPACE_RE = re.compile('[/(){}[]|@,;]')BAD_SYMBOLS_RE = re.compile('[^0-9a-z #+_]')# STOPWORDS = set(stopwords.words('english'))def clean_text(text): """ text: a string  return: modified initial string """text = BeautifulSoup(text, "lxml").text # HTML decoding. BeautifulSoup's text attribute will return a string stripped of any HTML tags and metadata. text = text.lower() # lowercase text text = REPLACE_BY_SPACE_RE.sub(' ', text) # replace REPLACE_BY_SPACE_RE symbols by space in text. substitute the matched string in REPLACE_BY_SPACE_RE with space. text = BAD_SYMBOLS_RE.sub('', text) # remove symbols which are in BAD_SYMBOLS_RE from text. substitute the matched string in BAD_SYMBOLS_RE with nothing. # text = ' '.join(word for word in text.split() if word not in STOPWORDS) # remove stopwors from text return textdf['post'] = df['post'].apply(clean_text)list_corpus = df["post"].tolist()list_labels = df["class_label"].tolist()X_train, X_test, y_train, y_test = train_test_split(list_corpus, list_labels, test_size=0.2, random_state=40)vectorizer = CountVectorizer(analyzer='word',token_pattern=r'w{1,}', ngram_range=(1, 3), stop_words = 'english', binary=True)train_vectors = vectorizer.fit_transform(X_train)test_vectors = vectorizer.transform(X_test)logreg = LogisticRegression(n_jobs=1, C=1e5)logreg.fit(train_vectors, y_train)pred = logreg.predict(test_vectors)accuracy = accuracy_score(y_test, pred)precision = precision_score(y_test, pred, average='weighted')recall = recall_score(y_test, pred, average='weighted')f1 = f1_score(y_test, pred, average='weighted')print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" % (accuracy, precision, recall, f1))
802c936e40d3a1c301bcbe62946cddd8.png

我們現在目標并不是產生最好的結果。我想盡快進入LIME & SHAP,這就是接下來發生的事情。

用LIME解釋文本預測

從現在開始,這是有趣的部分。下面的代碼片段主要是從LIME教程中借來的。

c = make_pipeline(vectorizer, logreg)class_names=list(df.tags.unique())explainer = LimeTextExplainer(class_names=class_names)idx = 1877exp = explainer.explain_instance(X_test[idx], c.predict_proba, num_features=6, labels=[4, 8])print('Document id: %d' % idx)print('Predicted class =', class_names[logreg.predict(test_vectors[idx]).reshape(1,-1)[0,0]])print('True class: %s' % class_names[y_test[idx]])
6219ec5a5a805ef5f801cb1659472929.png

我們在測試集中隨機選擇一個文檔,它恰好是一個標記為sql的文檔,我們的模型也預測它是sql。使用這個文檔,我們為標簽4 (sql)和標簽8 (python)生成解釋。

print ('Explanation for class %s' % class_names[4])print (''.join(map(str, exp.as_list(label=4))))
a529ebb31f6abb1d24b4ee20eb2a1c7e.png
print ('Explanation for class %s' % class_names[8])print (''.join(map(str, exp.as_list(label=8))))
d9856a627bb952b9b7b86b26f484c38a.png

很明顯,這個文檔對標簽sql有最高的解釋。我們還注意到正負號與特定的標簽有關,例如單詞"sql"對類sql是正的,而對類python是負的,反之亦然。

我們要為這個文檔生成2類標簽頂部。

exp = explainer.explain_instance(X_test[idx], c.predict_proba, num_features=6, top_labels=2)print(exp.available_labels())
ee064dfcc066d4dfb66402dbbbc7758b.png

它給出了sql和python。

exp.show_in_notebook(text=False)
dc44007bb735dd8cc1d4e09efb4c2281.png

讓我來解釋一下這種可視化:

1. 對于本文檔,詞 "sql"對于類sql具有最高的正分數。

2. 我們的模型預測該文檔應該標記為sql,其概率為100%。

3. 如果我們從文檔中刪除word"sql",我們期望模型預測label sql的概率為100% - 65% = 35%。

4. 另一方面,單詞"sql"對于類python是負面的,我們的模型已經了解到單詞"range"對于類python有一個小的正面得分。

我們可能想放大并研究類sql的解釋,以及文檔本身。

exp.show_in_notebook(text=y_test[idx], labels=(4,))
63d6c08fa688480d89a9862af59f469f.png

使用SHAP解釋文本預測

以下過程是從本教程中學到的。「鏈接」

from sklearn.preprocessing import MultiLabelBinarizerimport tensorflow as tffrom tensorflow.keras.preprocessing import textimport keras.backend.tensorflow_backend as KK.set_sessionimport shaptags_split = [tags.split(',') for tags in df['tags'].values]tag_encoder = MultiLabelBinarizer()tags_encoded = tag_encoder.fit_transform(tags_split)num_tags = len(tags_encoded[0])train_size = int(len(df) * .8)y_train = tags_encoded[: train_size]y_test = tags_encoded[train_size:]class TextPreprocessor(object): def __init__(self, vocab_size): self._vocab_size = vocab_size self._tokenizer = None def create_tokenizer(self, text_list): tokenizer = text.Tokenizer(num_words = self._vocab_size) tokenizer.fit_on_texts(text_list) self._tokenizer = tokenizer def transform_text(self, text_list): text_matrix = self._tokenizer.texts_to_matrix(text_list) return text_matrixVOCAB_SIZE = 500train_post = df['post'].values[: train_size]test_post = df['post'].values[train_size: ]processor = TextPreprocessor(VOCAB_SIZE)processor.create_tokenizer(train_post)X_train = processor.transform_text(train_post)X_test = processor.transform_text(test_post)def create_model(vocab_size, num_tags): model = tf.keras.models.Sequential() model.add(tf.keras.layers.Dense(50, input_shape = (VOCAB_SIZE,), activation='relu')) model.add(tf.keras.layers.Dense(25, activation='relu'))model.add(tf.keras.layers.Dense(num_tags, activation='sigmoid')) model.compile(loss = 'binary_crossentropy', optimizer='adam', metrics = ['accuracy']) return model model = create_model(VOCAB_SIZE, num_tags)model.fit(X_train, y_train, epochs = 2, batch_size=128, validation_split=0.1) print('Eval loss/accuracy:{}'.format(model.evaluate(X_test, y_test, batch_size = 128)))
  • 模型訓練完成后,我們使用前200個訓練文檔作為背景數據集進行集成,并創建一個SHAP explainer對象。
  • 我們在測試集的子集上獲得各個預測的屬性值。
  • 將索引轉換為單詞。
  • 使用SHAP的summary_plot方法來顯示影響模型預測的主要特性。
attrib_data = X_train[:200]explainer = shap.DeepExplainer(model, attrib_data)num_explanations = 20shap_vals = explainer.shap_values(X_test[:num_explanations])words = processor._tokenizer.word_indexword_lookup = list()for i in words.keys(): word_lookup.append(i)word_lookup = [''] + word_lookupshap.summary_plot(shap_vals, feature_names=word_lookup, class_names=tag_encoder.classes_)
ae8b75f5173caf9fd415ae913aa7aa25.png
  • 單詞"want"是我們模型使用的最大信號詞,對類jquery預測貢獻最大。
  • 單詞"php"是我們模型使用的第四大信號詞,當然對PHP類貢獻最大。
  • 另一方面,單詞"php"可能對另一個類有負面信號,因為它不太可能在python文檔中看到單詞"php"。

關于LIME & SHAP的機器學習可解釋性,還有很多需要學習的地方。我只介紹了一小部分NLP。其余的可以在Github上找到。NLP-with-Python/LIME_SHAP_StackOverflow.ipynb at master · susanli2016/NLP-with-Python · GitHub

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258932.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258932.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258932.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

向往2的年代

1. JDBC 對插入大量數據如何處理? 2. JAVA反射的使用,如何獲取一個java類的某個方法? 3. 數據庫連接池(優化) 4. 分布式事務管理轉載于:https://www.cnblogs.com/dragonflyyi/p/3564843.html

C++ limits頭文件的用法(numeric_limits)

初學C的時候,對這個模板很陌生,不知道它到底是做什么用的,今天拿起《C標準程序庫》,出現了它的討論,所以決定好好研究一番。 1. numeric_limits是什么? (A)《C標準程序庫》&#xff…

三層架構——配置文件

1、配置文件是什么? 配置文件是隨安裝程序一起被安裝到計算機上的文件,里面存放著安裝好的應用程序執行時所須要的參數。 應用程序配置文件是標準的XML文件,XML標記和屬性是區分大寫和小寫的。它能夠按須要更改,開發者可使用配置文…

《嵌入式系統開發之道——菜鳥成長日志與項目經理的私房菜》——02-04項目范圍(Scope)管理...

本節書摘來異步社區《嵌入式系統開發之道——菜鳥成長日志與項目經理的私房菜》一書中的第2章,第2.4節,作者:邱毅凌,更多章節內容可以訪問云棲社區“異步社區”公眾號查看 02-04項目范圍(Scope)管理 嵌入式…

flex(入門)之timer的使用,鍵盤,鼠標的監聽

package {import flash.display.Shape;import flash.display.Sprite;import flash.events.Event;import flash.events.KeyboardEvent;import flash.events.MouseEvent;import flash.events.TimerEvent;import flash.utils.Timer;import mx.controls.Label;//窗體大小&#xff0…

python 線程超時設置_python 條件變量Condition(36)

文章首發微信公眾號,微信搜索:猿說python對于線程與線程之間的交互我們在前面的文章已經介紹了 python 互斥鎖Lock / python事件Event , 今天繼續介紹一種線程交互方式 – 線程條件變量Condition.一.線程條件變量Condition相關函數介紹acquire() — 線程…

MsWord 操作總結

轉自(http://www.cnblogs.com/eye-like/p/4121219.html) Msdn上的word操作api(不過只有英文版,英文差的先閃過) Word2007的API:http://msdn.microsoft.com/en-us/library/bb257531(voffice.12).aspxWord201…

fwrite,fread and fprintf,fscanf的一些使用體會

這周一直在完成一個任務,就是將訓練出的多個model寫成一個model。其中我們使用了c語言的讀寫方法,搞了一星期, 挖了很多坑,最終都鏟平了。下面列舉出若干有用的知識。 1.fwrite,fread VS fprintf,fscanf的區別 fwrite,fread 讀寫…

《第一桶金怎么賺——淘寶開店創業致富一冊通》一一1.4 淘寶開店創業的流程...

本節書摘來自異步社區出版社《第一桶金怎么賺——淘寶開店創業致富一冊通》一書中的第1章,第1.4節,作者:葛存山,更多章節內容可以訪問云棲社區“異步社區”公眾號查看。 1.4 淘寶開店創業的流程 第一桶金怎么賺——淘寶開店創業致…

檢測虛擬機代碼總匯(更新中)

檢測虛擬機代碼 1 004092D0 /. 55 push ebp2 004092D1 |. 8BEC mov ebp,esp3 004092D3 |. 51 push ecx4 004092D4 |. 53 push ebx5 004092D5 |. 68 1D934000 push 0040931D 6 004092DA |. 64:FF35…

兩數之和 python_同一屏幕播放兩個視頻 視頻左右兩個畫面或視頻上下兩個畫面如何制作...

咱們在網上經常可以看到一些視頻畫面是可以在同一屏幕播放兩個視頻,有的是視頻左右兩個畫面或視頻上下兩個畫面這些是如何制作的呢,其實熟悉視頻編輯軟件的網友應該會比較了解這些操作,好嘞,來,現在就讓小編來演示一下…

dlib人臉特征點對齊

前面我們介紹了使用dlib進行人臉檢測&#xff0c;下面我們給出如何使用dlib進行人臉特征點檢測。我們直接貼出代碼。我們的代碼包括如下幾部分功能&#xff1a; 檢測單張圖片檢測一個視頻檢測一個camera 先給出代碼&#xff1a; #include <dlib/image_processing/frontal_…

IOS開發基礎知識--碎片13

1:運行程序報the file couldnt be opened because you dont have permission to view it 解決辦法&#xff1a;項目—>targets->build settings->build options->changed the value of the "Compiler for C/C/Objective-C" to Default Compiler. 2:百度…

《LoadRunner 12七天速成寶典》—第2章2.6節第二個性能測試案例

本節書摘來自異步社區《LoadRunner 12七天速成寶典》一書中的第2章&#xff0c;第2.6節第二個性能測試案例&#xff0c;作者陳霽&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看。 2.6 第二個性能測試案例云云&#xff1a;烤魚吃得很爽。 戀戀&#xff1a;就…

MongoDB_1

突然想去看下MongoDB的東西&#xff0c;于是有了這篇文章。其實很早以前就看過一些關于NoSql的文章&#xff0c;還記得當時里面有介紹MongoDB的&#xff0c;多瞅了2眼&#xff0c;并且在Window下安裝了MongoDB的驅動&#xff0c;小玩了會。今天重新翻出來&#xff0c;沒成想在命…

牛頓法與擬牛頓法,SDM方法的一些注記

SDM方法 考慮一般額NLS問題&#xff1a; f(x)minx||h(x)?y||2這里x為優化參數&#xff0c;h為非線性函數&#xff0c;y是已知變量&#xff0c;如下是基于梯度的迭代公式&#xff1a; ΔxαAJTh(h(x)?y)這里α是步長&#xff0c;A是縮放因子&#xff0c;Jh是h在當前參數x下的…

pyqt5從子目錄加載qrc文件_實戰PyQt5: 045-添加資源文件

添加資源文件在使用PyQt進行圖形界面開發的時候不免要用到一些外部資源&#xff0c;比如圖片&#xff0c;qss配置文件等。在前面代碼中&#xff0c;遇到這類問題&#xff0c;我們使用絕對路徑的方式來解決&#xff0c;這種方式&#xff0c;本身有其不方便之處(比如&#xff0c;…

《 Python樹莓派編程》——2.7 總結

本節書摘來自華章出版社《Python樹莓派編程》一書中的第2章&#xff0c;第2.7節&#xff0c;作者&#xff1a;[美]沃爾弗拉姆多納特&#xff08;Wolfram Donat&#xff09;著 韓德強 等譯&#xff0c;更多章節內容可以訪問云棲社區“華章計算機”公眾號查看。 2.7 總結 本章簡…

ACM的輸入輸出總結

關于ACM的輸入輸出&#xff08;一&#xff09; 一般來說ACM的現場賽會規定輸入輸出 或者是文件輸入標準輸出 也可能是文件輸入文件輸出 如果沒有規定的話那么一般就是標準的輸入輸出了 那說一下輸入輸出的重定向 一般用下面兩種方法 c常用: #include <fstream.h>ifstream…

hdu 2064漢諾塔III 遞推

漢諾塔遞推題&#xff0c;比漢諾塔多了一個限制條件&#xff0c;盤子只允許在相鄰的柱子之間移動。 分析&#xff1a; 第1步:初始狀態&#xff1b; 第2步:把上面的n-1個盤移到第3號桿上&#xff1b; 第3步:把第n個盤從1移到2&#xff1b; 第4步:把前n-1個從3移到1&#xff0c;給…