Skip to content

Error thrown when using t-SNE with 'cosine' as metric #5772

Closed

Description

@KaffeLatte

Hi,

When I use 'cosine' as metric for sklearn.manifold.tsne I get the following error:
"ValueError: All distances should be positive, either the metric or precomputed distances given as X are not correct".

As the codomain of cosine distance is [-1,1] all output from pairwise comparison will not be positive.

For euclidean distance there is a hard coded solution for keeping distances positive by squaring them (passed as parameter).

For cosine distance I suspect that a simply 'absoluting' the values should be a fix to make it run, however seems like there should be a proper understanding of the algorithm before doing a fix (which I currently do not have unfortunately)

Activity

  1. giorgiop commented on Nov 9, 2015

    @giorgiop
    Contributor

    Can you check if some of your inputs are constant vectors? I am fixing a very similar issue on correlation (#4475) in PR #5333

  2. KaffeLatte commented on Nov 9, 2015

    @KaffeLatte
    Author

    What do you mean by constant vectors?

  3. KaffeLatte commented on Nov 9, 2015

    @KaffeLatte
    Author

    This is exactly what my input looks like:
    array([[ 0.09167306, 0.09066585],
    [ 0.09133385, 0.09077385],
    [ 0.09176718, 0.09076084],
    [ 0.09245387, 0.09060492],
    [ 0.09150095, 0.09072065],
    [ 0.0967673 , 0.08904389],
    [ 0.09183362, 0.09067974],
    [ 0.09125591, 0.09079867],
    [ 0.09250068, 0.0914137 ],
    [ 0.0909817 , 0.09088597],
    [ 0.09112864, 0.09083919],
    [ 0.09127129, 0.09079377],
    [ 0.09172245, 0.09065012],
    [ 0.09142663, 0.09074431],
    [ 0.09805167, 0.08863495],
    [ 0.09121854, 0.09081057],
    [ 0.09177796, 0.09063245],
    [ 0.09156886, 0.09087076],
    [ 0.09269265, 0.09034122],
    [ 0.09136634, 0.09076351],
    [ 0.09399547, 0.08992641],
    [ 0.10255342, 0.08766025],
    [ 0.09143851, 0.09090493],
    [ 0.09090909, 0.09090909],
    [ 0.09092785, 0.09083994],
    [ 0.09192853, 0.09073296],
    [ 0.09185886, 0.09060669],
    [ 0.09105428, 0.09086286],
    [ 0.09240936, 0.09082208],
    [ 0.09284828, 0.09029167],
    [ 0.09101436, 0.09087557],
    [ 0.09196271, 0.08990672],
    [ 0.09265921, 0.09035187],
    [ 0.09175739, 0.09051504],
    [ 0.09163332, 0.09037447],
    [ 0.09151055, 0.09106741],
    [ 0.09380898, 0.08998579],
    [ 0.09241967, 0.09029113],
    [ 0.09512628, 0.08956638],
    [ 0.09366744, 0.09008822],
    [ 0.09127662, 0.09079207],
    [ 0.09522775, 0.08917747],
    [ 0.09091669, 0.09090667],
    [ 0.09091308, 0.09090782],
    [ 0.09128375, 0.0907898 ],
    [ 0.09090909, 0.09090909],
    [ 0.09126985, 0.09079423],
    [ 0.09226533, 0.09071602],
    [ 0.09308521, 0.08984655],
    [ 0.09358604, 0.09005677],
    [ 0.0955665 , 0.08942621],
    [ 0.09229888, 0.09149478],
    [ 0.09291504, 0.09044462],
    [ 0.09143233, 0.09095439],
    [ 0.09134934, 0.09036695],
    [ 0.09090909, 0.09090909],
    [ 0.09090909, 0.09090909],
    [ 0.09090909, 0.09090909],
    [ 0.09336972, 0.09012565],
    [ 0.09420685, 0.0899133 ],
    [ 0.09176772, 0.09063571],
    [ 0.09285949, 0.0902881 ],
    [ 0.09106962, 0.09092466],
    [ 0.09146598, 0.09073178],
    [ 0.09252208, 0.09014651],
    [ 0.09176437, 0.09063678],
    [ 0.09128573, 0.09078917],
    [ 0.09294534, 0.09026077],
    [ 0.09164598, 0.09067447],
    [ 0.09133896, 0.09077222],
    [ 0.09090909, 0.09090909],
    [ 0.09183748, 0.0906135 ],
    [ 0.09152983, 0.09071145],
    [ 0.09121385, 0.09081206],
    [ 0.09187251, 0.09060235],
    [ 0.09355347, 0.09018397],
    [ 0.09090909, 0.09090909],
    [ 0.09262965, 0.09063861],
    [ 0.09161119, 0.09068555],
    [ 0.09094758, 0.09089684],
    [ 0.09216817, 0.09050821],
    [ 0.09975737, 0.08809187],
    [ 0.09134445, 0.09077048],
    [ 0.09376311, 0.0900004 ],
    [ 0.09157299, 0.09069771],
    [ 0.09115721, 0.09083009],
    [ 0.09329357, 0.08966455],
    [ 0.09113151, 0.09083828],
    [ 0.09090909, 0.09090909],
    [ 0.09121276, 0.0908124 ],
    [ 0.09490713, 0.08938893],
    [ 0.09163985, 0.09052225],
    [ 0.09286909, 0.09030785],
    [ 0.09241624, 0.09020385],
    [ 0.09164079, 0.090516 ],
    [ 0.09300068, 0.09030075],
    [ 0.09229067, 0.09046921],
    [ 0.09645867, 0.0898571 ],
    [ 0.09202203, 0.09070106],
    [ 0.09125591, 0.09079867],
    [ 0.09102737, 0.09087143],
    [ 0.09173248, 0.09038022],
    [ 0.0914672 , 0.09073139],
    [ 0.09138648, 0.09070819],
    [ 0.09192827, 0.09041669],
    [ 0.09096157, 0.0909359 ],
    [ 0.09090909, 0.09090909],
    [ 0.09103268, 0.09086974],
    [ 0.09237924, 0.09065317],
    [ 0.09200152, 0.09060078],
    [ 0.09151852, 0.09053708],
    [ 0.09339108, 0.08984899],
    [ 0.09340691, 0.09011381],
    [ 0.09167192, 0.09066621],
    [ 0.09090909, 0.09090909],
    [ 0.09224366, 0.09037891],
    [ 0.09163191, 0.09067895],
    [ 0.10316249, 0.08713437],
    [ 0.0917057 , 0.09143187],
    [ 0.0916372 , 0.09044672],
    [ 0.09095603, 0.09089415],
    [ 0.091606 , 0.0906872 ],
    [ 0.10988504, 0.09604037],
    [ 0.09144098, 0.09073091],
    [ 0.09090909, 0.09090909]])

  4. giorgiop commented on Nov 9, 2015

    @giorgiop
    Contributor

    The issue is in pairwise_distances and not in T-SNE. If I load your example in an array X:

    pairwise_distances(X, metric='euclidean')  # all good
    array([[ 0.        ,  0.00035599,  0.00013372, ...,  0.01898846,
             0.00024103,  0.00080176],
           [ 0.00035599,  0.        ,  0.00043353, ...,  0.01928427,
             0.00011542,  0.00044577],
           [ 0.00013372,  0.00043353,  0.        , ...,  0.01887141,
             0.00032757,  0.0008708 ],
           ..., 
           [ 0.01898846,  0.01928427,  0.01887141, ...,  0.        ,
             0.01919306,  0.01965748],
           [ 0.00024103,  0.00011542,  0.00032757, ...,  0.01919306,
             0.        ,  0.00056094],
           [ 0.00080176,  0.00044577,  0.0008708 , ...,  0.01965748,
             0.00056094,  0.        ]])
    pairwise_distances(X, metric='cosine')  # some are <0, due to numerical errors
    array([[ -2.22044605e-16,   2.99802728e-06,   5.50104406e-11, ...,
              1.89709125e-03,   1.32193126e-06,   1.52560258e-05],
           [  2.99802728e-06,  -2.22044605e-16,   2.97239782e-06, ...,
              2.05084337e-03,   3.38408436e-07,   4.72809680e-06],
           [  5.50104406e-11,   2.97239782e-06,   0.00000000e+00, ...,
              1.89773709e-03,   1.30493105e-06,   1.51981417e-05],
           ..., 
           [  1.89709125e-03,   2.05084337e-03,   1.89773709e-03, ...,
             -2.22044605e-16,   1.99851949e-03,   2.25240303e-03],
           [  1.32193126e-06,   3.38408436e-07,   1.30493105e-06, ...,
              1.99851949e-03,   0.00000000e+00,   7.59634457e-06],
           [  1.52560258e-05,   4.72809680e-06,   1.51981417e-05, ...,
              2.25240303e-03,   7.59634457e-06,   2.22044605e-16]])
    
  5. giorgiop commented on Nov 9, 2015

    @giorgiop
    Contributor

    Actually, in the case of cosine, constant vectors should be all right, but there is still this issue with floating point arithmetic. This is likely to be fixed in #5333. Thanks for reporting!

  6. asanakoy commented on Oct 24, 2016

    @asanakoy
    Contributor

    We discussed the problem with negative cosine distance in #4475

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions