import numpy as np
def cosine_similarity(v, A):
return np.argmax(np.dot(v, A.T) / (np.linalg.norm(v, ord=2) * np.linalg.norm(A, axis=1, ord=2))
A = np.stack([member.vector for member in member_bio])
v = cur_search.vector
closest_idx = cosine_similarity(v, A)
You can use linear algebra to compute this similarity in a broadcasted manner:
import numpy as np
def cosine_similarity(v, A):
return np.argmax(np.dot(v, A.T) / (np.linalg.norm(v, ord=2) * np.linalg.norm(A, axis=1, ord=2))
A = np.stack([member.vector for member in member_bio])
v = cur_search.vector
closest_idx = cosine_similarity(v, A)
发布评论
评论(1)
您可以使用线性代数以广播方式计算这种相似性:
You can use linear algebra to compute this similarity in a broadcasted manner: