← Back to Pull requests

[python] Apply LearningRateScheduler rates to the tree of the same round #12791

Closed
unknown closed this pull request without merging on Nov 04, 2026 10:36 UTC
View on GitHub
unknown unknown commented on Nov 03, 2026 18:19 UTC
Author

LearningRateScheduler sets the learning rate in after_iteration, so a rate only takes effect from the next round. Round i is built with learning_rates(i - 1): the first tree uses the booster's default eta, and the last entry of the schedule is never used. The docstring says the callable gets epoch and "returns the corresponding learning rate", and that a sequence has "the same size of boosting rounds".

import numpy as np, xgboost as xgb
from xgboost.callback import LearningRateScheduler

rng = np.random.default_rng(0)
X = rng.normal(size=(500, 5))
y = X[:, 0] * 3 + rng.normal(size=500)
d = xgb.DMatrix(X, label=y)
params = {"max_depth": 2, "base_score": 0.0}
rates = [0.01, 0.5, 0.1, 0.9]

scheduled = xgb.train(params, d, 4, callbacks=[LearningRateScheduler(rates)])
manual = None
for rate in rates:
    manual = xgb.train({**params, "learning_rate": rate}, d, 1, xgb_model=manual)

print(np.allclose(scheduled.predict(d), manual.predict(d)))

Largest absolute leaf value per tree:

| tree | rate | master | this PR | built by hand |
|---|---|---|---|---|
| 0 | 0.01 | 1.4499 | 0.0483 | 0.0483 |
| 1 | 0.5 | 0.0417 | 2.3926 | 2.3926 |
| 2 | 0.1 | 2.0622 | 0.389 | 0.389 |
| 3 | 0.9 | 0.2636 | 3.0153 | 3.0153 |

On master the script prints False, with this PR True. The first row on master is what the default eta=0.3 gives.

The fix moves the update to before_iteration, which is also where the R package's xgb.cb.reset.parameters sets it (f_before_iter), so the two interfaces now agree. xgb.cv goes through the same callback: with rates [0, 0, 0.5] the test RMSE stays flat for two rounds and then drops.

This changes results for existing users of the callback, as each tree now gets the rate one position earlier in the schedule, so I added a versionchanged note to the docstring. If you would rather handle the behaviour change differently, let me know.

Tests: the two helpers in xgboost/testing/callbacks.py encoded the old timing, so they are updated.

  • run_eta_decay asserted that a schedule leaves the first round's error untouched. It now asserts the first round differs. The last round is left out of the comparison because the large rates in that test bring it back to the error of a constant prediction.

  • run_eta_decay_leaf_output changes the schedule from i > 2, so tree 2 stays equal and tree 3 differs, as before.

  • New: a scheduled model has to dump identically to one built tree by tree with the same rates. This fails on master.
  • tests/python/test_callback.py and tests/python/test_early_stopping.py pass on macOS with CPU tree methods. The GPU variants of these helpers (tests/python-gpu/test_gpu_callbacks.py) were not run, as I have no NVIDIA GPU.

    Diff: 344 lines (+258 / -86) across 4 files Reviewer: RAMitchell Review duration: 976m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 344 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.350
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #12791"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.4% (MEDIUM) · Higher = larger blast radius
    42.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.7% · Safe to merge
    16.7%
    Repository