Skip to content

TSNE segfaults when numbers turn to NaN #32788

Closed

Description

@david-cortes

Describe the bug

When running TSNE.fit_transform, if numbers produced during the initialization somehow turned to NaN, it will segfault the process instead of throwing a python exception.

Steps/Code to Reproduce

import numpy as np
from sklearn.manifold import TSNE
X = np.ones((10,5))
TSNE(init="pca", perplexity=5).fit_transform(X)

Expected Results

Should not segfault.

Actual Results

Segfaults.

Versions

System:
    python: 3.12.6 | packaged by conda-forge | (main, Sep 22 2024, 14:16:49) [GCC 13.3.0]
executable: /home/david/miniforge3/bin/python
   machine: Linux-6.12.57+deb12-amd64-x86_64-with-glibc2.36

Python dependencies:
      sklearn: 1.7.1
          pip: 24.2
   setuptools: 74.1.2
        numpy: 2.0.1
        scipy: 1.14.1
       Cython: 3.1.0
       pandas: 2.2.3
   matplotlib: 3.9.2
       joblib: 1.4.2
threadpoolctl: 3.5.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 20
         prefix: libscipy_openblas
       filepath: /home/david/.local/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-99b71e71.so
        version: 0.3.27
threading_layer: pthreads
   architecture: Haswell

       user_api: blas
   internal_api: mkl
    num_threads: 14
         prefix: libmkl_rt
       filepath: /home/david/miniforge3/lib/libmkl_rt.so.2
        version: 2023.2-Product
threading_layer: gnu

       user_api: openmp
   internal_api: openmp
    num_threads: 20
         prefix: libgomp
       filepath: /home/david/miniforge3/lib/libgomp.so.1.0.0
        version: None

Activity

  1. added
    Needs Triage
    on Nov 25, 2025
  2. Aaraviitkgp commented on Nov 25, 2025

    @Aaraviitkgp

    @david-cortes I think that pca is giving t-sne func zero value (as variance is 0), so zero gets in denominator (func:X_embedded = X_embedded / np.std(X_embedded[:, 0]) * 1e-4), due to which nan values are produced causing segmentation fault.

  3. nandan2003 commented on Nov 25, 2025

    @nandan2003
    Contributor

    Hi @Aaraviitkgp

    I've opened a PR to solve this issue regarding TSNE segfaults when numbers turn to NaN #32788.

  4. lesteve commented on Nov 25, 2025

    @lesteve
    Member

    @david-cortes this seems like a duplicate of #28368, right?

  5. david-cortes commented on Nov 25, 2025

    @david-cortes
    ContributorAuthor

    Yes, seems to be the same underlying issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions