woohaaha

woohaaha

Is cosine distance wrong?

I’m using embeddings with pgvector but when using cosine distance the results weren’t matching expectations so I decided to make sure my test example would work with Scholar, but the results are off in Scholar as well which implies to me that maybe I have the wrong expectations, and yet… after looking it up, I don’t think I’m misunderstanding.

Most simple example:

Scholar.Metrics.Distance.cosine(Nx.tensor([1,1]), Nx.tensor([1,1]))

#Nx.Tensor<
  f32
  5.960464477539063e-8
>

If two tensors are “pointing” in the same direction the cosine distance should be 1, but this is pretty much 0 implying that the tensors are “orthogonal” (not similar at all).

What am I doing wrong?

Thank you

Update 1

Do I need to subtract from 1?

Nx.subtract(Nx.tensor(1), Scholar.Metrics.Distance.cosine(Nx.tensor([1,2]), Nx.tensor([1,2])))

#Nx.Tensor<
  f32
  0.9999999403953552
>

Most Liked

polvalente

polvalente

Nx Core Team

Scholar uses the cosine distance. You want the cosine similarity, which is 1 - distance

Last Post!

woohaaha

woohaaha

Thank you

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement