Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cosine distance #231

Merged
merged 4 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/service/src/prelude/global/f16_cos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl G for F16Cos {
type L2 = F16L2;

fn distance(lhs: &[F16], rhs: &[F16]) -> F32 {
super::f16::cosine(lhs, rhs) * (-1.0)
F32(1.0) - super::f16::cosine(lhs, rhs)
}

fn elkan_k_means_normalize(vector: &mut [F16]) {
Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/prelude/global/f32_cos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl G for F32Cos {
type L2 = F32L2;

fn distance(lhs: &[F32], rhs: &[F32]) -> F32 {
cosine(lhs, rhs) * (-1.0)
F32(1.0) - cosine(lhs, rhs)
}

fn elkan_k_means_normalize(vector: &mut [F32]) {
Expand Down
2 changes: 1 addition & 1 deletion docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ We support three operators to calculate the distance between two vectors.

- `<->`: squared Euclidean distance, defined as $\Sigma (x_i - y_i) ^ 2$.
- `<#>`: negative dot product, defined as $- \Sigma x_iy_i$.
- `<=>`: negative cosine similarity, defined as $- \frac{\Sigma x_iy_i}{\sqrt{\Sigma x_i^2 \Sigma y_i^2}}$.
- `<=>`: cosine distance, defined as $1 - \frac{\Sigma x_iy_i}{\sqrt{\Sigma x_i^2 \Sigma y_i^2}}$.

```sql
-- call the distance function through operators
Expand Down