Skip to content

fix(embedding): mean-centre the neural space so semantic search actually works - #329

Merged
SMK1705 merged 2 commits into
mainfrom
fix/neural-embedder-anisotropy
Jul 28, 2026
Merged

fix(embedding): mean-centre the neural space so semantic search actually works#329
SMK1705 merged 2 commits into
mainfrom
fix/neural-embedder-anisotropy

Conversation

@SMK1705

@SMK1705 SMK1705 commented Jul 28, 2026

Copy link
Copy Markdown
Owner

SMK1705 added 2 commits July 28, 2026 13:39
…lly works (#316)

The on-device run of the opt-in neural embedder found it useless: the SAME handful of
notes surfaced for completely unrelated queries, and genuinely-related items didn't.
Instrumenting the cosine distribution on-device showed why — USE is strongly
ANISOTROPIC: every note scored 0.6–0.95 to ANY query (mean ~0.78), so a few "hub"
vectors sit close to everything and dominate. The searchFloor was a red herring; no
threshold can fix a space where the hubs out-score the real match for every query.

Fix: mean-centre the vectors before comparing. Subtracting the corpus centroid removes
the shared hub direction so the residuals discriminate — the standard remedy for
anisotropic sentence embeddings.

- Embedder.meanCenter (default false); MediaPipeEmbedder sets it true, HashingEmbedder
  (isotropic) stays off; RoutingEmbedder delegates.
- SemanticIndex.scores() centres the query and each note by the SAME corpus mean and
  re-normalises when the active embedder asks for it. Search-time, so no stored state
  and no migration; the mean is always consistent with the current corpus.
- searchFloor recalibrated for the CENTRED space: 0.55 (raw) -> 0.50. On-device the
  centred distribution spreads to ~[-0.4, +0.9], mean ~0; genuine zero-overlap matches
  land ~0.53–0.77, noise is near zero, so 0.50 keeps the matches and drops the bulk.

Verified on device (Galaxy S25): before, "kitchen sink tap" returned unrelated finance
notes and missed the plumber note; after, it returns exactly "Call the plumber about
the leak" — a real semantic match with zero shared words, the thing USE was added for.
Post-centre distribution mean fell 0.78 -> ~0.01 and per-query top results became
distinct and relevant.

Tests: SemanticIndexTest gains an anisotropic stand-in embedder and proves the
mechanism — without centring an unrelated note scores >0.95 and the true match beats
it by <0.05; with centring the match stays >0.8 and unrelated notes collapse below 0.1.

Residual limitation (noted on #316): a couple of broadly-similar "hub" notes still sit
~0.5–0.7 after centring; full "all-but-the-top" PC removal would tighten it further,
but that's over-investment for an opt-in fallback.
…arger APK

CI's packageDebug hit java.lang.OutOfMemoryError at the 2 GB daemon heap now that the
APK is ~199 MB — the MediaPipe tasks-text native runtime added in #328 pushed packaging
over the edge (#328 passed narrowly; the next build tipped it). GitHub runners have ~7 GB,
so 4 GB leaves comfortable headroom.
@SMK1705
SMK1705 merged commit 3ce1cd8 into main Jul 28, 2026
1 check passed
@SMK1705
SMK1705 deleted the fix/neural-embedder-anisotropy branch July 28, 2026 17: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.

1 participant