Skip to content

Commit

Permalink
Fix error when encoding empty set of strings with convert_to_tensor=T…
Browse files Browse the repository at this point in the history
…rue (#1775)

* Fix error when encoding empty set of strings with convert_to_tensor=True

* Place common behaviour in if-branch

---------

Co-authored-by: Tom Aarsen <[email protected]>
  • Loading branch information
oToToT and tomaarsen authored Dec 18, 2023
1 parent 2a639f0 commit 1c396f8
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sentence_transformers/SentenceTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ def encode(self, sentences: Union[str, List[str]],
all_embeddings = [all_embeddings[idx] for idx in np.argsort(length_sorted_idx)]

if convert_to_tensor:
all_embeddings = torch.stack(all_embeddings)
if len(all_embeddings):
all_embeddings = torch.stack(all_embeddings)
else:
all_embeddings = torch.Tensor()
elif convert_to_numpy:
all_embeddings = np.asarray([emb.numpy() for emb in all_embeddings])

Expand Down

0 comments on commit 1c396f8

Please sign in to comment.