Repository navigation
Normalize numpy_impl log-likelihood to Fortran's scale - #215
Merged
Merged
Conversation
numpy_impl stored a raw sum over samples and channels while Fortran stores LL(iter) = LLtmp2 / dble(numgoodsum*nw) (amica15.f90:1752), so AMICA_NumPy reported -3317862.78 where Fortran reports about -3.3 on the bundled sample. min_dll defaults to 1e-9 and is compared against that delta, so use_min_dll could never fire from real convergence. Four call sites had been hand-compensating for the wrong scale and now stop: the cross-backend LL comparison in test_ng_backend.py, the results column in benchmark_dimsweep.py, and the LLt check in test_sample_data.py. The gradient norm self.nd has the same class of defect and is NOT fixed here; a comment at the site records why. See issue #212. Closes #212 partially
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses the log-likelihood half of #212. Found while implementing #207, which had assumed
numpy_implwas the Fortran-faithful reference for the convergence criteria.The bug
Fortran stores a normalized per-iteration log-likelihood (
amica15.f90:1752):numpy_implstored the raw sum. On the bundled sampleAMICA_NumPyreported-3317862.78where Fortran reports about-3.3, a factor of roughly 1e6 (30504 samples x 32 channels). Sincemin_dlldefaults to1e-9and is compared againstll[-1] - ll[-2],use_min_dllcould never fire from genuine convergence: themaxincscounter around it was correct but unreachable.After this change, the same fit reports
ll[-1] = -3.443.Why it survived
Four call sites were hand-compensating, each locally correct:
pamica/tests/torch_tests/test_ng_backend.pydivided by(n_samples * NW)before comparing backendsbenchmarks/benchmark_dimsweep.pydivided by(data.shape[0] * data.shape[1])before reportingpamica/tests/test_sample_data.pydivided bynum_samplesbefore checking against the writtenLLtAll three now compare directly. That pattern is the reason the wrong convention was invisible: nothing ever saw the raw value in a context where it looked wrong.
Deliberately not fixed here
self.nd(weight-gradient norm) has the same class of defect and is worse: after this change it still reads ~5.4e3 where Fortran reports ~1e-5. Fortran dividesdAkbyzetabefore squaring and masks bycomp_used(amica15.f90:1739-1743);numpy_impldoes neither.AMICATorchNGdoes both, so this is a NumPy-only divergence. Consequence:min_nd/use_grad_normis unreachable there too, so both Fortran convergence criteria are dead in this backend rather than one.A first attempt at the
comp_usedmask alone was reverted. It did not address the scale, which is the actual problem, and it regressedtest_newton_mstep_matches_numpy_referenceby assertingcomp_used is not Noneon a path where the M-step runs before that attribute is set. A comment at the site now records the divergence instead of leaving it silently wrong. Remaining work is tracked in #212.Verification
pamica/tests/torch_tests/test_ng_backend.py: 38 passed, including the cross-backend LL comparison that previously needed the manual rescale.pamica/tests/test_numpy_reject.py: passed; thedo_rejectreasoning still holds, since dropping below-average samples raises a mean just as it raised a sum.torch_impl/core.pyis a comment correcting a now-stale claim that the NumPy backend compares un-normalized log-likelihood.