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

Why is there a small difference between the vector of a single sentence and the vector of a batch of sentences? #2451

Closed
wencan opened this issue Jan 27, 2024 · 2 comments

Comments

@wencan
Copy link

wencan commented Jan 27, 2024

model = SentenceTransformer('LaBSE')
zh_vec = model.encode('可以给你认识的人打个电话。')
vecs = model.encode(['contacts who may know about a job?', '可以给你认识的人打个电话。'])

print(zh_vec.mean(), vecs1[1].mean())

got:
-0.011027637 -0.011027641

@ir2718
Copy link
Contributor

ir2718 commented Jan 27, 2024

@wencan

This is a known issue stemming from the underlying libraries (probably even torch)
#2312
huggingface/transformers#2401

Although, on second thought, when you instantiate a model this way, you're using it in training mode. It might be because of the way dropout behaves in training mode. Try adding model.eval() before calculating the embeddings.

@tomaarsen
Copy link
Collaborator

Hello!

@ir2718 is right, those other issues contain a bit more information. In short: there are slight differences, but significantly small that the embeddings are not notably affected.

As for the second possible explanation: model.encode moves the model to eval() mode automatically, so that shouldn't be the cause.

  • Tom Aarsen

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

3 participants