Skip to content

fix(training): forward optimizer_kwargs and respect a custom regression loss - #430

Open
ChrisW09 wants to merge 2 commits into
mainfrom
fix/optimizer-kwargs
Open

fix(training): forward optimizer_kwargs and respect a custom regression loss#430
ChrisW09 wants to merge 2 commits into
mainfrom
fix/optimizer-kwargs

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #415

Problems

  1. TrainerConfig.optimizer_kwargs are silently discarded. _FitMixin.fit passes them into TaskModel(optimizer_args=...), which routes them through normalize_optimizer_kwargs — a filter that keeps only keys prefixed optimizer_. Modern unprefixed kwargs are all dropped, and the optimizer trains with torch defaults. Reproduced live: optimizer_kwargs={"eps": 1e-1, "betas": (0.5, 0.6)} → optimizer runs with eps=1e-08, betas=(0.9, 0.999).
  2. A custom regression loss is overwritten. The num_classes == 1 branch of TaskModel.__init__ does self.loss_fct = nn.MSELoss() unconditionally, while the binary and multiclass branches guard with if not self.loss_fct. The docstring documents loss_fct as "Custom loss function overriding the automatic selection".

Fix

  • normalize_optimizer_kwargs strips the legacy prefix when present and passes unprefixed keys through; lr/weight_decay are reserved in both spellings because build_optimizer receives them explicitly (forwarding them would raise a duplicate-keyword error).
  • Guard the regression branch with if not self.loss_fct, matching the other two.

Tests

  • test_unprefixed_keys_pass_through, test_reserved_keys_dropped_regardless_of_prefix
  • test_trainer_config_optimizer_kwargs_reach_the_optimizer — end-to-end: fits a model and asserts eps/betas on the live optimizer param group.
  • New tests/test_lightning_module_loss.py covering custom/default loss selection for all three task types.

🤖 Generated with Claude Code

ChrisW09 and others added 2 commits July 27, 2026 21:24
…on loss

normalize_optimizer_kwargs kept only keys prefixed with 'optimizer_', so
the documented TrainerConfig.optimizer_kwargs={'eps': ..., 'betas': ...}
was discarded and the optimizer silently trained with torch defaults.
Unprefixed keys now pass through; lr and weight_decay are dropped in
both spellings because build_optimizer receives them explicitly.

TaskModel's num_classes==1 branch also replaced a user-supplied loss_fct
with MSELoss unconditionally, unlike the binary and multiclass branches,
contradicting the documented 'custom loss function overriding the
automatic selection'.

Fixes #415

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
model._task_model is typed ITaskModel | None, which has no .trainer; cast
to Any for the end-to-end optimizer inspection.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

[BUG] TrainerConfig.optimizer_kwargs are silently dropped

1 participant