Live data from Hacker News

How to Implement a Cosine Similarity Function in TypeScript

alexop.dev

21–30 of 43 posts

Re: How to Implement a Cosine Similarity Function in TypeScript

#21
post #15

As a physicist, I always found it funny that in ML people renamed 'the angle between vectors' into something as fancy-feeling as 'cosine similarity'.

I have a feeling it might be because the dot product of two unit length vectors is equal to the cosine of the angle between them.. but that's just a wild guess

Re: How to Implement a Cosine Similarity Function in TypeScript

#22
post #18

Earlier quoted context omitted.

Because op knows TS and doesn't know Rust?

What I'm asking really is what is the benefit of TS if we could use decent and improving coding AIs to help us code in Rust, even if we are new to it, or never used it, and compile to WASM. Much faster execution per my experience. I mean not even comparable.

How sad is it to want a solution but none of the journey to get there.

The future is bleak.

Re: How to Implement a Cosine Similarity Function in TypeScript

#23
post #12

Great post, but what struck me (again, like every time I look at cos similiarity) is how unreasonably well it works. It's just one of those things that's so "weird" about our world: why would cosine similarity work in n-dimensional semantic spaces? It's so stupid simple, and it intuitively makes sense, and it works really well. Crazy cool. I'm reminded of that old Eugene Wigner quote: "The most incomprehensible thing…

That cosine distance works at all as a concept isn't terribly shocking, especially given our habit of norming everything. Cosine similarity in a unit vector space is monotonic with euclidean distance, and we're using this stuff in "select the K most relevant vectors" sorts of queries, so cosine similarity behaves identically to euclidean distance. Tack on the fact that every finite set of vectors, with your favorite metric, can be embedded in euclidean space with at most ~41% relative error (and errors that high require somewhat special circumstances, so you'd expect most real-world data to have lower errors -- plus, the error doesn't apply to every pair of points, and many will definitely have much lower error), and you're able to use normed cosine similarity somewhat reasonably on every finite set of stuff you care about, so long as you choose an appropriate embedding. All sets of things you care about in ML are finite, and the sub-metric induced by whichever infinite set you're considering works just fine for everything we've discussed, so cosine similarity is reasonable for all practical ML purposes.

It's much more interesting that almost any set of ML-adjacent vectors can be somewhat reasonably compared via cosine distance _even without_ explicitly constructing an optimal embedding. It's not at all intuitive to me that an autoencoder's interior layer should behave well with respect to cosine similarity and not have any knots or anything warping that (associated) metric's usefulness.

Re: How to Implement a Cosine Similarity Function in TypeScript

#24
post #15

As a physicist, I always found it funny that in ML people renamed 'the angle between vectors' into something as fancy-feeling as 'cosine similarity'.

As an engineer I find it funny when physicists (and general ML bros) don't know about linear algebra concepts

Re: How to Implement a Cosine Similarity Function in TypeScript

#26
post #15

As a physicist, I always found it funny that in ML people renamed 'the angle between vectors' into something as fancy-feeling as 'cosine similarity'.

It sounds pretentious, but I think it comes from having multiple ways to compare things for similarity. Using the word similarity says what you are using the angle for. Angle similarity might work just as well until someone comes up with another angle based calculation. Cosine Similarity becomes a specific meaning indicating how it's caclulated and why it's calculated.

If you really wanted to be pretentious you could invent a Dimension Insensitive Euclidean Metric and call it DIEM to make it sound like you are putting some Latin into your papers.

Re: How to Implement a Cosine Similarity Function in TypeScript

#28
post #23
post #12

Great post, but what struck me (again, like every time I look at cos similiarity) is how unreasonably well it works. It's just one of those things that's so "weird" about our world: why would cosine similarity work in n-dimensional semantic spaces? It's so stupid simple, and it intuitively makes sense, and it works really well. Crazy cool. I'm reminded of that old Eugene Wigner quote: "The most incomprehensible thing…

That cosine distance works at all as a concept isn't terribly shocking, especially given our habit of norming everything. Cosine similarity in a unit vector space is monotonic with euclidean distance, and we're using this stuff in "select the K most relevant vectors" sorts of queries, so cosine similarity behaves identically to euclidean distance. Tack on the fact that every finite set of vectors, with your favorite…

> behaves identically to euclidean distance

Tbh, I would argue that's also pretty surprising, as Euclidean distance is notoriously unintuitive[1] (and noisy) in higher dimensions. (I guess norming does help, so that's likely a good point.)

[1] https://bib.dbvis.de/uploadedFiles/155.pdf

Re: How to Implement a Cosine Similarity Function in TypeScript

#30
post #15

As a physicist, I always found it funny that in ML people renamed 'the angle between vectors' into something as fancy-feeling as 'cosine similarity'.

I think the point is they're often doing it on things that aren't strictly vectors by definition, as in the physics sense, rather just some sack of stuff organised like they might be a vector and assuming its valid to compare angles because its just something numerically without much bias that does the job.
Post reply on HN