-
Notifications
You must be signed in to change notification settings - Fork 5
/
bert_train_predict.py
370 lines (310 loc) · 14.5 KB
/
bert_train_predict.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import transformers
import torch
import pandas as pd
import argparse
import random
import numpy as np
from sklearn.metrics import classification_report, roc_auc_score, precision_recall_fscore_support
from sklearn.preprocessing import MultiLabelBinarizer
from transformers import Trainer, TrainingArguments, AutoTokenizer, AutoModelForSequenceClassification, AutoConfig, BertForSequenceClassification
from ray.tune.schedulers import PopulationBasedTraining, ASHAScheduler
import ray
from ray import tune
from ray.tune import CLIReporter
from datasets import Dataset, load_dataset, DatasetDict, concatenate_datasets
from functools import partial
from utils import grade_preproc, group_labels, undersample_dataset, data_split
import os
from collections import Counter
import pathlib
from peft import LoraConfig, get_peft_model, prepare_model_for_int8_training, TaskType
from torch import nn
from ray.tune.search.bayesopt import BayesOptSearch
from ray.tune.search.hyperopt import HyperOptSearch
from sklearn.utils import class_weight
# Disable logging for raytune, but it will still make folders and jsons for experiment states
# They're not big files, but should be deleted PATH: ./to_be_deleted_rayArtifact
os.environ["TUNE_DISABLE_AUTO_CALLBACK_LOGGERS"] = "1"
parser = argparse.ArgumentParser()
parser.add_argument('--logdir', type=str, help='The path to the directory to temporarily store checkpoints')
parser.add_argument('--evaldir', type=str, help='The path to the directory to store model evaluation results')
parser.add_argument('--num_trials', type=int, help='Number hyperparameter trials', default=5)
parser.add_argument('--seqlens', type=str, help='list of sequence lengths to search for ray', default='20,35,50')
parser.add_argument('--batches', type=str, help='list of batch sizes to search for ray', default='32,64,128')
parser.add_argument('--model', type=str, help='select model to run classification: (BERT, ROBERTA, BIOBERT)', default='bert-base-uncased')
parser.add_argument('--synth_data', type=str, help='path to synthetic data file', default='')
parser.add_argument('--undersample', type=float, default=0.0, help='undersample majority class in train set by proportion. E.g. 0.2 will keep 20 percent of majority class data')
parser.add_argument('--ray', action='store_true', help='tune hyperparameters')
parser.add_argument('--adverse', action='store_true', help='for non adverse synthetic data')
parser.add_argument('--epochs', type=int, default=5)
args = parser.parse_args()
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
SEED_VAL = 42
random.seed(SEED_VAL)
np.random.seed(SEED_VAL)
torch.manual_seed(SEED_VAL)
torch.cuda.manual_seed_all(SEED_VAL)
MLB = MultiLabelBinarizer()
if args.adverse:
LABELS = {'TRANSPORTATION_distance', 'TRANSPORTATION_resource',
'TRANSPORTATION_other', 'HOUSING_poor', 'HOUSING_undomiciled','HOUSING_other',
'RELATIONSHIP_divorced', 'RELATIONSHIP_widowed', 'RELATIONSHIP_single',
'PARENT','EMPLOYMENT_underemployed','EMPLOYMENT_unemployed', 'EMPLOYMENT_disability','SUPPORT_minus'}
else:
LABELS = {'TRANSPORTATION_distance', 'TRANSPORTATION_resource',
'TRANSPORTATION_other', 'HOUSING_poor', 'HOUSING_undomiciled',
'HOUSING_other', 'RELATIONSHIP_married', 'RELATIONSHIP_partnered',
'RELATIONSHIP_divorced', 'RELATIONSHIP_widowed', 'RELATIONSHIP_single',
'PARENT','EMPLOYMENT_employed', 'EMPLOYMENT_underemployed',
'EMPLOYMENT_unemployed', 'EMPLOYMENT_disability', 'EMPLOYMENT_retired',
'EMPLOYMENT_student', 'SUPPORT_plus', 'SUPPORT_minus'}
BROAD_LABELS = {lab.split('_')[0] for lab in LABELS}
BROAD_LABELS.add('<NO_SDOH>')
LABEL_BROAD_NARROW = LABELS.union(BROAD_LABELS)
if args.ray:
ray.init(log_to_driver=False)
class BCETrainer(Trainer):
def compute_loss(self, model, inputs, return_outputs=False):
labels = inputs.get("labels").to(DEVICE) # batch[0, 1, 0, 1, 0, 0]
# forward pass
outputs = model(inputs['input_ids'])
logits = outputs.get("logits").to(DEVICE)
# compute custom loss (suppose one has 3 labels with different weights)
loss_fct = nn.BCEWithLogitsLoss().to(DEVICE)
loss = loss_fct(logits.to(DEVICE), labels.float().to(DEVICE))
return (loss, outputs) if return_outputs else loss
def undersample(df, label, keep_percent):
"""
Undersamples the majority class in a Pandas dataframe to balance the classes.
Parameters:
df (pandas.DataFrame): The dataframe to undersample.
keep_percent (float): The percentage of the majority class to keep.
Returns:
pandas.DataFrame: The undersampled dataframe.
"""
# Find the majority class based on the labels column
counts = df[label].value_counts()
majority_class = counts.idxmax()
# Get the indices of rows in the majority class
majority_indices = df[df[label] == majority_class].index
# Calculate the number of majority class rows to keep
num_majority_keep = int(keep_percent * counts[majority_class])
# Get a random subset of the majority class rows to keep
majority_keep_indices = np.random.choice(majority_indices, num_majority_keep, replace=False)
# Get the indices of rows in the minority class
minority_indices = df[df[label] != majority_class].index
# Combine the majority class subset and the minority class rows
undersampled_indices = np.concatenate([majority_keep_indices, minority_indices])
# Return the undersampled dataframe
return df.loc[undersampled_indices]
def generate_label_list(row: pd.DataFrame) -> str:
"""
Generate a label list based on the given row from a Pandas DataFrame.
Args:
row (pd.DataFrame): A row from a Pandas DataFrame.
Returns:
str: A comma-separated string of labels extracted from the row.
Examples:
>>> df = pd.DataFrame({'label1_1': [1], 'label2_0': [0], 'label3_1': [1]})
>>> generate_label_list(df.iloc[0])
'label1,label3'
>>> df = pd.DataFrame({'label2_0': [0], 'label3_0': [0]})
>>> generate_label_list(df.iloc[0])
'<NO_SDOH>'
"""
labels = set()
for col_name, value in row.items():
if col_name in LABELS and value == 1:
labels.add(col_name.split('_')[0])
if len(labels) == 0:
labels.add('<NO_SDOH>')
return ','.join(list(labels))
def compute_metrics(pred):
"""
Calculate Evaluation metrics
"""
labels = pred.label_ids
logits = torch.tensor(pred.predictions)
act = nn.Sigmoid()
probs = act(logits)
preds = (probs>= 0.5).int()
# labels = mlb.fit_transform(labels)
# preds = MLB.transform(preds)
prec, rec, f1, _ = precision_recall_fscore_support(labels, preds)
micro_f1 = precision_recall_fscore_support(labels, preds, average='micro')[2]
weight_f1 = precision_recall_fscore_support(labels, preds, average='weighted')[2]
macro_f1 = precision_recall_fscore_support(labels, preds, average='macro')[2]
metrics_out = {'macro_f1':macro_f1, 'micro_f1': micro_f1, 'weighted_f1': weight_f1}
for i, lab in enumerate(list(MLB.classes_)):
metrics_out['precision_'+str(lab)] = prec[i]
metrics_out['recall_'+str(lab)] = rec[i]
metrics_out['f1_'+str(lab)] = f1[i]
print(classification_report(labels, preds, target_names=MLB.classes_))
return metrics_out
def train_hf(config, dataset):
# Define the Trainer and TrainingArguments objects
# Initialize the tokenizer with the sequence_length parameter
tokenizer = AutoTokenizer.from_pretrained(args.model, use_fast=True)
def tokenize(batch):
return tokenizer(batch['text'], padding='max_length', truncation=True, return_tensors="pt", max_length=config["sequence_length"])
tokenized_dataset = dataset.map(tokenize, batched=True, remove_columns=["text"])
training_args = TrainingArguments(
output_dir=args.logdir,
per_device_train_batch_size=config["batch_size"],
per_device_eval_batch_size=config["batch_size"],
learning_rate=config["learning_rate"],
num_train_epochs=config["epochs"],
disable_tqdm=False,
bf16=True, # bfloat16 training
optim='adamw_hf',
logging_dir=f"{args.logdir}/logs",
overwrite_output_dir = True,
evaluation_strategy = 'epoch',
weight_decay= config["weight_decay"],
save_strategy='epoch',
save_total_limit = 1,
load_best_model_at_end=True,
metric_for_best_model="macro_f1",
seed = SEED_VAL,
gradient_accumulation_steps = config["gradient_accumulation_steps"]
)
model = AutoModelForSequenceClassification.from_pretrained(
pretrained_model_name_or_path=args.model,
num_labels=len(dataset['train']['labels'][0]),
attention_probs_dropout_prob=config["hidden_dropout_prob"],
hidden_dropout_prob=config["hidden_dropout_prob"]
)
# clws = torch.tensor([config["class_weight0"], config["class_weight1"]], dtype=torch.float).to(DEVICE)
trainer = BCETrainer(
args=training_args,
tokenizer=tokenizer,
train_dataset=tokenized_dataset['train'],
eval_dataset=tokenized_dataset['dev'],
model=model,
compute_metrics=compute_metrics,
)
# Train the model and return the evaluation
trainer.train()
eval_result = trainer.evaluate()
if args.ray:
tune.report(eval_result)
else:
return eval_result
def main(args):
train_data = pd.read_csv('./data/train_sents.csv')
dev_data = pd.read_csv('./data/dev_sents.csv')
train_data.fillna(value={'text':''}, inplace=True)
dev_data.fillna(value={'text':''}, inplace=True)
dev_text = dev_data['text'].tolist()
dev_labels = dev_data.apply(generate_label_list, axis=1).tolist()
train_data['LABEL'] = train_data.apply(generate_label_list, axis=1).tolist()
if args.undersample:
train_data = undersample(train_data, label='LABEL', keep_percent=args.undersample)
train_text = train_data['text'].tolist()
train_labels = train_data['LABEL'].tolist()
if args.synth_data:
synthetic_data = pd.read_csv(args.synth_data)
if args.adverse:
synthetic_data = synthetic_data[synthetic_data['adverse']=='adverse']
synthetic_data.reset_index(inplace=True, drop=True)
binary_synthetic = pd.get_dummies(synthetic_data['label'])
binary_synthetic['text'] = synthetic_data['text']
synth_labels = binary_synthetic.apply(generate_label_list, axis=1).tolist()
synth_text = synthetic_data['text'].tolist()
train_text.extend(synth_text)
train_labels.extend(synth_labels)
train_labels = [labs.split(',') for labs in train_labels]
train_labs_mlb = MLB.fit_transform(train_labels)
train_labs_mlb = [ar.tolist() for ar in train_labs_mlb]
dev_labels = [labs.split(',') for labs in dev_labels]
dev_labs_mlb = MLB.transform(dev_labels)
dev_labs_mlb = [ar.tolist() for ar in dev_labs_mlb]
train_t5 = pd.DataFrame({'text':train_text, 'labels':train_labs_mlb})
dev_t5 = pd.DataFrame({'text':dev_text, 'labels':dev_labs_mlb})
train_dataset = Dataset.from_pandas(train_t5)
dev_dataset = Dataset.from_pandas(dev_t5)
dataset = DatasetDict()
dataset['train'] = train_dataset
dataset['dev'] = dev_dataset
seq_length_search = [int(x) for x in args.seqlens.split(',')]
batch_size_search = [int(x) for x in args.batches.split(',')]
params_dict ={
'model':args.model,
'undersample_bool':args.undersample
}
if args.ray:
if args.undersample:
usample = args.undersample
else:
usample = 1
config_space = {
"learning_rate": tune.loguniform(1e-5, 1e-3),
"batch_size": tune.choice(batch_size_search),
"hidden_dropout_prob": tune.uniform(0.1, 0.5),
"undersample": usample,
"weight_decay": tune.loguniform(1e-8, 1e-5),
"sequence_length": tune.choice(seq_length_search),
"gradient_accumulation_steps": 3,
"epochs": args.epochs
}
scheduler = ASHAScheduler(
metric="_metric/eval_macro_f1",
mode="max",
grace_period=1,
reduction_factor=2
)
met_cols = ["training_iteration","macro_f1", "micro_f1", "precision", "recall"]
for i in range(len(train_labs_mlb[0])):
met_cols.append('precision_'+str(i))
met_cols.append('recall_'+str(i))
met_cols.append('f1_'+str(i))
reporter = CLIReporter(
parameter_columns=list(config_space.keys()),
metric_columns=met_cols,
)
result = tune.run(
partial(train_hf,dataset=dataset),
config=config_space,
num_samples=args.num_trials,
resources_per_trial={"gpu": 1},
scheduler=scheduler,
progress_reporter=reporter,
local_dir="./to_be_deleted_rayArtifact",
name='empty_folders',
log_to_file=False,
)
best_trial = result.get_best_trial(metric='_metric/eval_macro_f1', mode='max', scope="all")
config_dict = best_trial.config
dev_eval_dict = best_trial.last_result['_metric']
output_dict = {**params_dict, **config_dict, **dev_eval_dict}
outpath = pathlib.Path().joinpath(args.evaldir, 'multi_BERT_ray.csv')
print(output_dict)
if os.path.isfile(outpath):
indf = pd.read_csv(outpath)
outdf = pd.concat([indf, pd.DataFrame([output_dict])], ignore_index=True)
else:
outdf = pd.DataFrame([output_dict])
outdf.to_csv(outpath, index=False)
else:
config_space = {
"learning_rate": 5e-5,
"batch_size":32, #32
"hidden_dropout_prob": 0.1,
"undersample": 1.0,
"weight_decay": 2e-8,
"sequence_length": 100,
"gradient_accumulation_steps": 3,
"epochs": 10
}
dev_eval_dict = train_hf(config_space, dataset)
output_dict = {**params_dict, **config_space, **dev_eval_dict}
outpath = pathlib.Path().joinpath(args.evaldir, 'multi_BERT_noray.csv')
print(output_dict)
if os.path.isfile(outpath):
indf = pd.read_csv(outpath)
outdf = pd.concat([indf, pd.DataFrame([output_dict])], ignore_index=True)
else:
outdf = pd.DataFrame([output_dict])
outdf.to_csv(outpath, index=False)
if __name__ =='__main__':
main(args)