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

Prevent sampling 2x more than requested when max_steps is set #556

Merged
merged 1 commit into from
Sep 18, 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
12 changes: 6 additions & 6 deletions src/setfit/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(
self.sentences = sentences
self.labels = labels
self.sentence_labels = list(zip(self.sentences, self.labels))
self.max_pairs = max_pairs
self.max_pos_or_neg = -1 if max_pairs == -1 else max_pairs // 2

if multilabel:
self.generate_multilabel_pairs()
Expand Down Expand Up @@ -90,8 +90,8 @@ def __init__(
def generate_pairs(self) -> None:
for (_text, _label), (text, label) in shuffle_combinations(self.sentence_labels):
is_positive = _label == label
is_positive_full = self.max_pairs != -1 and len(self.pos_pairs) >= self.max_pairs
is_negative_full = self.max_pairs != -1 and len(self.neg_pairs) >= self.max_pairs
is_positive_full = self.max_pos_or_neg != -1 and len(self.pos_pairs) >= self.max_pos_or_neg
is_negative_full = self.max_pos_or_neg != -1 and len(self.neg_pairs) >= self.max_pos_or_neg

if is_positive:
if not is_positive_full:
Expand All @@ -106,8 +106,8 @@ def generate_multilabel_pairs(self) -> None:
for (_text, _label), (text, label) in shuffle_combinations(self.sentence_labels):
# logical_and checks if labels are both set for each class
is_positive = any(np.logical_and(_label, label))
is_positive_full = self.max_pairs != -1 and len(self.pos_pairs) >= self.max_pairs
is_negative_full = self.max_pairs != -1 and len(self.neg_pairs) >= self.max_pairs
is_positive_full = self.max_pos_or_neg != -1 and len(self.pos_pairs) >= self.max_pos_or_neg
is_negative_full = self.max_pos_or_neg != -1 and len(self.neg_pairs) >= self.max_pos_or_neg

if is_positive:
if not is_positive_full:
Expand Down Expand Up @@ -180,5 +180,5 @@ def generate_pairs(self) -> None:
self.pos_pairs.append(
{"sentence_1": text_one, "sentence_2": text_two, "label": self.cos_sim_matrix[id_one][id_two]}
)
if self.max_pairs != -1 and len(self.pos_pairs) > self.max_pairs:
if self.max_pos_or_neg != -1 and len(self.pos_pairs) > self.max_pos_or_neg:
break
Loading