davidberenstein1957 / fast-sentence-transformers

Simply, faster, sentence-transformers
MIT License
140 stars 10 forks source link

Fixed dimension of normalized embeddings #4

Closed arrmansa closed 2 years ago

arrmansa commented 2 years ago

Pull Request Overview

Fixes the issue described here, where using normalize_embeddings=True returned an incorrect result.

Brief

embeddings = np.linalg.norm(embeddings, ord=2, axis=1, keepdims=False)

was changed to

norms = np.linalg.norm(embeddings, ord=2, axis=1, keepdims=True)
embeddings = embeddings/np.where(norms<1e-12, 1e-12, norms)

so that the output is same as the original SentenceTransformer.py.

Minimum reproducible example

from fast_sentence_transformers import FastSentenceTransformer as SentenceTransformer
encoder = SentenceTransformer("all-MiniLM-L6-v2", device="cpu", quantize=False)
print(encoder.encode("Hello hello, hey, hello hello", normalize_embeddings=True).shape)

output before this PR

()

output after this PR

(384,)