NLP中的餘弦相似度 Cosine similarity 是什麼,如何計算(學習心得)
餘弦相似度 Cosine similarity
To measure how similar two words are, we need a way to measure the degree of similarity between two embedding vectors for the two words. Given two vectors 𝑢 and 𝑣, cosine similarity is defined as follows:
分子:u 和 v 的點積 dot product(或內積 inner product)
分母:向量 u 和 v 的範式 norm(或長度 length)相乘
得到的就是 u 和 v 向量夾角 θ 的餘弦值,即兩個向量的相似度
餘弦值的範圍是 -1~1,其中 1 對應 完全正相關,-1 對應完全負相關,0 對應完全不相關
其中,向量 norm 的計算公式為,向量中每個元素的平方和開根號