Skip to content

Commit

Permalink
[binding] refine compute_cosine_score (#192)
Browse files Browse the repository at this point in the history
* [binding] refine compute_cosine_score

* [binding] fix lint
  • Loading branch information
cdliang11 authored Aug 15, 2023
1 parent 67ff51d commit 75f8bf9
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions runtime/binding/python/py/speaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,19 @@ def extract_embedding_kaldiio(self,
def compute_cosine_score(self, emb1, emb2):
""" Compute cosine score between emb1 and emb2.
Args:
emb1(numpy.ndarray): embedding of speaker-1
emb2(numpy.ndarray): embedding of speaker-2
emb1(numpy.ndarray): embedding of speaker-1 [B, emb]
emb2(numpy.ndarray): embedding of speaker-2 [B, emb]
Return:
score(float): cosine score
score(float): cosine score [B]
"""
assert isinstance(emb1, np.ndarray) and isinstance(
emb2, np.ndarray
), "NOTE: the type of emb1 and emb2 need be numpy.ndarray"
assert len(emb1.shape) == len(
emb2.shape
), "NOTE: the embedding size of emb1 and emb2 need to be equal"
return np.dot(emb1,
emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2))
xx = np.sum(emb1 ** 2, axis=1) ** 0.5
x = x / xx[:, np.newaxis]
yy = np.sum(emb2 ** 2, axis=1) ** 0.5
y = y / yy[:, np.newaxis]
return np.diagonal(np.dot(x, y.transpose()))

0 comments on commit 75f8bf9

Please sign in to comment.