Skip to content

Add Bounded Window to Inference Models for Rescoring to Ensure Positive Score Range #125694

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

Merged

Conversation

markjhoy
Copy link
Contributor

@markjhoy markjhoy commented Mar 26, 2025

This PR allows LTR configuration model inference to get rid of negative score that can be issued by model trained using xgboost or other framework. The negative score can cause issues if used in Lucene where they are forbidden.

The implementation here checks the model's predictive bounds (when possible) to see if the minimum predicted value will be below zero, and if so adjust the scores upward so the minimum value is 0 by sliding all the predicted values upwards in the result (thus, keeping the rankings and the relativity of the scores to each other).

@markjhoy markjhoy requested a review from afoucret March 26, 2025 22:00
@markjhoy markjhoy added >bug :Search Relevance/Ranking Scoring, rescoring, rank evaluation. v8.19.0 labels Mar 26, 2025
@markjhoy markjhoy marked this pull request as ready for review March 26, 2025 22:04
@elasticsearchmachine elasticsearchmachine added the Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch label Mar 26, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-relevance (Team:Search Relevance)

this(model, model.getMinPredictedValue(), model.getMaxPredictedValue());
}

public BoundedWindowInferenceModel(BoundedInferenceModel model, double minPredictedValue, double maxPredictedValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The absolute minimum of 0 is only for LTR models but we can imagine situations were we want to scale the result of a regression model in [-1; 1].

I would personally remove the adjusment from the constructor cause it does not make sense:

public BoundedWindowInferenceModel(BoundedInferenceModel model, double minPredictedValue, double maxPredictedValue) {
        this.model = model;
        this.minPredictedValue = minPredictedValue;
        this.maxPredictedValue = maxPredictedValue;
}

Then you can create a static method (scaleLtrModel ?) that would do the following:

public scaleLtrModel(BoundedInferenceModel model) {
  int adjustment =  LTR_MIN_PREDICTED_VALUE - model.getMinPredictedValue();
  return new BoundedWindowInferenceModel(model, model.getMinPredictedValue() + adjustment, model.getMaxPredictedValue() + adjustment)
}

Then you can use the formula of the POC to scale the prediction:

double predictedValue = ((Number) regressionInferenceResults.predictedValue()).doubleValue();
// First we scale the data to [0 ,1]
predictedValue = (predictedValue - model.getMinPredictedValue()) / (model.getMaxPredictedValue() - model.getMinPredictedValue());

// Then we scale the data to the desired interval
predictedValue = predictedValue * (getMaxPredictedValue() - getMinPredictedValue()) + getMinPredictedValue();

Also I would rename the class into something like MinMaxScaledInferenceModel

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The absolute minimum of 0 is only for LTR models but we can imagine situations were we want to scale the result of a regression model in [-1; 1].

I thought we had decided with @jimczi that we would not perform scaling, but rather slide the scores up to ensure they are all positive (only if the minimum score was negative). Correct?

The absolute minimum of 0 is only for LTR models but we can imagine situations were we want to scale the result of a regression model in [-1; 1].

That makes sense. Is there a need for this now to fix this bug though? I was under the impression that the bug was only about the negative scores being returned and us having to deal with that if true.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I proposed "sliding" the score because we can apply it on the minimum and maximum value for a model entirely (instead of per query). This means that scores will still be comparable between queries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jimczi - what are you thoughts on the PR as-is then?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Let's use the new minPredictedValue and maxPredictedValue from the BoundedInferenceModel directly, no need to make it configurable for now.

Copy link
Contributor Author

@markjhoy markjhoy Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afoucret -

Then you can use the formula of the POC to scale the prediction:
double predictedValue = ((Number) regressionInferenceResults.predictedValue()).doubleValue(); // First we scale the data to [0 ,1] predictedValue = (predictedValue - model.getMinPredictedValue()) / (model.getMaxPredictedValue() - model.getMinPredictedValue());

Looking at this here -- would scaling in this method (normalizing it to 0 -> 1.0 first) fall victim to what we're trying to avoid here - that is, doing this may compress the space and lose precision for the scores and might cause closely scored items to have equal rank?

I think it's a good idea overall, and certainly more flexible for the developer though... so, I'm on the fence about it. cc: @jimczi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afoucret - the more I think about this, for the time being, let's go with this solution, and if we decide to add in your suggestion, let's do that afterwords so we can unblock your work.

@markjhoy markjhoy requested a review from afoucret March 27, 2025 13:47
@jimczi jimczi added the :ml Machine learning label Mar 28, 2025
@elasticsearchmachine elasticsearchmachine added the Team:ML Meta label for the ML team label Mar 28, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/ml-core (Team:ML)

Copy link
Contributor

@jimczi jimczi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach looks good to me.
Pinging @elastic/ml-core for a validation here. We're enforcing all retrievers/query to return positive scores and this PR handles the tree inference models.

@markjhoy markjhoy requested a review from a team April 1, 2025 13:15
@markjhoy markjhoy requested a review from jimczi April 1, 2025 21:42
@markjhoy markjhoy merged commit e77bf80 into elastic:main Apr 2, 2025
17 checks passed
@markjhoy markjhoy added the auto-backport Automatically create backport pull requests when merged label Apr 2, 2025
@markjhoy
Copy link
Contributor Author

markjhoy commented Apr 2, 2025

💚 All backports created successfully

Status Branch Result
8.x

Questions ?

Please refer to the Backport tool documentation

markjhoy added a commit to markjhoy/elasticsearch that referenced this pull request Apr 2, 2025
…ve Score Range (elastic#125694)

* apply bounded window inference model

* linting

* add unit tests

* [CI] Auto commit changes from spotless

* add additional tests

* remove unused constructor

---------

Co-authored-by: elasticsearchmachine <[email protected]>
(cherry picked from commit e77bf80)
andreidan pushed a commit to andreidan/elasticsearch that referenced this pull request Apr 9, 2025
…ve Score Range (elastic#125694)

* apply bounded window inference model

* linting

* add unit tests

* [CI] Auto commit changes from spotless

* add additional tests

* remove unused constructor

---------

Co-authored-by: elasticsearchmachine <[email protected]>
markjhoy added a commit that referenced this pull request Apr 23, 2025
…ve Score Range (#125694) (#126149)

* apply bounded window inference model

* linting

* add unit tests

* [CI] Auto commit changes from spotless

* add additional tests

* remove unused constructor

---------

Co-authored-by: elasticsearchmachine <[email protected]>
(cherry picked from commit e77bf80)
markjhoy added a commit to markjhoy/elasticsearch that referenced this pull request Apr 24, 2025
…ve Score Range (elastic#125694)

* apply bounded window inference model

* linting

* add unit tests

* [CI] Auto commit changes from spotless

* add additional tests

* remove unused constructor

---------

Co-authored-by: elasticsearchmachine <[email protected]>
(cherry picked from commit e77bf80)
@markjhoy
Copy link
Contributor Author

💚 All backports created successfully

Status Branch Result
9.0

Questions ?

Please refer to the Backport tool documentation

markjhoy added a commit that referenced this pull request Apr 24, 2025
…ve Score Range (#125694) (#127345)

* apply bounded window inference model

* linting

* add unit tests

* [CI] Auto commit changes from spotless

* add additional tests

* remove unused constructor

---------

Co-authored-by: elasticsearchmachine <[email protected]>
(cherry picked from commit e77bf80)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Automatically create backport pull requests when merged >bug :ml Machine learning :Search Relevance/Ranking Scoring, rescoring, rank evaluation. Team:ML Meta label for the ML team Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v8.19.0 v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants