Answers.Body: <p>As the objective is to find the most similar to A and disimilar Vector to B approach 2 would be the most appropriate.</p>
  <p>Why not Approach 1:
  It can lead to confusing results. If you look at the example below multiple scenarios lead to same final value. This may lead to few problems :</p>
  <ol>
  <li>For same output how do you know which vector should be preferred</li>
  <li>Subtracting with each other may cancel out impact of dissimilarity on one with other</li>
  </ol>
  <p><a href="https://i.sstatic.net/pYmZf.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/pYmZf.png" alt="enter image description here" /></a></p>
  <p>Why not Approach 3:
  Vector Subtraction is not <em><strong>commutative</strong></em>. So you will get different results based on whether you use A first or B first. Moreover lets see an example where it fails completely :</p>
  <p>Suppose we have 2 dimension vector A and B, and we want to vector V which is</p>
  <p>A = 6,0
  B = 0.01,-0.01</p>
  <p>V = 3,-1</p>
  <p>Cos(A,V) = 0.94
  Cos(B,V) = 0.894
  Cos(A-B,V) = 0.94</p>
  <p>Second Secnario:
  A = 6,0
  B = -0.01,0.01</p>
  <p>V = 3,-1</p>
  <p>Cos(A,V) = 0.94
  Cos(B,V) = -0.894
  Cos(A-B) = 0.94</p>
  <p>Though cos A and cos B are two extremes in scenario 1 and scenario 2. Cos(A-B) gives the same result. Hence i will not advice to use Cos(A-B)</p>
  <p>Approach 2 will work expected in your case.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-06T16:40:11.320Z
Answers.LastActivityDate: 2022-01-06T16:40:11.320Z
Answers.UserDisplayName: Ashwiniku918
Answers.Body: <p>This is just a heuristic, but how about <code>f(V) = cosine_similarity(A,V) * min(1, 1 - cosine_similarity(B,V))</code>? I'm applying the <code>min</code> here so that the multiplier won't get larger than one. Up to you whether you want to include it or not.</p>
  <p>This could be tuned further as <code>f(V) = max(0, cosine_similarity(A,V))^a * min(1, 1 - cosine_similarity(B,V))^b</code>, but you'd need to experiment to see which values of <code>a</code> and <code>b</code> works the best for you.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-06T21:57:50.720Z
Answers.LastActivityDate: 2022-01-06T21:57:50.720Z
Answers.UserDisplayName: NikoNyrh
Body: <p>Which would be better approach for getting top k embedding vectors such that they are similar to embedding vector A and dissimilar to vector B.</p>
  <p><strong>Approach 1:</strong></p>
  <ul>
  <li>calculate <code>f(V) = cosine_similarity(A,V) - cosine_similarity(B,V)</code> for each vector V</li>
  <li>sort vectors by f(V) value in descending order</li>
  <li>take first k of them.</li>
  </ul>
  <p><strong>Approach 2:</strong></p>
  <ul>
  <li>calculate
  <code>f(V) = cosine_similarity(A,V)</code> , <code>g(V) = cosine_similarity(B,V)</code>
  for each vector V</li>
  <li>sort vectors by f(V) value in descending order</li>
  <li>take first k of them</li>
  <li>sort selected k vectors by g(V) in ascending order.</li>
  </ul>
  <p><strong>Approach 3:</strong></p>
  <ul>
  <li>calculate <code>f(V) = cosine_similarity((A - B),V)</code> for each vector V</li>
  <li>sort vectors by f(V) value in descending order</li>
  <li>take first k of them.</li>
  </ul>
  <p>Also, suggest better approach if you have other than above two.</p>
  <p>Note: embedding vector was calculated using word2vec algorithm</p>
  
Tags: <machine-learning><word-embedding><word2vec><cosine-similarity>
Title: Given embedding vector A and vector B, how to find top k embedding vectors such that they are similar to vector A and dissimilar to vector B
UserDisplayName: Shubham
ThreadText: # Given embedding vector A and vector B, how to find top k embedding vectors such that they are similar to vector A and dissimilar to vector B
  
  (Asked by: username_0 on 2022-01-06)
  
  Which would be better approach for getting top k embedding vectors such that they are similar to embedding vector A and dissimilar to vector B.
  
  **Approach 1:**
  
  * calculate `f(V) = cosine_similarity(A,V) - cosine_similarity(B,V)` for each vector V
  * sort vectors by f(V) value in descending order
  * take first k of them.
  
  **Approach 2:**
  
  * calculate
    `f(V) = cosine_similarity(A,V)` , `g(V) = cosine_similarity(B,V)`
    for each vector V
  * sort vectors by f(V) value in descending order
  * take first k of them
  * sort selected k vectors by g(V) in ascending order.
  
  **Approach 3:**
  
  * calculate `f(V) = cosine_similarity((A - B),V)` for each vector V
  * sort vectors by f(V) value in descending order
  * take first k of them.
  
  Also, suggest better approach if you have other than above two.
  
  Note: embedding vector was calculated using word2vec algorithm
  
  **Tags:** machine-learning, word-embedding, word2vec, cosine-similarity
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-01-06. Score: 0
  
  As the objective is to find the most similar to A and disimilar Vector to B approach 2 would be the most appropriate.
  
  Why not Approach 1:
  It can lead to confusing results. If you look at the example below multiple scenarios lead to same final value. This may lead to few problems :
  
  1. For same output how do you know which vector should be preferred
  2. Subtracting with each other may cancel out impact of dissimilarity on one with other
  
  [![enter image description here](https://i.sstatic.net/pYmZf.png)](https://i.sstatic.net/pYmZf.png)
  
  Why not Approach 3:
  Vector Subtraction is not ***commutative***. So you will get different results based on whether you use A first or B first. Moreover lets see an example where it fails completely :
  
  Suppose we have 2 dimension vector A and B, and we want to vector V which is
  
  A = 6,0
  B = 0.01,-0.01
  
  V = 3,-1
  
  Cos(A,V) = 0.94
  Cos(B,V) = 0.894
  Cos(A-B,V) = 0.94
  
  Second Secnario:
  A = 6,0
  B = -0.01,0.01
  
  V = 3,-1
  
  Cos(A,V) = 0.94
  Cos(B,V) = -0.894
  Cos(A-B) = 0.94
  
  Though cos A and cos B are two extremes in scenario 1 and scenario 2. Cos(A-B) gives the same result. Hence i will not advice to use Cos(A-B)
  
  Approach 2 will work expected in your case.
  
  
  ---
  
  ### Answer by username_2 on 2022-01-06. Score: 0
  
  This is just a heuristic, but how about `f(V) = cosine_similarity(A,V) * min(1, 1 - cosine_similarity(B,V))`? I'm applying the `min` here so that the multiplier won't get larger than one. Up to you whether you want to include it or not.
  
  This could be tuned further as `f(V) = max(0, cosine_similarity(A,V))^a * min(1, 1 - cosine_similarity(B,V))^b`, but you'd need to experiment to see which values of `a` and `b` works the best for you.
  
  
  ---
  
  