Skip to content

Commit

Permalink
fix: don't use max
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardopinosio committed Apr 30, 2024
1 parent 73ca043 commit a87cfb4
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions utils/vectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"fmt"
"math"

"golang.org/x/exp/slices" // like in tokenClassification.go
)

Expand Down Expand Up @@ -78,8 +79,11 @@ func Norm(v []float32, p int) float64 {

// Normalize single vector according to: https://pytorch.org/docs/stable/generated/torch.nn.functional.normalize.html
func Normalize(embedding []float32, p int) []float32 {
const eps = 1e-12
normalizeDenominator := float32(max(Norm(embedding, p), eps))

This comment has been minimized.

Copy link
@sedletsky-f5

sedletsky-f5 Apr 30, 2024

Collaborator

you could just use Math.max - then the code would stay more readable and with eps constant :-)

This comment has been minimized.

Copy link
@riccardopinosio

riccardopinosio May 1, 2024

Author Collaborator

Fair enough, I might change it to Math.max

var normalizeDenominator float32 = 1e-12
embeddingNorm := float32(Norm(embedding, p))
if embeddingNorm > normalizeDenominator {
normalizeDenominator = embeddingNorm
}
for i, v := range embedding {
embedding[i] = v / normalizeDenominator
}
Expand Down

0 comments on commit a87cfb4

Please sign in to comment.