Skip to content

Instantly share code, notes, and snippets.

@usametov
Last active July 8, 2023 03:19
Show Gist options
  • Save usametov/509f3466e141db6f125b815c5ae12c75 to your computer and use it in GitHub Desktop.
Save usametov/509f3466e141db6f125b815c5ae12c75 to your computer and use it in GitHub Desktop.
cosine-similarity
(:require [tech.v3.datatype.functional :as func])
(defn cosine-similarity
[v1 v2]
(/ (func/dot-product v1 v2)
(Math/sqrt (* (func/magnitude-squared v1) (func/magnitude-squared v2)))))
;; Maybe, we don't really need to divide by magnitudes product.
;; it looks like embeddings coming from openai are already normalized.
{:deps {
techascent/tech.ml.dataset {:mvn/version "7.000-beta-51"}
techascent/tech.datatype {:mvn/version "5.18"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment