-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl_conf2.py
281 lines (265 loc) · 11.3 KB
/
cl_conf2.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# supervised text classification: configuration#1
# last maintained: 2024-05-12 15:22:51
import torch
cossim = torch.nn.CosineSimilarity(dim=0)
import datasets
import transformers
from sklearn.model_selection import train_test_split
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AdamW
from torch.optim import AdamW
import pandas as pd
import json
import tinydb
from datasets.utils.logging import disable_progress_bar
#disable_progress_bar()
import get_tword_vectors as gtv
#
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
#
from sklearn.metrics import accuracy_score, f1_score
def compute_metrics(pred):
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
f1 = f1_score(labels, preds, average="weighted")
acc = accuracy_score(labels, preds)
return {"accuracy": acc, "f1": f1}
#
from transformers import TrainingArguments
from transformers import Trainer
from transformers import EarlyStoppingCallback
#bert_base = 'bert-large-uncased'
#bert_base = 'roberta-base'
#bert_base = 'roberta-large'
bert_base = 'bert-base-uncased'
#
bert_model_name = bert_base
tokenizer = AutoTokenizer.from_pretrained(bert_model_name)
#
from transformers import AutoConfig
from transformers import BertConfig
from transformers.modeling_outputs import TokenClassifierOutput, SequenceClassifierOutput
from transformers.models.bert.modeling_bert import BertModel
from transformers.models.bert.modeling_bert import BertPreTrainedModel
from torch import nn
#
class WiC_with_Bert(BertPreTrainedModel):
config_class = BertConfig
#
def __init__(self, config, wv_dim, dv_dim=512):
super().__init__(config)
self.num_labels = config.num_labels
self.bert = BertModel(config)
self.hidden_size = config.hidden_size
self.wv_dim = wv_dim
self.dv_dim = dv_dim
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.linear = nn.Sequential(
nn.Linear(config.hidden_size, dv_dim),
)
if wv_dim==0:
self.classifier = nn.Sequential(
nn.Linear(dv_dim, 128), # 2023-10-04 20:28:00
nn.ReLU(inplace=True),
nn.Dropout(0.25),
nn.Linear(128, config.num_labels), # 2023-10-04 20:41:47
)
elif wv_dim==-1:
self.classifier = nn.Sequential(
nn.Linear(dv_dim+config.hidden_size*2, 128), # 2023-10-04 20:28:00
nn.ReLU(inplace=True),
nn.Dropout(0.25),
nn.Linear(128, config.num_labels), # 2023-10-04 20:41:47
)
else:
self.classifier = nn.Sequential(
nn.Linear(dv_dim+self.wv_dim*2, 128), # 2023-10-04 20:28:00
nn.ReLU(inplace=True),
nn.Dropout(0.25),
nn.Linear(128, config.num_labels), # 2023-10-04 20:41:47
)
if wv_dim!=0 and wv_dim!=-1: # use FF layeer; wv_dim==0: not using vectors; wv_dim=-1: not using FF
self.feedforward = nn.Linear(config.hidden_size*2, self.wv_dim*2, config.num_labels)
print('Classifier:', self.classifier)
self.init_weights()
#
def forward(self, input_ids=None, attention_mask=None, token_type_ids=None,
labels=None, w1_idx=None, w2_idx=None, **kwargs):
bert_outputs = self.bert(input_ids, attention_mask=attention_mask,
token_type_ids=token_type_ids,
**kwargs)
dropout_output = self.dropout(self.linear(bert_outputs[1])) # 2023-10-04 20:23:46 just for probing
if self.wv_dim==0: # do not use bert word vectors
merged_output = dropout_output
else: # use bert word vectors
w1_vector, w2_vector = gtv.get_tword_vectors_(bert_outputs[0], input_ids, w1_idx, w2_idx)
w1_vector = torch.stack(w1_vector, dim=0)
w2_vector = torch.stack(w2_vector, dim=0)
if self.wv_dim==-1: # use bert word vectors as is by concanenation (not using FF)
merged_output = torch.concat([dropout_output, w1_vector, w2_vector], dim=1)
else: # use bert word vectors with a FF layer
mult_vector = w1_vector * w2_vector
diff_vector = torch.abs(w1_vector - w2_vector)
ff_vector = self.feedforward(torch.cat([mult_vector, diff_vector], dim=1))
merged_output = torch.concat([dropout_output, ff_vector], dim=1)
#
logits = self.classifier(merged_output)
if labels is not None:
loss_function = nn.CrossEntropyLoss()
loss = loss_function(logits.view(-1, self.num_labels), labels.view(-1))
#
return SequenceClassifierOutput(loss=loss, logits=logits)
#
def evaluate(trainer_obj, test_ds, verbose=True):
eval_obj = trainer_obj.predict(test_ds)
golds = eval_obj.label_ids
preds = [np.argmax(_) for _ in eval_obj.predictions]
conf_matrix = confusion_matrix(golds, preds)
class_report = classification_report(golds, preds)
accuracy = accuracy_score(golds, preds)
if verbose:
print(conf_matrix)
print(class_report)
print('Accuracy:', accuracy)
return eval_obj, golds, preds, accuracy, conf_matrix, class_report
#
def train(ds, epochs, lr, batch_size, patience):
global wic_with_bert_model
#
model = wic_with_bert_model
optimizer = AdamW(model.parameters(), lr=lr)
#
train_ds = ds['train']
val_ds = ds['validation']
test_ds = ds['test']
#
batch_size = batch_size
logging_steps = len(train_ds) // batch_size
model_name = "cl_conf2"
#
training_args = TrainingArguments(
output_dir=model_name,
num_train_epochs=epochs,
learning_rate=lr,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
weight_decay=0.01,
evaluation_strategy='epoch',
save_strategy='epoch',
save_on_each_node=1,
metric_for_best_model='accuracy',
load_best_model_at_end=True,
disable_tqdm=False,
#disable_tqdm=True,
logging_steps=logging_steps,
push_to_hub=False,
log_level='error',
lr_scheduler_type='linear',
warmup_ratio=0.1,
)
#
trainer = Trainer(
model=model,
args=training_args,
compute_metrics=compute_metrics,
train_dataset=train_ds,
eval_dataset=val_ds,
callbacks=[EarlyStoppingCallback(early_stopping_patience=patience)],
)
#
trainer.train()
#
return trainer
#
import numpy as np
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
###
import make_transformers_dataset as mtd
import argparse
import tinydb
###
def main(verbose=True, save_db=True):
global wic_with_bert_model
#
arg_p = argparse.ArgumentParser()
arg_p.add_argument('--verb', type=str, default='contrast', help='contrast, direct, direct2, or none')
arg_p.add_argument('--train_fname', type=str, default='./data_tsv/train.tsv', help='train data file')
arg_p.add_argument('--dev_fname', type=str, default='./data_tsv/dev.tsv', help='dev data file')
arg_p.add_argument('--test_fname', type=str, default='./data_tsv/test.tsv', help='test data file')
arg_p.add_argument('--llm', type=str, default='gpt3', help='model name; gpt3 or gpt4')
arg_p.add_argument('--seed', type=int, default=23, help='random seed')
arg_p.add_argument('--batch_size', type=int, default=16, help='batch size')
arg_p.add_argument('--epochs', type=int, default=20, help='number of maximum epochs')
arg_p.add_argument('--lr', type=float, default=4e-5, help='learning rate')
arg_p.add_argument('--patience', type=int, default=5, help='early stopping patience')
arg_p.add_argument('--wv_dim', type=int, default=0, help='word vector dimensionality')
arg_p.add_argument('--dv_dim', type=int, default=512, help='[CLS] vector dimensionality') # 2023-10-04 20:54:22
arg_p.add_argument('--n_train', type=int, default=5428, help='number of training data instances') # first n instances
arg_p.add_argument('--dev_idxs', type=list, default=[], help='list of instance id indices')
arg_p.add_argument('--test_idxs', type=list, default=[], help='list of instance id indices')
arg_p.add_argument('--save_db', type=str, default="True", help='save the evaluation results in db')
arg_p.add_argument('--verbose', type=str, default="True", help='verbose')
args = arg_p.parse_args()
#
verbose = args.verbose
#
if args.save_db == 'True' or args.save_db == 'true':
save_db = True
else:
save_db = False
#
transformers.set_seed(args.seed)
#
wic_with_bert_config = AutoConfig.from_pretrained(bert_model_name, num_labels=2)
wic_with_bert_model = (WiC_with_Bert.from_pretrained(bert_model_name,
config=wic_with_bert_config, wv_dim=args.wv_dim, dv_dim=args.dv_dim).to(device))
#
if verbose: print('Parameters:', args)
#
# pretrained contenxt-dependent embeddings for w1 and w2
print('>>> In Preparation')
#
if verbose:
print('train, dev, test files:', args.train_fname, args.dev_fname, args.test_fname)
#
ds = mtd.make_transformers_dataset(args.verb, args.llm,
args.train_fname, args.dev_fname, args.test_fname,
range(args.n_train), args.dev_idxs, args.test_idxs,
)
#
if verbose: print(ds)
#
print('>>> In Training')
trainer_obj = train(ds,
batch_size=args.batch_size, epochs=args.epochs, lr=args.lr, patience=args.patience)
print('>>> Evaluation')
val_result = trainer_obj.evaluate()
print('--- Validation results ---')
print(val_result)
test_result = trainer_obj.evaluate(ds['test'])
print('--- Test results ---')
print(test_result)
eval_obj, golds, preds, accuracy, confusion_matrix, classification_report = evaluate(trainer_obj, ds['test'], verbose)
#
if save_db:
wic_db_path = './wic_results_db/'
wic_cl_results_db = tinydb.TinyDB(wic_db_path + 'cl_config_2' + '.json')
rslt_table = wic_cl_results_db.table('_'.join([args.verb, args.llm, str(args.wv_dim), str(args.seed), str(args.n_train)]))
save_results_in_db(rslt_table, args.verb, args.llm, args.seed, args.wv_dim,
json.dumps(golds.tolist()), str(preds),
accuracy, json.dumps(confusion_matrix.tolist()), classification_report,
json.dumps(val_result), json.dumps(test_result))
#
print('Accuracy:', accuracy, flush=True)
return trainer_obj, eval_obj, golds, preds
#
def save_results_in_db(rslt_table, desc, llm, seed, wv_dim,
golds, preds, accuracy, confusion_matrix, classification_report,
val_result, test_result):
rslt_table.insert({'desc':desc, 'llm':llm, 'seed':seed, 'wv_dim':wv_dim,
'golds':golds, 'preds':preds,
'accuracy':accuracy, 'confusion_matrix':confusion_matrix,
'classfication_report':classification_report,
'val_result':val_result, 'test_result':test_result})
#####
if __name__ == '__main__':
main()