KEMBAR78
kthvalue consistency with sort in the presence of NaN by t-vi · Pull Request #17824 · pytorch/pytorch · GitHub
Skip to content

Conversation

@t-vi
Copy link
Collaborator

@t-vi t-vi commented Mar 8, 2019

This PR causes kthvalue to be consistent with sort
(i.e. treat NaN as larger than any number), so that
a.kthvalue(n) == a.sort()[n - 1].

One drawback is that median with a NaN argument does not return NaN,
which is a deviation from NumPy.

Thank you, @ngimel, for raising this.

This PR causes kthvalue to be consistent with sort
(i.e. treat NaN as larger than any number), so that
a.kthvalue(n) == a.sort()[n - 1].

One drawback is that median with a NaN argument does not return NaN,
which is a deviation from NumPy.

Thank you, @ngimel, for raising this.
@t-vi
Copy link
Collaborator Author

t-vi commented Mar 9, 2019

This will get tests, but I'd thought I'd see if anyone has an opinion whether it's OK to take sort as a model here.

@soumith
Copy link
Member

soumith commented Mar 10, 2019

@t-vi yes taking sort's behavior with nans (placing them as last descending) is reasonable.

@t-vi t-vi added the ready for review (this tag is deprecated) All PRs are ready for review unless they are draft, WIP, or have undismissed requested changes label Mar 10, 2019
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@ezyang is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@ngimel
Copy link
Collaborator

ngimel commented Mar 11, 2019

Currently sort places nans as last ascending (and so would this pr, so topK of an array having at least K nans will be all nans)

n [15]: a                                                                                                                                     
Out[15]: tensor([0.7134, 0.2836, 0.9203,    nan, 0.9634])

In [16]: a.sort()                                                                                                                              
Out[16]: (tensor([0.2836, 0.7134, 0.9203, 0.9634,    nan]), tensor([1, 0, 2, 4, 3]))

In [17]: a.sort(descending=True)                                                                                                               
Out[17]: (tensor([   nan, 0.9634, 0.9203, 0.7134, 0.2836]), tensor([3, 4, 2, 0, 1]))

zdevito pushed a commit to zdevito/ATen that referenced this pull request Mar 12, 2019
Summary:
This PR causes kthvalue to be consistent with sort
(i.e. treat NaN as larger than any number), so that
`a.kthvalue(n) == a.sort()[n - 1]`.

One drawback is that median with a NaN argument does not return NaN,
which is a deviation from NumPy.

Thank you, ngimel, for raising this.
Pull Request resolved: pytorch/pytorch#17824

Differential Revision: D14410092

Pulled By: ezyang

fbshipit-source-id: bdec2d8272dc4c65bcf2f9b8995e237774c44c02
petrex pushed a commit to petrex/pytorch that referenced this pull request Mar 14, 2019
* upstream/master: (87 commits)
  Make Variable::set_data non-const; cosmetic fixes.
  remove warning for upsample code (pytorch#17921)
  Optimize TileOp (pytorch#17290)
  Optimize channel_stats_op (pytorch#16243)
  enable shape inference for elementwise operators (pytorch#17885)
  Remove remaining test jit expects redux (pytorch#17924)
  Handle Scalars Better (pytorch#17875)
  Fixed a formatting issue in doc comments (pytorch#17505)
  Add nbytes, itemsize, element_size to at::Tensor. (pytorch#17810)
  Fix lint in test_distributions.py
  Fix lint in test_jit.py
  Fix lint errors in test_autograd
  Added a few extra python bindings to help with walking the IR graph from Python (pytorch#17822)
  kthvalue consistency with sort in the presence of NaN (pytorch#17824)
  Fix minor grammatical mistakes in torch/nn/modules/loss.py (pytorch#17892)
  Remove (almost all) TensorOptions from native_functions.yaml (pytorch#17385)
  Restore full Windows tests (pytorch#17102)
  Prevent VS2017 from emitting ambiguous symbol errors (second time)
  Fix windows test hang (pytorch#17778)
  torch.btrifact for tensors with greater than 3 dimensions (pytorch#14964)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open source ready for review (this tag is deprecated) All PRs are ready for review unless they are draft, WIP, or have undismissed requested changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants