-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add geometric mean normalization for scores (#239)
* Add geometric mean normalization for scores Signed-off-by: Martin Gaievski <[email protected]>
- Loading branch information
1 parent
1f67b94
commit b867e69
Showing
15 changed files
with
1,187 additions
and
386 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...opensearch/neuralsearch/processor/combination/GeometricMeanScoreCombinationTechnique.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.neuralsearch.processor.combination; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Abstracts combination of scores based on geometrical mean method | ||
*/ | ||
public class GeometricMeanScoreCombinationTechnique implements ScoreCombinationTechnique { | ||
|
||
public static final String TECHNIQUE_NAME = "geometric_mean"; | ||
public static final String PARAM_NAME_WEIGHTS = "weights"; | ||
private static final Set<String> SUPPORTED_PARAMS = Set.of(PARAM_NAME_WEIGHTS); | ||
private static final Float ZERO_SCORE = 0.0f; | ||
private final List<Float> weights; | ||
private final ScoreCombinationUtil scoreCombinationUtil; | ||
|
||
public GeometricMeanScoreCombinationTechnique(final Map<String, Object> params, final ScoreCombinationUtil combinationUtil) { | ||
scoreCombinationUtil = combinationUtil; | ||
scoreCombinationUtil.validateParams(params, SUPPORTED_PARAMS); | ||
weights = scoreCombinationUtil.getWeights(params); | ||
} | ||
|
||
/** | ||
* Weighted geometric mean method for combining scores. | ||
* | ||
* We use formula below to calculate mean. It's based on fact that logarithm of geometric mean is the | ||
* weighted arithmetic mean of the logarithms of individual scores. | ||
* | ||
* geometric_mean = exp(sum(weight_1*ln(score_1) + .... + weight_n*ln(score_n))/sum(weight_1 + ... + weight_n)) | ||
*/ | ||
@Override | ||
public float combine(final float[] scores) { | ||
float weightedLnSum = 0; | ||
float sumOfWeights = 0; | ||
for (int indexOfSubQuery = 0; indexOfSubQuery < scores.length; indexOfSubQuery++) { | ||
float score = scores[indexOfSubQuery]; | ||
if (score <= 0) { | ||
// scores 0.0 need to be skipped, ln() of 0 is not defined | ||
continue; | ||
} | ||
float weight = scoreCombinationUtil.getWeightForSubQuery(weights, indexOfSubQuery); | ||
sumOfWeights += weight; | ||
weightedLnSum += weight * Math.log(score); | ||
} | ||
return sumOfWeights == 0 ? ZERO_SCORE : (float) Math.exp(weightedLnSum / sumOfWeights); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.