From a87cfb4f7e2ef663a2da83d86e0a3b34cc21ee3e Mon Sep 17 00:00:00 2001 From: riccardopinosio Date: Tue, 30 Apr 2024 10:59:17 +0200 Subject: [PATCH] fix: don't use max --- utils/vectors.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/utils/vectors.go b/utils/vectors.go index 75030d4..265773a 100644 --- a/utils/vectors.go +++ b/utils/vectors.go @@ -3,6 +3,7 @@ package util import ( "fmt" "math" + "golang.org/x/exp/slices" // like in tokenClassification.go ) @@ -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)) + var normalizeDenominator float32 = 1e-12 + embeddingNorm := float32(Norm(embedding, p)) + if embeddingNorm > normalizeDenominator { + normalizeDenominator = embeddingNorm + } for i, v := range embedding { embedding[i] = v / normalizeDenominator }