Live data from Hacker News

How to Implement a Cosine Similarity Function in TypeScript

alexop.dev

31–40 of 43 posts

Re: How to Implement a Cosine Similarity Function in TypeScript

#31
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.

It's nice for performance, but if that's not a bottleneck, Typescript is pretty convenient. It has better tooling for the web, and compiles almost instantly. Rust -> WASM can be frustratingly slow if you're exploring a new idea or prototyping.

Re: How to Implement a Cosine Similarity Function in TypeScript

#32
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.

It's stil a vector though.

Re: How to Implement a Cosine Similarity Function in TypeScript

#33
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.

I think what you meant to say is that they aren’t strictly geometric or “interpretable”. But they absolutely are vectors in the linear algebraic sense.

Re: How to Implement a Cosine Similarity Function in TypeScript

#34
post #13

It’s a nice post, but “using array methods” probably shouldn’t be placed in the “Efficient Implementation” section. As often happens with high-level languages, a single plain old loop is faster than three array methods. Similarly, if you plan to query those vectors in search, you should consider continuous `TypedArray` types and smaller scalars than the double precision `number`. I know very little about JS, but some…

ooh, fantastic.. thankyou ! Im doing a bunch of 3D rotation transforms on millions of points in javascript .. this looks like what I need.

I'd probably use gpu.js for something like this.

Re: How to Implement a Cosine Similarity Function in TypeScript

#35

Meanwhile in Python this is just (a @ b.T)/(np.linalg.norm(a)*np.linalg.norm(b))

Because you use numpy. Could as well import cosine_similarity from sklearn.

you could also normalize (divide all components by magnitude) both vectors and simply take the dot product?

Re: How to Implement a Cosine Similarity Function in TypeScript

#36
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 is used as a distance function, so the important thing it is not that it is an angle, but that it is a similarity function.

And I think today it is preferred to call it "similarity" instead of "distance" because it is not a true distance function (15 years ago we called it "cosine distance").

Re: How to Implement a Cosine Similarity Function in TypeScript

#37
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…

No mystery there IMO. It works well because linear projections are the basic building block of modern neural networks.

Re: How to Implement a Cosine Similarity Function in TypeScript

#39
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

huh? This is just a funny joke on the way its named, what made you think I don't know about dot products ?

Re: How to Implement a Cosine Similarity Function in TypeScript

#40
post #39

Earlier quoted context omitted.

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

huh? This is just a funny joke on the way its named, what made you think I don't know about dot products ?

It could be a joke, but the way you wrote it makes it sound like you didn't know it

> in ML people renamed 'the angle between vectors' into something as fancy-feeling as 'cosine similarity'.

Since cosine similarity is a name derived from linear algebra

Post reply on HN