Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'text' KeyError in data collator #331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions pygaggle/run/finetune_monot5.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,28 @@
TrainerCallback,
)


class MonoT5Dataset(Dataset):
def __init__(self, data):
self.data = data
def __init__(self, data, tokenizer):
self.tokenizer = tokenizer
self.data = [self.tokenize(sample) for sample in data]

def tokenize(self, sample):
text = f'Query: {sample[0]} Document: {sample[1]} Relevant:'
tokenized_text = self.tokenizer(text, padding='max_length', truncation=True, max_length=512, return_tensors='pt')
tokenized_label = self.tokenizer(sample[2], padding='max_length', truncation=True, max_length=512, return_tensors='pt')['input_ids']
return {
'input_ids': tokenized_text['input_ids'].squeeze(0),
'attention_mask': tokenized_text['attention_mask'].squeeze(0),
'labels': tokenized_label.squeeze(0)
}

def __len__(self):
return len(self.data)

def __getitem__(self, idx):
sample = self.data[idx]
text = f'Query: {sample[0]} Document: {sample[1]} Relevant:'
return {
'text': text,
'labels': sample[2],
}
return self.data[idx]


def main():
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -73,16 +81,16 @@ def main():
train_samples.append((query, negative, 'false'))

def smart_batching_collate_text_only(batch):
texts = [example['text'] for example in batch]
tokenized = tokenizer(texts, padding=True, truncation='longest_first', return_tensors='pt', max_length=512)
tokenized['labels'] = tokenizer([example['labels'] for example in batch], return_tensors='pt')['input_ids']

for name in tokenized:
tokenized[name] = tokenized[name].to(device)

return tokenized
input_ids = torch.stack([item['input_ids'] for item in batch])
attention_mask = torch.stack([item['attention_mask'] for item in batch])
labels = torch.stack([item['labels'] for item in batch])
return {
'input_ids': input_ids.to(device),
'attention_mask': attention_mask.to(device),
'labels': labels.to(device)
}

dataset_train = MonoT5Dataset(train_samples)
dataset_train = MonoT5Dataset(train_samples, tokenizer)

if args.save_every_n_steps:
steps = args.save_every_n_steps
Expand Down