Skip to content

Commit

Permalink
fix: ensure new triplets are unique (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
bclavie authored Jan 28, 2024
1 parent 5d34ba3 commit 67c2d15
Showing 1 changed file with 16 additions and 7 deletions.
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

0 comments on commit 67c2d15

Please sign in to comment.