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

Add support for asymmetric models in NoDuplicatesDataLoader #2301

Open
wants to merge 2 commits 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
20 changes: 17 additions & 3 deletions sentence_transformers/datasets/NoDuplicatesDataLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def __iter__(self):

valid_example = True
for text in example.texts:
if text.strip().lower() in texts_in_batch:
if self._text_to_str(text).strip().lower() in texts_in_batch:
valid_example = False
break

if valid_example:
batch.append(example)
for text in example.texts:
texts_in_batch.add(text.strip().lower())
texts_in_batch.add(self._text_to_str(text).strip().lower())

self.data_pointer += 1
if self.data_pointer >= len(self.train_examples):
Expand All @@ -41,4 +41,18 @@ def __iter__(self):
yield self.collate_fn(batch) if self.collate_fn is not None else batch

def __len__(self):
return math.floor(len(self.train_examples) / self.batch_size)
return math.floor(len(self.train_examples) / self.batch_size)


def _text_to_str(self, text) -> str:
"""
In symmetric models, the text is a string, but in asymmetric models the text is a dictionary.
This method extracts a string in both cases.
"""

# there is only one key value. Example is : `{"query": "Some query"}`
if isinstance(text, dict):
return list(text.values())[0]
else:
return text