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: ensure new triplets are unique #78

Merged
merged 2 commits into from
Jan 28, 2024
Merged
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
23 changes: 16 additions & 7 deletions ragatouille/data/training_data_processor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import random
from collections import defaultdict
from itertools import product
from pathlib import Path
from typing import Literal, Union

Expand Down Expand Up @@ -82,11 +83,19 @@ def _make_individual_triplets(self, query, positives, negatives):
triplets.append([q, p, n])

extra_triplets_needed = max_triplets_per_query - initial_triplets_count
while extra_triplets_needed > 0:
p = self.passage_map[random.choice(all_pos_texts)]
n = self.passage_map[random.choice(negatives)]
triplets.append([q, p, n])
extra_triplets_needed -= 1
if extra_triplets_needed > 0:
all_combinations = list(product(all_pos_texts, negatives))
random.seed(42)
random.shuffle(all_combinations)
for pos, neg in all_combinations:
p = self.passage_map[pos]
n = self.passage_map[neg]
if [q, p, n] not in triplets:
triplets.append([q, p, n])
extra_triplets_needed -= 1
if extra_triplets_needed <= 0:
break

else:
p = self.passage_map[positives[0]]
for n in negatives:
Expand Down Expand Up @@ -140,8 +149,8 @@ def _process_raw_pairs(self, raw_data, mine_hard_negatives, n_new_negatives):
)
training_triplets += self._make_individual_triplets(
query=query,
positives=passages["positives"],
negatives=passages["negatives"],
positives=list(set(passages["positives"])),
negatives=list(set(passages["negatives"])),
)
self.training_triplets = training_triplets

Expand Down
Loading