forked from ulzee/dkbehrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_bert.py
263 lines (237 loc) · 10 KB
/
train_bert.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
# %%
import argparse
import os, sys
#%%
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, required=True)
parser.add_argument('--mode', type=str, default='base')
parser.add_argument('--use_embedding', type=str, default=None)
parser.add_argument('--layers', type=int, default=1)
parser.add_argument('--heads', type=int, default=1)
parser.add_argument('--batch_size', type=int, default=64)
parser.add_argument('--embdim', type=int, default=192)
parser.add_argument('--train_subsample', default=None, type=int)
parser.add_argument('--val_subsample', type=int, default=10)
parser.add_argument('--eval_batch_size', type=int, default=32)
parser.add_argument('--eval_steps', type=int, default=2000)
parser.add_argument('--lr', type=float, default=1e-5)
parser.add_argument('--epochs', type=int, default=100)
parser.add_argument('--max_steps', type=int, default=None)
parser.add_argument('--gpus', type=str, default='0')
parser.add_argument('--nowandb', action='store_true', default=False)
parser.add_argument('--mask_ratio', type=float, default=0.5)
parser.add_argument('--code_resolution', type=int, default=5)
parser.add_argument('--disable_visit_shuffle', action='store_true', default=False)
parser.add_argument('--disable_visible_devices', action='store_true', default=False)
parser.add_argument('--covariates', default='gender,age', type=str)
parser.add_argument('--emb_proj', default='linear', type=str)
args = parser.parse_args()
#%%
if not args.nowandb:
wandb_subtag = '' if args.train_subsample is None else '-sub'
os.environ["WANDB_PROJECT"] = f'icdbert-{args.dataset}{wandb_subtag}'
os.environ["WANDB_LOG_MODEL"] = "end"
import wandb
else:
os.environ['WANDB_DISABLED'] = 'true'
if not args.disable_visible_devices:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpus
# else:
# torch.cuda_set_device(int(os.environ['CUDA_VISIBLE_DEVICES']))
import torch
import torch.nn as nn
import pickle as pk
import numpy as np
from transformers import BertConfig, BertForMaskedLM, TrainerCallback
from transformers.integrations import WandbCallback
from transformers import AutoTokenizer, TrainingArguments, Trainer, DataCollatorForLanguageModeling
import collator
from collator import CustomDataCollatorForLanguageModeling
from torch.utils.data import Dataset
import embedding
import pandas as pd
import attention
import utils
import random, string
run_tag = ''.join(random.choices(string.ascii_letters + string.digits, k=5))
#%%
with open(f'saved/{args.dataset}/diagnoses-cr{args.code_resolution}.pk', 'rb') as fl:
dxs = pk.load(fl)
#%%
covs = utils.load_covariates(f'saved/{args.dataset}/cov.csv', covlist=args.covariates.split(','))
#%%
tokenizer = AutoTokenizer.from_pretrained(f'./saved/{args.dataset}/tokenizers/bert-cr{args.code_resolution}')
tokenizer._pad = lambda *args, **kwargs: collator._pad(tokenizer, *args, **kwargs)
#%%
bert_emb_size = args.embdim
bertconfig = BertConfig(
vocab_size=len(tokenizer.vocab),
max_position_embeddings=tokenizer.model_max_length,
hidden_size=bert_emb_size,
num_hidden_layers=args.layers,
num_attention_heads=args.heads,
intermediate_size=1024,
)
model = BertForMaskedLM(bertconfig)
#%%
def load_embedding_file(fname):
with open(fname, 'rb') as fl:
edict = pk.load(fl)
edim = len(next(iter(edict.values())))
template = np.zeros((len(tokenizer.vocab), edim))
nmatched = 0
for w, i in tokenizer.vocab.items():
w = w.upper()
if w in edict:
template[i] = edict[w]
nmatched += 1
els = np.array(template).astype(np.float32)
els = np.array([v/np.sqrt(np.sum(v**2)) if np.sum(v != 0) != 0 else v for v in els])
print(f'Loaded embeddings for {nmatched}/{len(tokenizer.vocab)}')
return els
if args.mode == 'base':
model.bert.embeddings = embedding.CovariateAddEmbeddings(config=bertconfig)
elif args.mode in ['emb', 'attn']:
assert args.use_embedding is not None
els = None
if args.use_embedding == 'zeros':
els = np.zeros((len(tokenizer.vocab), 100))
print(f'Loading all-zero embeddings (for debugging)')
elif '.txt' not in args.use_embedding:
els = load_embedding_file(args.use_embedding)
emb_dict_list = [els]
elif '.txt' in args.use_embedding:
emb_dict_list = []
with open(args.use_embedding) as fl:
for efilename in fl:
efilename = efilename.strip()
emb_dict_list += [load_embedding_file(efilename)]
if args.mode == 'emb':
if els.shape[1] <= bertconfig.hidden_size:
els = np.concatenate([els, np.zeros((len(els), bertconfig.hidden_size - els.shape[1]))], axis=1)
else:
raise 'Not handled'
model.bert.embeddings = embedding.InjectEmbeddings(bertconfig, els, keep_training=False)
elif args.mode == 'attn':
# NOTE: some variables are wrapped in a non-module class to prevent issue
# with safetensors trying to save duplicate (shared) variables
model.bert.embeddings = embedding.KeepInputEmbeddings(config=bertconfig)
emb_holders = []
for ei, ith_embset in enumerate(emb_dict_list):
extra_embeddings = nn.Embedding(*ith_embset.shape)
extra_embeddings.weight = nn.Parameter(torch.from_numpy(ith_embset.astype(np.float32)).cuda(), requires_grad=False)
emb_holders += [
embedding.NonTorchVariableHolder(
extra_embeddings=extra_embeddings)
]
for layer in model.bert.encoder.layer:
layer.attention.self = attention.WeightedAttention(
config=bertconfig,
embeddings=emb_holders, # NOTE: now expects a list (should be list of one item for one emb set)
current_input=model.bert.embeddings.input_ids,
use_proj=args.emb_proj)
print('Attached weighted attention layers.')
# %%
phase_ids = { phase: np.genfromtxt(f'files/{args.dataset}/{phase}_ids.txt') for phase in ['train', 'val', 'test'] }
phase_ids['val'] = phase_ids['val'][::args.val_subsample]
if args.train_subsample is not None:
phase_ids['train'] = phase_ids['train'][:args.train_subsample]
datasets = { phase: utils.ICDDataset(
dxs,
tokenizer,
ids,
covs,
separator='[SEP]',
max_length=512,
shuffle_in_visit=False if args.disable_visit_shuffle else phase=='train',
) for phase, ids in phase_ids.items() }
token_frequency = dict()
for sample in datasets['val']:
for tkn in sample['input_ids']:
if tkn < 3: continue
c = token_frequency.get(tkn, 0)
token_frequency[tkn] = c + 1
least_frequent = dict()
tix, counts = list(zip(*token_frequency.items()))
prev_cval = 10
for cutoff in [50, 100, 200]:
cval = cutoff
least_frequent[f'least{cutoff}'] = [i for i, c in token_frequency.items() if c <= cval and c > prev_cval]
print(f'Tokens <{cutoff}:', len(least_frequent[f'least{cutoff}']))
prev_cval = cval
data_collator = CustomDataCollatorForLanguageModeling(
tokenizer=tokenizer, mlm=True, mlm_probability=args.mask_ratio,
)
subtag = '' if args.train_subsample is None else f'sub{args.train_subsample}-'
mdlname = f'bert-{args.mode}-{subtag}cr{args.code_resolution}-lr{args.lr}-e{args.embdim}-layers{args.layers}-h{args.heads}_{run_tag}'
print(mdlname)
training_args = TrainingArguments(
output_dir=f'runs/{args.dataset}/{mdlname}',
per_device_train_batch_size=args.batch_size,
per_device_eval_batch_size=args.eval_batch_size,
eval_accumulation_steps=15,
learning_rate=args.lr,
num_train_epochs=args.epochs,
max_steps=-1 if args.max_steps is None else args.max_steps,
report_to='wandb' if not args.nowandb else None,
evaluation_strategy='steps',
run_name=mdlname,
eval_steps=args.eval_steps,
save_steps=args.eval_steps,
)
def compute_metrics(eval_pred, mask_value=-100, topns=(1, 5, 10)):
logits, labels = eval_pred
bsize, seqlen = labels.shape
logits = torch.from_numpy(np.reshape(logits, (bsize*seqlen, -1)))
labels = torch.from_numpy(np.reshape(labels, (bsize*seqlen)))
where_prediction = labels != mask_value
topaccs = utils.topk_accuracy(logits[where_prediction], labels[where_prediction], topk=topns)
out = dict()
for n, acc in zip(topns, topaccs):
out[f'top{n:02d}'] = acc
if args.mode == 'emb':
inspect_idx = tokenizer.vocab['f32'] if 'f32' in tokenizer.vocab else tokenizer.vocab['f329']
cl = model.bert.embeddings.coef_learn[inspect_idx].item()
out['coef_learn'] = cl
logits = logits.cpu().numpy()
labels = labels.cpu().numpy()
for freqbin, tixs in least_frequent.items():
idict = { i: True for i in tixs }
where_bin = [i for i, l in enumerate(labels.astype(int).tolist()) if l in idict]
out[freqbin+'_count'] = len(where_bin)
out[freqbin] = utils.topk_accuracy(
torch.from_numpy(logits[where_bin]),
torch.from_numpy(labels[where_bin]),
topk=[5])[0]
return out
class CustomCallback(TrainerCallback):
def on_log(self, __args, state, control, logs=None, **kwargs):
# super().on_log(**kwargs)
if state.is_local_process_zero:
if args.mode == 'emb':
coefs = model.bert.embeddings.coef_learn.detach().cpu().numpy()
coefs = [[i] for i in coefs]
table = wandb.Table(data=coefs, columns=["coefs"])
wandb.log({
'histogram-coef_learn': wandb.plot.histogram(table, "coefs", title="Embedding mixing coefficient")
})
if state.global_step > 0:
ckpt_path = f'runs/{args.dataset}/{mdlname}/checkpoint-{state.global_step}'
if not os.path.exists(ckpt_path):
os.mkdir(ckpt_path)
torch.save(
model.state_dict(),
f'{ckpt_path}/weights.pth')
trainer = Trainer(
model=model,
data_collator=data_collator,
args=training_args,
train_dataset=datasets['train'],
eval_dataset=datasets['val'],
compute_metrics=compute_metrics,
callbacks=[CustomCallback()]
)
#%%
trainer.evaluate()
trainer.train()
# %%