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

Refactoring BinaryClassificationEvaluator.py #2658

Open
Adversarian opened this issue May 18, 2024 · 0 comments
Open

Refactoring BinaryClassificationEvaluator.py #2658

Adversarian opened this issue May 18, 2024 · 0 comments

Comments

@Adversarian
Copy link

Adversarian commented May 18, 2024

Hi (again!),

I'm not sure if this is standard procedure for suggesting a refactor so if this "issue" is not conforming to your guidelines please feel free to immediately close it.

I've recently been tasked with implementing a semantic search functionality on a website and I've decided to use sentence-transformers and train models from scratch in my desired language. I've used OnlineContrastiveLoss as my training objective and the respective evaluation module BinaryClassificationEvaluator to evaluate my models.

While reading the source code for this file out of curiosity a couple of refactoring points come to mind which I would like to bring up here. I'd be happy to submit a PR for these but I just wanted to run these by you first in an issue before getting started on a PR.

  1. This assertion at the beginning:

Instead of

for label in labels:
            assert label == 0 or label == 1

we could more succinctly have

assert all([label == 0 or label==1 for label in labels]) 
  1. from_input_examples:

We could instead simply do:

sentences1, sentences2, scores = zip(
    *[(example.texts[0], example.texts[1], example.label) for example in examples]
)
  1. This line from compute_metrics:

Instead of

dot_scores = [np.dot(embeddings1_np[i], embeddings2_np[i]) for i in range(len(embeddings1_np))]

which can be potentially slow on large batches of data due to the list comprehension, we can just vectorize the operation and do

dot_scores = (embedding1_np*embeddings2_np).sum(axis=1).tolist()

which seems to be more efficient, at least at first glance (I haven't performed any benchmarks to know for sure).

Thanks again for your tremendous work! If these look like feasible changes, let me know and I can submit a PR myself. I'd imagine other evaluators could use similar refactors in that case as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant