PIXELBANKv8.2.1
Menu

Embedding Cosine Similarity

Compute the cosine similarity between two embedding vectors.

Cosine similarity is defined as: cos_sim(a,b)=abab\text{cos\_sim}(a, b) = \frac{a \cdot b}{\|a\| \cdot \|b\|}

Input:

  • Line 1: space-separated floats (vector a)
  • Line 2: space-separated floats (vector b)

Output: Cosine similarity rounded to 4 decimal places.

Example:

Input:
1.0 0.0 0.0
0.0 1.0 0.0
Output:
0.0
Reasoning:
  • First, we define the two embedding vectors: a=(1.0,0.0,0.0)a = (1.0, 0.0, 0.0) and b=(0.0,1.0,0.0)b = (0.0, 1.0, 0.0)
  • Then, we calculate the dot product of aa and bb: ab=(1.0)(0.0)+(0.0)(1.0)+(0.0)(0.0)=0.0a \cdot b = (1.0)(0.0) + (0.0)(1.0) + (0.0)(0.0) = 0.0
  • Next, we calculate the magnitudes of aa and bb: a=(1.0)2+(0.0)2+(0.0)2=1.0\|a\| = \sqrt{(1.0)^2 + (0.0)^2 + (0.0)^2} = 1.0 and b=(0.0)2+(1.0)2+(0.0)2=1.0\|b\| = \sqrt{(0.0)^2 + (1.0)^2 + (0.0)^2} = 1.0
  • The final output is the cosine similarity: cos_sim(a,b)=abab=0.01.01.0=0.0\text{cos\_sim}(a, b) = \frac{a \cdot b}{\|a\| \cdot \|b\|} = \frac{0.0}{1.0 \cdot 1.0} = 0.0

Constraints:

  • Vectors have the same dimension (1 <= D <= 100)
  • Vectors are not zero vectors
  • Output rounded to 4 decimal places
Editor

Test Results

0/0
Run code to see test results.