Skip to content

Conversation

@heitorschueroff
Copy link
Contributor

@heitorschueroff heitorschueroff commented Sep 11, 2020

Stack from ghstack:

Add a note that torch.median returns the smaller of the two middle elements for even-sized input and refer user to torch.quantile for the mean of the middle values.

fixes #39520

Differential Revision: D23657208

heitorschueroff added a commit that referenced this pull request Sep 11, 2020
Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

Thanks!

@codecov
Copy link

codecov bot commented Sep 11, 2020

Codecov Report

Merging #44562 into gh/heitorschueroff/13/base will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@                     Coverage Diff                     @@
##           gh/heitorschueroff/13/base   #44562   +/-   ##
===========================================================
  Coverage                       68.00%   68.00%           
===========================================================
  Files                             384      384           
  Lines                           49599    49599           
===========================================================
  Hits                            33728    33728           
  Misses                          15871    15871           
Impacted Files Coverage Δ
torch/_torch_docs.py 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c68a99b...acc4530. Read the comment docs.

:func:`torch.quantile` with ``q=0.5`` instead.
.. warning::
This function produces deterministic (sub)gradients unlike ``median(dim=0)``
Copy link
Collaborator

Choose a reason for hiding this comment

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

Aside: it's very surprising to me that this version of median produces deterministic gradients but the other version, which actually computes indices, doesn't. We should probably fix "median_with_indices" to consistently return the FIRST valid median, just like we fixed argmin and argmax.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think, as you mention, that the other function returns non-deterministic indices and thus non-deterministic subgradients.
This one evenly distribute the gradient to all the inputs with the value used. So it always is deterministic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason it returns non-deterministic indices is because it uses quickselect to partition which is not stable. One way to solve it is to use 3-way quickselect that partitions input into <, = and >. Then choose the smallest index from the = segment.

Add a note that torch.median returns the smaller of the two middle elements for even-sized input and refer user to torch.quantile for the mean of the middle values.

fixes #39520

Differential Revision: [D23657208](https://our.internmc.facebook.com/intern/diff/D23657208)

[ghstack-poisoned]
heitorschueroff added a commit that referenced this pull request Sep 14, 2020
@facebook-github-bot
Copy link
Contributor

@heitorschueroff merged this pull request in 199435a.

@facebook-github-bot facebook-github-bot deleted the gh/heitorschueroff/13/head branch September 18, 2020 14:17
xuzhao9 pushed a commit that referenced this pull request Sep 18, 2020
Summary:
Pull Request resolved: #44562

Add a note that torch.median returns the smaller of the two middle elements for even-sized input and refer user to torch.quantile for the mean of the middle values.

fixes #39520

Test Plan: Imported from OSS

Reviewed By: albanD

Differential Revision: D23657208

Pulled By: heitorschueroff

fbshipit-source-id: 2747aa652d1e7f10229d9299b089295aeae092c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torch.median returns the smaller element when the median value lies between two elements.

5 participants