Skip to content

Fix TSNE gradient computation - #421

Merged
andrewdalpino merged 7 commits into
masterfrom
fix-t-sne-gradient
Aug 18, 2026
Merged

Fix TSNE gradient computation#421
andrewdalpino merged 7 commits into
masterfrom
fix-t-sne-gradient

Conversation

@andrewdalpino

Copy link
Copy Markdown
Member

t-SNE gradient is mathematically wrong — src/Transformers/TSNE.php:523-543 The q-kernel uses raw (non-squared) Euclidean distances (Euclidean::compute() returns sqrt(Σ(a−b)²)), the gradient weight is raw d instead of (1 + d²/ν)^⁻¹, and Q is normalized by sum()×2 including the diagonal (all 1.0). The embedding does not minimize the documented KL divergence; wrong output is produced silently.

@andrewdalpino
andrewdalpino requested review from a team and a lite review from Copilot August 15, 2026 22:51
@andrewdalpino andrewdalpino added the bug Something isn't working label Aug 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to correct the mathematical implementation of the t-SNE gradient computation so that optimization steps better align with the intended KL divergence objective.

Changes:

  • Update the low-dimensional Student-t kernel to use squared distances and adjust Q normalization to exclude the diagonal.
  • Add a targeted unit test for the gradient() calculation.
  • Document the fix in the changelog.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Transformers/TSNE.php Fixes kernel/normalization pieces used in gradient computation.
tests/Transformers/TSNETest.php Adds a regression test for gradient behavior (and helper to invoke it).
CHANGELOG.md Records the fix under a new 2.5.3 entry.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/Transformers/TSNETest.php
Comment thread tests/Transformers/TSNETest.php
Comment thread src/Transformers/TSNE.php Outdated
andrewdalpino and others added 6 commits August 15, 2026 17:56
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@apphp

apphp commented Aug 16, 2026

Copy link
Copy Markdown

Very good fix!

@andrewdalpino
andrewdalpino merged commit 33b4db8 into master Aug 18, 2026
12 checks passed
@andrewdalpino
andrewdalpino deleted the fix-t-sne-gradient branch August 18, 2026 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants