MCPcopy Create free account
hub / github.com/MichSchli/AVeriTeC / DualEncoderDataLoader

Class DualEncoderDataLoader

data_loaders/DualEncoderDataLoader.py:11–165  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9import random
10
11class DualEncoderDataLoader(pl.LightningDataModule):
12 def __init__(self, tokenizer, data_file, batch_size):
13 super().__init__()
14 self.tokenizer = tokenizer
15 self.data_file = data_file
16 self.batch_size = batch_size
17
18 def encode_sentences(self, positives, negatives, max_length=512, pad_to_max_length=False, return_tensors="pt"):
19 encoded_dict = self.tokenizer(
20 positives,
21 max_length=max_length,
22 padding="max_length" if pad_to_max_length else "longest",
23 truncation=True,
24 return_tensors=return_tensors
25 )
26
27 pos_input_ids = encoded_dict['input_ids']
28 pos_attention_masks = encoded_dict['attention_mask']
29
30 negatives_unstacked = list(np.reshape(negatives, -1))
31
32 encoded_dict = self.tokenizer(
33 negatives_unstacked,
34 max_length=max_length,
35 padding="max_length" if pad_to_max_length else "longest",
36 truncation=True,
37 return_tensors=return_tensors
38 )
39
40 neg_input_ids = encoded_dict['input_ids'].view(len(negatives), len(negatives[0]), -1)
41 neg_attention_masks = encoded_dict['attention_mask'].view(len(negatives), len(negatives[0]), -1)
42
43 batch = {
44 "pos_input_ids": pos_input_ids,
45 "pos_attention_masks": pos_attention_masks,
46 "neg_input_ids": neg_input_ids,
47 "neg_attention_masks": neg_attention_masks,
48 }
49
50 return batch
51
52 def triple_to_string(self, x):
53 return " </s> ".join([item.strip() for item in x])
54
55 def load_tsv_files(self, filepaths):
56 srcs = []
57
58 for filepath in filepaths:
59 with open(filepath) as tsv:
60 for line in tsv:
61 parts = line.strip().split("\t")
62
63 if len(parts) == 2:
64 claim = parts[0].split("||")[0].replace("[CLAIM]", "").strip()
65 question = parts[0].split("||")[1].replace("[QUESTION]", "").strip()
66 answer = parts[1].split("||")[1]
67
68 srcs.append([claim, question, answer])

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected