text stringlengths 1 93.6k |
|---|
combined_outputs = process_encoder_outputs(output, bert_mask)
|
processed_examples.extend(combined_outputs)
|
#package preprocessed data together + return
|
data = list(zip(processed_examples, output_masks, instances, label_indexes))
|
return data
|
def _train(train_data, probe, optim, criterion, bsz=1, silent=False):
|
if not silent: train_data = tqdm(train_data)
|
for input_ids, output_mask, _, label in train_data:
|
input_ids = input_ids.cuda()
|
output_mask = output_mask.cuda()
|
label = label.cuda()
|
optim.zero_grad()
|
output = probe(input_ids)
|
#mask to candidate senses for target word
|
output = torch.mul(output, output_mask)
|
#set masked out items to -inf to get proper probabilities over the candidate senses
|
output[output == 0] = float('-inf')
|
output = F.softmax(output, dim=-1)
|
loss = criterion(output, label)
|
batch_sz = loss.size(0)
|
loss = loss.sum()/batch_sz
|
loss.backward()
|
optim.step()
|
return probe, optim
|
def _eval(eval_data, probe, label_space):
|
eval_preds = []
|
for input_ids, output_mask, inst, _ in eval_data:
|
input_ids = input_ids.cuda()
|
output_mask = output_mask.cuda()
|
#run example through model
|
with torch.no_grad():
|
output = probe(input_ids)
|
#mask to candidate senses for target word
|
output = torch.mul(output, output_mask)
|
#set masked out items to -inf to get proper probabilities over the candidate senses
|
output[output == 0] = float('-inf')
|
output = F.softmax(output, dim=-1)
|
#get predicted label
|
pred_id = output.topk(1, dim=-1)[1].squeeze().item()
|
pred_label = label_space[pred_id]
|
eval_preds.append((inst[0], pred_label))
|
return eval_preds
|
def _eval_with_backoff(eval_data, probe, label_space, wn_senses, coverage, keys):
|
eval_preds = []
|
for key, (input_ids, output_mask, inst, _) in zip(keys, eval_data):
|
input_ids = input_ids.cuda()
|
output_mask = output_mask.cuda()
|
if key in coverage:
|
#run example through model
|
with torch.no_grad():
|
output = probe(input_ids)
|
output = torch.mul(output, output_mask)
|
#set masked out items to -inf to get proper probabilities over the candidate senses
|
output[output == 0] = float('-inf')
|
output = F.softmax(output, dim=-1)
|
#get predicted label
|
pred_id = output.topk(1, dim=-1)[1].squeeze().item()
|
pred_label = label_space[pred_id]
|
eval_preds.append((inst[0], pred_label))
|
#backoff to wsd for lemma+pos
|
else:
|
#this is ws1 for given key
|
pred_label = wn_senses[key][0]
|
eval_preds.append((inst[0], pred_label))
|
return eval_preds
|
def train_probe(args):
|
lr = args.lr
|
bsz = args.bsz
|
#create passed in ckpt dir if doesn't exist
|
if not os.path.exists(args.ckpt): os.mkdir(args.ckpt)
|
'''
|
LOAD PRETRAINED BERT MODEL
|
'''
|
#model loading code based on pytorch_transformers README example
|
tokenizer = load_tokenizer(args.encoder_name)
|
pretrained_model, output_dim = load_pretrained_model(args.encoder_name)
|
pretrained_model = pretrained_model.cuda()
|
'''
|
LOADING IN TRAINING AND EVAL DATA
|
'''
|
print('Loading data + preprocessing...')
|
sys.stdout.flush()
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.