Add use_dual to renyi_entropy and log_determinant - #244
Open
AnnaWegmann wants to merge 3 commits into
Open
Conversation
With use_dual=True (the default) the cosine kernel is built through the d x d dual matrix X'^T X' whenever it is smaller than the n x n Gram matrix X' X'^T. The two share the eigenvalue spectrum the measures are computed from, so results agree up to floating point while large inputs never materialize an n x n matrix; log_determinant adds the structural zero eigenvalues' log(eps) contribution in closed form. use_dual=False keeps the full n x n construction, and the test compares the two paths on the same data. vendi_score already has the same flag with the same semantics. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Adds a
use_dualflag (defaultTrue) torenyi_entropyandlog_determinant, with the same semantics as the existing flag onvendi_score:use_dual=True: the cosine kernel is built through the d×d dual matrixX'ᵀ X'whenever it is smaller than the n×n Gram matrixX' X'ᵀ. The two share their nonzero eigenvalues, trace, and Frobenius norm — everything the measures consume — so values agree with the full construction up to floating point, while an n=100k input needs a ~1 MB matrix instead of an 80 GB one (which previously OOM'd). Forlog_determinant, the rank-deficient kernel'sn − dstructural zero eigenvalues enterlog det(K + εI)in closed form (each contributeslog ε).use_dual=False: the full n×n construction, unchanged from before.At n=100k, d=384 (M1 Pro):
renyi_entropy0.32 s,log_determinant0.16 s with the default; both out-of-memory before.Tests run each measure both ways on the same data and require agreement to 1e-10 (both orientations, n>d and n<d), plus a speed check that the dual route wins for n ≫ d. The
log_determinantcomparison useseps=1e-2: at small ε, the full path's structural zero eigenvalues surface as ~1e-15 floating-point noise thatlog(λ+ε)amplifies by 1/ε (≈3e-9 at the default ε=1e-6), while the dual path carries them exactly — the larger ε removes that amplification so the 1e-10 bound is meaningful.🤖 Generated with Claude Code