Repository navigation
Conversation
6760313 to
dc779c3
Compare
|
There are actually two issues here. One is that PCA initialization cannot be scaled to have std 1e-4 if all input points are identical (as in #28368) and PCA has zero variance:
Another issue is that Barnes--Hut can cause problems if some points overlap exactly, as reported in #28368:
This will happen in PCA initialization if some input points are identical. A simple workaround to prevent both issues is to add a small amount of random noise to the PCA initialization. This is what is done in the For example: This should fix the reported issues. I don't think we necessarily need to remove nans/infs from the user-provided init... If a user explicitly provides a broken initialization, then it's their problem. |
|
I think we also need good tests to see how each solution's dealing with the issue and which one's really necessary. @dkobak do you have good reproducers at hand? |
I agree.
No, not really. But I now tried the following: and while this does not crash, it reports negative Kullback-Leibler divergence, which is clearly mathematically impossible: Running the same code with I am pretty sure that the negative KL values are due to points exactly overlapping at initialization, and consequently Barnes--Hut experiencing weird issues (which apparently can sometimes lead to a crash). Adding a small amount of noise to the PCA initialization should fix this. |
|
Marking as "Closing candidate" to come back to this in case @Tokasumi doesn't respond here. |
Reference Issues/PRs
#28368 reports crashes caused by t-SNE on Windows.
#29127 reports crashes on Mac.
What does this implement/fix? Explain your changes.
Before calling
_tsne(), check the initialization arrayX_embeddedto be all finite.Any other comments?
In
sklearn.manifold._t_sne.TSNE._fit:The line
X_embedded = X_embedded / np.std(X_embedded[:, 0]) * 1e-4would leads to all-NaN entries. This could happen ifnp.std(X_embedded[:, 0])evaluates to zero (The #29127 case) if the first principal component has zero standard deviation.By user input, the line
X_embedded = self.initwould include NaN or Inf entries. The crash caused bysklearn.manifold.TSNEcan be reproduced by inserting a NaN or Inf entry to the initialX_embedded:The code runs into Segmentation fault
Or run with gdb
Same outcome if Inf or -Inf is inserted to the embeddings, and same happens on Windows.
go further...
The
_QuadTreeis used by_kl_divergence_bhto calculate KL-divergence. The_QuadTree.build_tree()is recursive.Result of above snippet:
The cause of the crash is highly suspected to be stack overflow caused by infinite recursion.