-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
354 lines (306 loc) · 14 KB
/
train.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
from __future__ import print_function
from cProfile import run
import pandas as pd
import numpy as np
import torch
import random
# Ignore excessive warnings
import logging
logging.propagate = False
logging.getLogger().setLevel(logging.ERROR)
# WandB – Import the wandb and hydra
import wandb
import hydra
from hydra.core.config_store import ConfigStore
from omegaconf import OmegaConf
#raytune
from ray import tune
from ray.tune import CLIReporter
from ray.tune.schedulers import PopulationBasedTraining
#huggingface
from datasets import load_dataset, concatenate_datasets
from transformers import TrainingArguments, EarlyStoppingCallback
from transformers import AutoTokenizer, AutoModelForSequenceClassification
#import helper functions
from src.n_proc import *
from src.n_metrics import *
from src.n_trainer_classes import *
# specifiy and clear device
torch.backends.cuda.matmul.allow_tf32 = True
torch.cuda.empty_cache()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@hydra.main(config_path="config", config_name="config")
def main(cfg) -> None:
print(OmegaConf.to_yaml(cfg))
MAX_LEN = int(cfg.run.max_len)
MODEL = cfg.run.hf_model
tokenizer = AutoTokenizer.from_pretrained(MODEL)
# log into wandb
wandb.login()
wandb.init(project=cfg.paths.project_name, entity="bitterman")
run_setup = [str(val) for _, val in cfg.run.items()]
run_setup = [i for i in run_setup if i != 'None']
wandb.run.name = '_'.join(run_setup)
print(wandb.run.name)
# Importing Data
dataset = load_dataset("csv", data_files={'train': cfg.paths.data + '/train.csv',
'valid': cfg.paths.data + '/dev.csv',
'test': cfg.paths.data + '/test.csv'})
# 'etrain': cfg.paths.data + '/etrain.csv',
# 'etest': cfg.paths.data + '/evalid.csv'})
try:
dataset = dataset.map(lambda x: grade2degree(x['Free Text Grade']))
except:
print('### using auged data ###')
print(dataset)
dataset['train'] = dataset['train'].filter(lambda example: example["g_or_s"]=='gold')
dataset['train'] = dataset['train'].filter(lambda example: example["g_or_s"]=='silver')
major_0_dataset = dataset['train'].filter(lambda example: example["degree"]==0)
minor_1_dataset = dataset['train'].filter(lambda example: example["degree"]==1)
minor_23_dataset = dataset['train'].filter(lambda example: example["degree"]>=2)
if float(cfg.run.downsample)>0:
ds = dataset['train'].filter(lambda example: example["degree"]<=1)
dataset['train'] = ds.train_test_split(test_size=float(cfg.run.downsample))['train']
print(dataset)
if int(cfg.run.num_classes) == 22:
# dataset['train'] = concatenate_datasets([dataset['train'], minor_1_dataset] ) #todo, ignore the ones
dataset['train'] = concatenate_datasets([dataset['train'], minor_23_dataset])
# dataset['train'] = concatenate_datasets([dataset['train'], minor_23_dataset])
# dataset['train'] = concatenate_datasets([dataset['train'], minor_23_dataset])
else:
dataset['train'] = concatenate_datasets([dataset['train'], minor_23_dataset])
# dataset['train'] = concatenate_datasets([dataset['train'], minor_23_dataset])
# dataset['train'] = concatenate_datasets([dataset['train'], minor_23_dataset])
# dataset['train'] = concatenate_datasets([dataset['train'], minor_1_dataset])
# dataset['train'] = concatenate_datasets([dataset['train'], minor_1_dataset])
print('### New sampled dataset stats ###')
print(dataset)
if bool(cfg.run.concat_eval):
dataset['test'] = concatenate_datasets([dataset['valid'], dataset['test']])
if cfg.run.num_classes == 222:
test_minor_1_dataset = dataset['test'].filter(lambda example: example["degree"]==1)
test_minor_23_dataset = dataset['test'].filter(lambda example: example["degree"]>=2)
dataset['train'] = concatenate_datasets([test_minor_1_dataset, test_minor_23_dataset])
dataset['valid'] = dataset['train']
print('~~~~final dataset~~~~')
print(dataset)
### marking labels:
dataset = dataset.map(lambda x: grade_preproc(x['degree'])) #Free Text Grade #degree
if cfg.run.num_classes == 2:
# grade 0 or grade 1,2,3
dataset = dataset.map(lambda x: binary_preproc(x['labels']))
elif cfg.run.num_classes == 22:
# grade 0&1 or grade 2&3
dataset = dataset.map(lambda x: bibi_preproc(x['labels']))
elif cfg.run.num_classes == 222:
# grade 1 or grade 2&3
datase = dataset.map(lambda x: bibi_preproc(x['labels']))
elif cfg.run.num_classes == 3:
# grade 0, 1, 2&3
dataset = dataset.map(lambda x: trinary_preproc(x['labels']))
elif cfg.run.num_classes == 6:
#joint training
dataset = dataset.map(lambda x: grade6_preproc(x['labels']))
###text pre_select part:
dataset = dataset.map(lambda x: project_text(x))
if cfg.run.exam != 'None':
dataset = dataset.map(lambda x: text_exam(x))
if cfg.run.ros != 'None':
dataset = dataset.map(lambda x: text_ros(x))
if cfg.run.rot != 'None':
dataset = dataset.map(lambda x: text_rot(x))
if cfg.run.ih != 'None':
dataset = dataset.map(lambda x: text_ih(x))
if cfg.run.ap != 'None':
dataset = dataset.map(lambda x: text_ap(x))
if cfg.run.sec != 'None':
dataset = dataset.map(lambda x: text_sec(x))
if cfg.run.rot == 'None' and cfg.run.ros == 'None' and cfg.run.exam == 'None' and cfg.run.ap == 'None' and cfg.run.ih == 'None' and cfg.run.sec == 'None':
print('no changes addded to original text')
dataset = dataset.map(lambda x: text2sec(x))
dataset = dataset.map(lambda x: chunk_off1(x['text']))
# dataset = dataset.map(lambda x: mask_struc(x))
# dataset = dataset.map(lambda x: mask_struc1(x))
# dataset = dataset.map(lambda x: mask_struc2(x))
else:
print('>======= going into section selected text here ========<')
dataset = dataset.map(lambda x: text2sec(x))
if cfg.run.chunk != 'None':
dataset = dataset.map(lambda x: chunk_off1(x['text']))
print('======chunking the text========')
print([i for i in dataset['valid']['text'][:3]])
print('*********testing ouput done********')
print('*********testing ouput done********')
dataset = dataset.map(lambda x: text_full_text(x))
if cfg.run.struc != 'None':
dataset = dataset.map(lambda x: struc_text_preproc1(x))
### clean and tokenizations:
if cfg.run.clean == 'clean':
print('### loading and cleaning text ###')
dataset = dataset.map(lambda x: text_preproc(x['Full Text']))
print('!!! Done loading and cleaning text !!!')
dataset = dataset.map(lambda x: tokenizer(x['clean_text'], padding='max_length', truncation=True, max_length=MAX_LEN), batched=True)
else:
dataset = dataset.map(lambda x: tokenizer(x['Full Text'], padding='max_length', truncation=True, max_length=MAX_LEN), batched=True)
### label class count
label_list = dataset['train'].unique('labels')
num_labels = len(label_list)
print(f'Total label numbers are {num_labels}, and the label list is {label_list}')
seed_list = [148, 81, 284, 42, 174, 407]
SEED= random.choice(seed_list)
print(SEED)
# config = cfg.params
T_args= TrainingArguments(
report_to = 'wandb',
output_dir = cfg.params.output_dir,
num_train_epochs = cfg.params.epochs,
overwrite_output_dir = True,
evaluation_strategy = 'steps',
learning_rate = cfg.params.learning_rate,
max_steps = cfg.params.max_steps, # will overwrite num_train_epochs
warmup_steps= cfg.params.warmup_steps,
weight_decay= cfg.params.weight_decay,
logging_steps = cfg.params.logging_steps,
eval_steps = cfg.params.eval_steps,
save_steps = int(cfg.params.logging_steps)*4,
load_best_model_at_end = cfg.params.load_best_model_at_end,
per_device_train_batch_size=cfg.params.batch_size,
gradient_accumulation_steps=cfg.params.gradient_accumulation_steps,
per_device_eval_batch_size=cfg.params.per_device_eval_batch_size,
tf32=bool(cfg.params.tf32),
seed = SEED,
save_total_limit = cfg.params.save_total_limit, # Only last x models are saved. Older ones are deleted.
metric_for_best_model = cfg.params.metric_for_best_model
)
print(f'The back bone model name is {MODEL}')
model = AutoModelForSequenceClassification.from_pretrained(
pretrained_model_name_or_path=MODEL,
num_labels=num_labels,
attention_probs_dropout_prob=cfg.params.dropout,
hidden_dropout_prob=cfg.params.dropout
)
# model.resize_token_embeddings(len(tokenizer))
if cfg.run.num_classes == 3:
trainer = CELTrainer(
args=T_args,
tokenizer=tokenizer,
train_dataset=dataset['train'],
eval_dataset=dataset['valid'],
model=model,
compute_metrics=compute_metrics3,
callbacks = [EarlyStoppingCallback(early_stopping_patience=3)]
)
elif cfg.run.num_classes == 2:
trainer = CELTrainer(
args=T_args,
# tokenizer=tokenizer,
train_dataset=dataset['train'],
eval_dataset=dataset['valid'],
model=model,
compute_metrics=compute_metrics2,
callbacks = [EarlyStoppingCallback(early_stopping_patience=3)]
)
elif cfg.run.num_classes == 22:
trainer = CELTrainer(
args=T_args,
tokenizer=tokenizer,
train_dataset=dataset['train'],
eval_dataset=dataset['valid'],
model=model,
compute_metrics=compute_metrics22,
callbacks = [EarlyStoppingCallback(early_stopping_patience=3)]
)
elif cfg.run.num_classes == 222:
trainer = CELTrainer(
args=T_args,
tokenizer=tokenizer,
train_dataset=dataset['train'],
eval_dataset=dataset['valid'],
model=model,
compute_metrics=compute_metrics222,
callbacks = [EarlyStoppingCallback(early_stopping_patience=3)]
)
elif cfg.run.num_classes == 6:
trainer = FLTrainer(
args=T_args,
tokenizer=tokenizer,
train_dataset=dataset['train'],
eval_dataset=dataset['valid'],
model=model,
compute_metrics=compute_metrics6,
callbacks = [EarlyStoppingCallback(early_stopping_patience=3)]
)
# use raytune to params search
if cfg.run.raytune != 'None':
tune_config = {
"per_device_train_batch_size": 16,
"per_device_eval_batch_size": 128,
# "num_train_epochs": tune.choice([2, 3, 4, 5]),
# "max_steps": 1 if smoke_test else -1, # Used for smoke test.
}
scheduler = PopulationBasedTraining(
time_attr="training_iteration",
metric="eval_accuracy",
mode="max",
# perturbation_interval=1,
hyperparam_mutations={
"weight_decay": tune.uniform(0.0, 0.3),
"learning_rate": tune.uniform(1e-5, 5e-5),
"per_device_train_batch_size": [8, 16, 18, 20, 32],
},
)
reporter = CLIReporter(
parameter_columns={
"weight_decay": "w_decay",
"learning_rate": "lr",
"per_device_train_batch_size": "train_bs/gpu",
"num_train_epochs": "num_epochs",
},
metric_columns=["eval_acc", "eval_accuracy", "epoch", "training_iteration"],
)
if cfg.run.num_classes == 22:
def model_init():
return AutoModelForSequenceClassification.from_pretrained(MODEL, num_labels=2)
trainer = CELTrainer(
args=T_args,
tokenizer=tokenizer,
train_dataset=dataset['train'],
eval_dataset=dataset['valid'],
# eval_dataset=dataset['test'],
model_init=model_init,
compute_metrics=compute_metrics22,
)
elif cfg.run.num_classes == 3:
def model_init():
return AutoModelForSequenceClassification.from_pretrained(MODEL, num_labels=cfg.run.num_classes)
trainer = CELTrainer(
args=T_args,
tokenizer=tokenizer,
train_dataset=dataset['train'],
eval_dataset=dataset['valid'],
# eval_dataset=dataset['test'],
model_init=model_init,
compute_metrics=compute_metrics3,
)
trainer.hyperparameter_search(
hp_space=lambda _: tune_config,
backend="ray",
n_trials=10,
# resources_per_trial={"cpu": 1, "gpu": gpus_per_trial},
scheduler=scheduler,
keep_checkpoints_num=1,
checkpoint_score_attr="training_iteration",
# stop={"training_iteration": 1} if smoke_test else None,
progress_reporter=reporter,
# local_dir="~/ray_results/",
# name="tune_transformer_pbt",
log_to_file=True,
)
else:
# trainer.train(resume_from_checkpoint = True)
trainer.train()
print(trainer.predict(dataset['test']).metrics)
with open(cfg.paths.print_out, 'a') as f:
print('Runname:', wandb.run.name, trainer.predict(dataset['test']).metrics, file=f)
if __name__ == "__main__":
main()