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

Assume normalized vecs #265

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Use anonymous wasm modules for better performance [#264](https://github.com/hypermodeAI/runtime/pull/264)
- Support optional parameters [#269](https://github.com/hypermodeAI/runtime/pull/269)
- Handle null parameters [#270](https://github.com/hypermodeAI/runtime/pull/2270)
- Assume normalized vectors when calculating cosine similarity [#265](https://github.com/hypermodeAI/runtime/pull/265)

## 2024-06-26 - Version 0.9.4

Expand Down
5 changes: 5 additions & 0 deletions collections/in_mem/sequential/vector_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func TestMultipleSequentialVectorIndexes(t *testing.T) {
for k := range vecs[j] {
vecs[j][k] += float32(i) / 10
}
var err error
vecs[j], err = utils.Normalize(vecs[j])
if err != nil {
t.Errorf("Failed to normalize vector: %v", err)
}
}

err := index.InsertVectorsToMemory(ctx, textIds, textIds, keys, vecs)
Expand Down
21 changes: 13 additions & 8 deletions collections/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ func IsBetterScoreForSimilarity(a, b float64) bool {
return a > b
}

func Normalize(v []float32) ([]float32, error) {
norm := norm(v)
if norm == 0 {
return nil, errors.New("can not normalize vector with zero norm")
}
for i := range v {
v[i] /= norm
}
return v, nil
}

func norm(v []float32) float32 {
vectorNorm, _ := DotProduct(v, v)
return math32.Sqrt(vectorNorm)
Expand All @@ -52,20 +63,14 @@ func DotProduct(a, b []float32) (float32, error) {
return dotProduct, nil
}

// assume normalization for vectors
func CosineSimilarity(a, b []float32) (float64, error) {
dotProd, err := DotProduct(a, b)
if err != nil {
return 0, err
}
normA := norm(a)
normB := norm(b)
if normA == 0 || normB == 0 {
err := errors.New("can not compute cosine similarity on zero vector")
var empty float64
return empty, err
}

return float64(dotProd) / (float64(normA) * float64(normB)), nil
return float64(dotProd), nil
}

func ConcatStrings(strs ...string) string {
Expand Down