Skip to content

Add _post_step hook to BaseGradientModel for proximal-gradient variants - #229

Closed
jameschapman19 wants to merge 1 commit into
mainfrom
claude/gradient-model-post-step-hook
Closed

Add _post_step hook to BaseGradientModel for proximal-gradient variants#229
jameschapman19 wants to merge 1 commit into
mainfrom
claude/gradient-model-post-step-hook

Conversation

@jameschapman19

Copy link
Copy Markdown
Owner

Summary

  • Adds a _post_step(weights) hook to BaseGradientModel, called on the weights right after the momentum update and before the tol convergence check. Default implementation is the identity (no-op), so this is fully backward compatible — every existing model's fit trajectory is unchanged.
  • Motivation: a proximal-gradient variant of CCA_EY (e.g. adding a sparsity penalty on the weights) currently has no way to add a proximal step without copy-pasting the entire _gradient_descent loop just to insert one line. With this hook, such a variant only needs to override _post_step — momentum, batching, the data-informed initial weights, and the convergence check are all inherited unchanged.
  • This is motivated by an external project (a sparse-CCA method built on the unconstrained EY objective) that currently reimplements its own training loop from scratch to add a group-lasso/L1 proximal step; with this hook it can become a thin CCA_EY subclass instead.

Test plan

  • test_post_step_default_is_identity: the unmodified base hook produces byte-identical weights to not having the hook at all.
  • test_post_step_override_is_applied_during_training: an overridden hook (hard-thresholding) is confirmed to fire on every iteration (not just once at the end, which a naive test could miss) via a call counter, plus the expected effect on the final fitted weights.
  • Full existing suite: pytest tests/ -q → 511 passed, 7 skipped (unrelated skips), no regressions.

Generated by Claude Code

Subclasses that need a proximal step after each momentum update (e.g. a
sparsity penalty on the weights) currently have no way to add one without
copy-pasting the whole _gradient_descent loop. _post_step is a no-op hook
called on the weights right after the momentum update and before the
convergence check, so a proximal-gradient variant only needs to override
this one method -- momentum, batching, initial weights, and the tol check
are all inherited unchanged.

Two tests: the default hook is provably a no-op (identical fit to the
unmodified base class), and an overridden hook actually fires every
iteration (not just once at the end), verified via a call counter plus
the expected effect on the final weights.
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.

2 participants