-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlime_explainer.py
252 lines (224 loc) · 9.42 KB
/
lime_explainer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import argparse
from pathlib import Path
from typing import List, Any
import numpy as np
from lime.lime_text import LimeTextExplainer
import sklearn.pipeline
import scipy.stats
import spacy
METHODS = {
'textblob': {
'class': "TextBlobExplainer",
'file': None,
'name': "TextBlob",
'lowercase': False,
},
'vader': {
'class': "VaderExplainer",
'file': None,
'name': "VADER",
'lowercase': False,
},
'logistic': {
'class': "LogisticExplainer",
'file': "data/sst/sst_train.txt",
'name': "Logistic Regression",
'lowercase': False,
},
'svm': {
'class': "SVMExplainer",
'file': "data/sst/sst_train.txt",
'name': "Support Vector Machine",
'lowercase': False,
},
'fasttext': {
'class': "FastTextExplainer",
'file': "models/fasttext/sst5_hyperopt.ftz",
'name': "FastText",
'lowercase': True,
},
}
def tokenizer(text: str) -> str:
"Tokenize input string using a spaCy pipeline"
nlp = spacy.blank('en')
nlp.add_pipe('sentencizer') # Very basic NLP pipeline in spaCy
doc = nlp(text)
tokenized_text = ' '.join(token.text for token in doc)
return tokenized_text
def explainer_class(method: str, filename: str) -> Any:
"Instantiate class using its string name"
classname = METHODS[method]['class']
class_ = globals()[classname]
return class_(filename)
class VaderExplainer:
"""Class to explain classification results of Vader.
Although VADER compound scores are in the range [-1.0, 1.0], we `simulate` the
probabilities that the model predicts using 5 equally-sized bins in this interval.
and using a normal distribution to artificially create class probabilities.
For example:
If Vader predicts a float sentiment score of 0.6834, this translates to an
integer-scaled class prediction of 4, assuming equally-sized bins for 5 classes.
We take this value and generate a normal distribution PDF with exactly 5 values.
The PDF is used as a simulated probability of classes that we feed to the LIME explainer.
"""
def __init__(self, model_file: str = None) -> None:
try:
from nltk.sentiment.vader import SentimentIntensityAnalyzer
except:
import nltk
nltk.download('vader_lexicon')
self.vader = SentimentIntensityAnalyzer()
self.classes = np.array([1, 2, 3, 4, 5])
def score(self, text: str) -> float:
return self.vader.polarity_scores(text)['compound']
def predict(self, texts: List[str]) -> np.array([float, ...]):
probs = []
for text in texts:
# First, offset the float score from the range [-1, 1] to a range [0, 1]
offset = (self.score(text) + 1) / 2.
# Convert float score in [0, 1] to an integer value in the range [1, 5]
binned = np.digitize(5 * offset, self.classes) + 1
# Similate probabilities of each class based on a normal distribution
simulated_probs = scipy.stats.norm.pdf(self.classes, binned, scale=0.5)
probs.append(simulated_probs)
return np.array(probs)
class LogisticExplainer:
"""Class to explain classification results of a scikit-learn
Logistic Regression Pipeline. The model is trained within this class.
"""
def __init__(self, path_to_train_data: str) -> None:
"Input training data path for training Logistic Regression classifier"
import pandas as pd
# Read in training data set
self.train_df = pd.read_csv(path_to_train_data, sep='\t', header=None, names=["truth", "text"])
self.train_df['truth'] = self.train_df['truth'].str.replace('__label__', '')
# Categorical data type for truth labels
self.train_df['truth'] = self.train_df['truth'].astype(int).astype('category')
def train(self) -> sklearn.pipeline.Pipeline:
"Create sklearn logistic regression model pipeline"
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
pipeline = Pipeline(
[
('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', LogisticRegression(
solver='newton-cg',
multi_class='multinomial',
random_state=42,
max_iter=100,
)),
]
)
# Train model
classifier = pipeline.fit(self.train_df['text'], self.train_df['truth'])
return classifier
def predict(self, texts: List[str]) -> np.array([float, ...]):
"""Generate an array of predicted scores (probabilities) from sklearn
Logistic Regression Pipeline."""
classifier = self.train()
probs = classifier.predict_proba(texts)
return probs
class SVMExplainer:
"""Class to explain classification results of a scikit-learn linear Support Vector Machine
(SVM) Pipeline. The model is trained within this class.
"""
def __init__(self, path_to_train_data: str) -> None:
"Input training data path for training Logistic Regression classifier"
import pandas as pd
# Read in training data set
self.train_df = pd.read_csv(path_to_train_data, sep='\t', header=None, names=["truth", "text"])
self.train_df['truth'] = self.train_df['truth'].str.replace('__label__', '')
# Categorical data type for truth labels
self.train_df['truth'] = self.train_df['truth'].astype(int).astype('category')
def train(self) -> sklearn.pipeline.Pipeline:
"Create sklearn logistic regression model pipeline"
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
pipeline = Pipeline(
[
('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', SGDClassifier(
loss='modified_huber',
penalty='l2',
alpha=1e-3,
random_state=42,
max_iter=100,
tol=None,
)),
]
)
# Train model
classifier = pipeline.fit(self.train_df['text'], self.train_df['truth'])
return classifier
def predict(self, texts: List[str]) -> np.array([float, ...]):
"""Generate an array of predicted scores (probabilities) from sklearn
Logistic Regression Pipeline."""
classifier = self.train()
probs = classifier.predict_proba(texts)
return probs
def explainer(method: str,
path_to_file: str,
text: str,
lowercase: bool,
num_samples: int) -> LimeTextExplainer:
"""Run LIME explainer on provided classifier"""
model = explainer_class(method, path_to_file)
predictor = model.predict
# Lower case the input text if requested (for certain classifiers)
if lowercase:
text = text.lower()
# Create a LimeTextExplainer
explainer = LimeTextExplainer(
# Specify split option
split_expression=lambda x: x.split(),
# Our classifer uses trigrams or contextual ordering to classify text
# Hence, order matters, and we cannot use bag of words.
bow=False,
# Specify class names for this case
class_names=[1, 2, 3, 4, 5]
)
# Make a prediction and explain it:
exp = explainer.explain_instance(
text,
classifier_fn=predictor,
top_labels=1,
num_features=20,
num_samples=num_samples,
)
return exp
def main(samples: List[str]) -> None:
# Get list of available methods:
method_list = [method for method in METHODS.keys()]
# Arguments
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--method', type=str, nargs='+', help="Enter one or more methods \
(Choose from following: {})".format(", ".join(method_list)),
required=True)
parser.add_argument('-n', '--num_samples', type=int, help="Number of samples for explainer \
instance", default=1000)
args = parser.parse_args()
for method in args.method:
if method not in METHODS.keys():
parser.error("Please choose from the below existing methods! \n{}".format(", ".join(method_list)))
path_to_file = METHODS[method]['file']
ENABLE_LOWER_CASE = METHODS[method]['lowercase']
# Run explainer function
print("Method: {}".format(method.upper()))
for i, text in enumerate(samples):
text = tokenizer(text) # Tokenize text using spaCy before explaining
print("Generating LIME explanation for example {}: `{}`".format(i+1, text))
exp = explainer(method, path_to_file, text, ENABLE_LOWER_CASE, args.num_samples)
# Output to HTML
output_filename = Path(__file__).parent / "{}-explanation-{}.html".format(i+1, method)
exp.save_to_file(output_filename)
if __name__ == "__main__":
# Evaluation text
samples = [
"It 's not horrible , just horribly mediocre .",
"The cast is uniformly excellent ... but the film itself is merely mildly charming .",
]
main(samples)