Skip to content

fix: exclude exponential loss for GradientBoostingClassifier on multiclass targets#120

Open
kimusaku wants to merge 2 commits intomainfrom
fix/gradient-boosting-multiclass-loss
Open

fix: exclude exponential loss for GradientBoostingClassifier on multiclass targets#120
kimusaku wants to merge 2 commits intomainfrom
fix/gradient-boosting-multiclass-loss

Conversation

@kimusaku
Copy link
Copy Markdown
Contributor

Problem

Closes #64.

sklearn’s ExponentialLoss only supports binary classification. When hyperparameter tuning was enabled with GradientBoostingClassifier on a multiclass target, optuna would occasionally sample loss='exponential', raising:

ValueError: ExponentialLoss requires 2 classes; got 3 class(es).

The same bug also listed 'deviance' as a loss candidate — that alias was removed in sklearn 1.3 and would raise ValueError on any modern install.

Changes

sapientml_core/templates/model_templates/hyperparameters.py.jinja

  • Gate 'exponential' behind {% if not is_multiclass %} — binary-only, never emitted for multiclass targets.
  • Remove 'deviance' entirely (removed in sklearn 1.3).

sapientml_core/adaptation/generation/pipeline_template.py

  • Forward pipeline.task.is_multiclass into the hyperparameters template render context so the Jinja guard has the information it needs.

tests/sapientml/test_hyperparameters_template.py

  • Update _render helper to accept an optional is_multiclass parameter (default False, preserving all existing tests).
  • Add four new regression tests:
    1. test_gradient_boosting_classifier_multiclass_excludes_exponential — verifies 'exponential' never appears when is_multiclass=True.
    2. test_gradient_boosting_classifier_binary_includes_exponential — verifies 'exponential' is available for binary classification.
    3. test_gradient_boosting_classifier_always_excludes_deviance — verifies 'deviance' never appears in any rendering.
    4. test_gradient_boosting_multiclass_tuning_completes_without_error — end-to-end optuna study on a synthetic 3-class dataset that must complete all trials without ValueError.

Test results

All 38 tests pass (pytest tests/sapientml/test_hyperparameters_template.py).

…class targets (#64)

sklearn's ExponentialLoss only supports binary classification.  When
hyperparameter tuning was enabled with GradientBoostingClassifier on a
multiclass target, optuna would occasionally sample loss='exponential',
triggering "ExponentialLoss requires 2 classes".

Changes:
- hyperparameters.py.jinja: gate 'exponential' behind is_multiclass check;
  also drop the long-removed 'deviance' alias (removed in sklearn 1.3)
- pipeline_template.py: forward pipeline.task.is_multiclass into the
  hyperparameters template render context
- test_hyperparameters_template.py: add four regression tests covering
  multiclass/binary template rendering and an end-to-end optuna study
  on a 3-class synthetic dataset

Co-authored-by: openhands <openhands@all-hands.dev>
Signed-off-by: openhands <openhands@all-hands.dev>
@kimusaku kimusaku requested a review from a team as a code owner March 26, 2026 06:35
@kimusaku kimusaku requested review from AkiraUra and fukuta-flab and removed request for a team March 26, 2026 06:35
Co-authored-by: openhands <openhands@all-hands.dev>
Signed-off-by: openhands <openhands@all-hands.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A ValueError occurs during hyperparameter tuning in the candidate script using GradientBoosting

2 participants