Skip to content

Commit

Permalink
fix cosine similarity example
Browse files Browse the repository at this point in the history
  • Loading branch information
briangu committed Dec 19, 2023
1 parent 3f0eae9 commit 83a80d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions examples/ml/cosine_similarity.kg
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ dot::{+/x*y}
mag::{(+/x*x)^0.5}

:" cosine similarity of x and y "
cossim::{dot(x;y)/mag(x)*mag(y)}
cossim::{dot(x;y)%mag(x)*mag(y)}

:" cosine distance "
cd::{1-cossim(x;y)}

a::[0.2242275924404239 0.882144226374053 0.9906177571863527 0.4108451091093793 0.9804472187642895 0.4060536401874264 0.3002237000518012 0.8514871407116807 0.6532517687913413 0.8627410006208077]
b::[0.30545495121740496 0.3771115787379501 0.7078223604498113 0.019355013585787284 0.16809292508968254 0.20411410383993234 0.09404528697075443 0.8849676641314673 0.9028064826465457 0.8102216701662028]

.d("test: ");.p(cd(a;b))

a::{.rn()}'!1536
b::{.rn()}'!1536

r::10000
t::time({{cd(a;b)}'!r})
.d("time (s) per pass: ");.p(t%r)
.d("iter per second: ");.p(1%(t%r))

14 changes: 14 additions & 0 deletions examples/ml/cosine_similarity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import math

def cosine_distance(a, b):
dot_product = sum(ai * bi for ai, bi in zip(a, b))
magnitude_a = math.sqrt(sum(ai * ai for ai in a))
magnitude_b = math.sqrt(sum(bi * bi for bi in b))
cosine_similarity = dot_product / (magnitude_a * magnitude_b)
print(dot_product, magnitude_a, magnitude_b, cosine_similarity)
return 1 - cosine_similarity

a = [0.2242275924404239, 0.882144226374053, 0.9906177571863527, 0.4108451091093793, 0.9804472187642895, 0.4060536401874264, 0.3002237000518012, 0.8514871407116807, 0.6532517687913413, 0.8627410006208077]
b = [0.30545495121740496, 0.3771115787379501, 0.7078223604498113, 0.019355013585787284, 0.16809292508968254, 0.20411410383993234, 0.09404528697075443, 0.8849676641314673, 0.9028064826465457, 0.8102216701662028]

print(cosine_distance(a,b))

0 comments on commit 83a80d5

Please sign in to comment.