Skip to content

Ratchet the learning rate on LL decrease (#41) - #43

Merged
neuromechanist merged 2 commits into
mainfrom
41-amica_numpy-long-fit-driftsdegrades-past-150-iters-ll-decreases-component-match-drops
Jul 5, 2026
Merged

neuromechanist merged 2 commits into
mainfrom
41-amica_numpy-long-fit-driftsdegrades-past-150-iters-ll-decreases-component-match-drops

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

The NumPy long fit degraded past ~150 iters (LL decreasing, component match dropping) because pyAMICA's convergence handling diverged from Fortran in two ways:

  1. numdecs/numincs never accumulated. _check_convergence incremented them locally but never returned them, so the caller's counters stayed at their initial values — the decrease handling effectively reset every iteration.
  2. On numdecs >= max_decs pyAMICA stopped, whereas Fortran (amica17.f90:1062-1108) lowers the learning-rate ceiling (lrate0, plus newtrate/rholrate0 under Newton), resets numdecs, and continues. Without the ceiling ratchet, lrate kept ramping back up (it's raised each iter in _update_parameters) and the fit oscillated/drifted.

Changes

  • Rewrote _check_convergence to match Fortran: thread numdecs/numincs back into the loop, ratchet lrate0/newtrate/rholrate0 down at max_decs and continue (only a lrate/gradient floor terminates on a decrease), stop after maxincs consecutive small increases (new maxincs param, Fortran default 5), and reset numdecs when Newton starts.

Effect

  • seed=0: the LL now improves over the long run — -3.399 at 2000 iters, vs the pre-fix -3.409 and the ~150-iter -3.404 (and better than Fortran's -3.402). No more degradation.
  • The 150-iter test_sample_data_numpy_vs_fortran (mean-corr > 0.9) still passes — no parity regression.
  • Non-slow suite: 36 passed, 1 xfailed.

Honest status on test_sample_data_cli

Still xfail. The #41 LL-degradation drift is fixed, but the test's strict all-32-components > 0.8 gate still fails (~27/32): at full convergence pyAMICA reaches a comparable/slightly-better-LL but different-partition solution than the Fortran reference on a few ill-determined components — a solution-basin difference (the single-model tail of #27's partition theme), not the drift. The project's other parity test uses the more appropriate mean > 0.9 bar and passes. The xfail reason documents this.

Related

The NumPy long fit degraded past ~150 iters (LL decreasing, component match
dropping) because the convergence handling diverged from Fortran:

- numdecs/numincs were incremented inside _check_convergence but never
  returned, so the caller's counters never accumulated -- the decrease
  handling effectively reset every iteration.
- On numdecs >= max_decs pyAMICA STOPPED, whereas Fortran
  (amica17.f90:1062-1108) lowers the learning-rate CEILING (lrate0, plus
  newtrate/rholrate0 under Newton), resets numdecs, and CONTINUES. Without the
  ceiling ratchet, lrate kept ramping back up and the fit oscillated/drifted.

Rewrote _check_convergence to match Fortran: thread numdecs/numincs back to
the loop, ratchet lrate0/newtrate/rholrate0 down at max_decs and continue
(only a lrate/gradient floor stops on a decrease), stop after maxincs small
increases (new maxincs param, Fortran default 5), and reset numdecs when
Newton starts.

Effect (seed=0): the LL now improves over the long run (-3.399 at 2000 iters
vs the pre-fix -3.409 and the ~150-iter -3.404), no longer degrades. The
150-iter NumPy-vs-Fortran mean-corr parity test still passes.

Note: the strict all-32-components CLI gate stays xfail on a residual
solution-basin difference (pyAMICA reaches a comparable/better-LL but
different-partition solution on a few ill-determined components); the
LL-degradation drift itself is fixed.
- Compute the weight-gradient norm every iteration (was gated on
  use_grad_norm), matching Fortran's always-computed ndtmpsum. The
  decrease-stop gradient floor now applies regardless of use_grad_norm
  (Fortran's `ndtmpsum <= min_nd` is unconditional; the flag only gates the
  separate final gradient-norm stop), closing a parity gap for
  use_grad_norm=False.
- Add test_check_convergence_ratchets_lrate_on_decrease: drives the
  convergence handler directly and asserts the lrate0/newtrate ceilings
  ratchet down and numdecs resets after max_decs decreases (not a stop).

Tested: non-slow suite 37 passed, 1 xfailed.
@neuromechanist

Copy link
Copy Markdown
Member Author

Review (code-reviewer, Sonnet) + resolutions

The reviewer confirmed the ratchet logic matches Fortran (amica17.f90:1062-1108) line-by-line -- the decrease-branch stop condition, the lrate0/newtrate/rholrate0 reductions and their iter>newt_start/do_newton guards, the numdecs reset, the numincs/maxincs handling (default 5, matching the Fortran header), the counter threading, and the newt_start reset placement. No hang/regression risk; the lrate0 ratchet is confirmed to actually affect the lrate ramp ceiling.

Addressed in the follow-up commit:

  • Important (parity gap for use_grad_norm=False): the decrease-branch gradient floor was gated by use_grad_norm, but Fortran's ndtmpsum <= min_nd decrease-stop is unconditional (the flag only gates the final grad-norm stop). Since self.nd was only populated when use_grad_norm was set, the floor silently dropped for use_grad_norm=False. Now the gradient norm is computed every iteration (matching Fortran), and the decrease-stop floor applies unconditionally; use_grad_norm still gates only the separate final stop.
  • Coverage (non-blocking): added a direct unit test for the ratchet (test_check_convergence_ratchets_lrate_on_decrease) so the counter-threading/ceiling-ratchet behavior is regression-guarded without relying on the slow integration test.

Non-slow suite: 37 passed, 1 xfailed (the CLI integration test, on the residual partition-basin difference documented in its xfail reason).

@neuromechanist
neuromechanist merged commit f30081f into main Jul 5, 2026
@neuromechanist
neuromechanist deleted the 41-amica_numpy-long-fit-driftsdegrades-past-150-iters-ll-decreases-component-match-drops branch July 5, 2026 03:57
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.

AMICA_NumPy long fit drifts/degrades past ~150 iters (LL decreases, component match drops)

1 participant