-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlime_script.py
179 lines (150 loc) · 5.41 KB
/
lime_script.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
import dataclasses
import datetime
import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple
import uuid
from functools import partial
import numpy as np
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
from transformers import (
AutoConfig,
AutoModelForSequenceClassification,
AutoTokenizer,
EvalPrediction,
GlueDataset,
AutoModel,
AutoModelForTokenClassification,
BertForTokenClassification,
)
from transformers import GlueDataTrainingArguments as DataTrainingArguments
from transformers import (
HfArgumentParser,
Trainer,
TrainingArguments,
set_seed,
)
from lime.lime_text import LimeTextExplainer
from utils.tsv_dataset import (
convert_examples_to_features,
InputExample,
TSVClassificationDataset,
)
import uuid
import torch
from torch.nn import CrossEntropyLoss, MSELoss
from utils.tsv_dataset import TSVClassificationDataset, Split, get_labels
from utils.arguments import (
datasets,
DataTrainingArguments,
ModelArguments,
parse_config,
)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def classify_sentence(input_words_str_lst, model, train_dataset, batch_size=64):
input_words_lst = [
input_words_str.split() for input_words_str in input_words_str_lst
]
inp_feats_lst = convert_examples_to_features(
[
InputExample(
guid=uuid.uuid4().hex,
words=input_words,
labels=[model.config.id2label[0]]
* len(input_words), # fill with dummy label
)
for input_words in input_words_lst
],
**train_dataset.convert_features_dict
)
input_ids = [inp_feats.input_ids for inp_feats in inp_feats_lst]
sm = torch.nn.Softmax(dim=1)
final_res = None
for batch_idx in range(0, len(input_ids), batch_size):
curr_input_ids = input_ids[batch_idx : batch_idx + batch_size]
data = {}
data["input_ids"] = torch.tensor(curr_input_ids).to(device)
res = model(**data)
res_sm = sm(res[0])
numpy_res = res_sm.detach().cpu().numpy()
if final_res is not None:
final_res = np.append(final_res, numpy_res, axis=0)
else:
final_res = numpy_res
# cleanup memory before next batch
del data["input_ids"]
del res
del res_sm
torch.cuda.empty_cache()
return final_res
if __name__ == "__main__":
if len(sys.argv) != 3:
logger.error("Required args: [config_path] [gpu_ids]")
exit()
config_dict = parse_config(sys.argv[1])
os.environ["CUDA_VISIBLE_DEVICES"] = str(sys.argv[2])
output_path = config_dict["output_file"].format(
model_name=config_dict["model_name"],
dataset_name=config_dict["dataset"],
experiment_name=config_dict["experiment_name"],
datetime=datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%S"),
)
set_seed(config_dict["seed"])
path = config_dict["model_path"]
tokenizer = AutoTokenizer.from_pretrained(config_dict["model_name"],)
model = AutoModelForSequenceClassification.from_pretrained(path)
labels = [model.config.id2label[0], model.config.id2label[1]]
model_args = ModelArguments(model_name_or_path=config_dict["model_name"])
data_args = datasets[config_dict["dataset"]]
data_config = dict(
data_dir=data_args.data_dir,
tokenizer=tokenizer,
labels=labels,
model_type=model.config.model_type,
max_seq_length=data_args.max_seq_length,
overwrite_cache=data_args.overwrite_cache,
file_name=data_args.file_name,
make_all_labels_equal_max=config_dict["make_all_labels_equal_max"],
default_label=config_dict["test_label_dummy"],
is_seq_class=config_dict["is_seq_class"],
lowercase=config_dict["lowercase"],
)
train_dataset = TSVClassificationDataset(mode=Split.train, **data_config)
if config_dict["dataset_split"] == "train":
dataset = train_dataset
elif config_dict["dataset_split"] == "dev":
dataset = TSVClassificationDataset(mode=Split.dev, **data_config)
elif config_dict["dataset_split"] == "test":
dataset = TSVClassificationDataset(mode=Split.test, **data_config)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
explainer = LimeTextExplainer(
class_names=(0, 1),
bow=False, # try with True as well: False causes masking to be done, True means removing words
mask_string=tokenizer.mask_token,
feature_selection="none", # use all features
split_expression=r"\s",
)
classify_sentence_partial = partial(
classify_sentence,
model=model,
train_dataset=train_dataset,
batch_size=config_dict["per_device_eval_batch_size"],
)
res_list = []
for i in range(0, len(dataset)):
if i % 50 == 0:
logger.info(i)
exp = explainer.explain_instance(
" ".join(dataset.examples[i].words),
classify_sentence_partial,
labels=(1,),
num_samples=config_dict["lime_num_samples"],
)
lst = exp.as_map()[1]
lst.sort(key=(lambda x: x[0]))
dataset.examples[i].predictions = list(map(lambda x: x[1], lst))
dataset.write_preds_to_file(output_path, dataset.examples)