question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

median is not implemented

See original GitHub issue

autograd.numpy.median() is not implemented.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
duvenaudcommented, May 28, 2020

The gradient will be zero for all elements except for the median element (or the two elements averaged to get the median). Here’s pseudocode:

def median_vjp(a, g):
  N = len(a)
  index_of_median = ((N - 1) / 2) + 0.5
  grad_out = np.zeros_like(a)
  s = numpy.argsort(a)
  if is_odd(N):
    grad_out[s[int(index_of_median)]] = g
  else:
    grad_out[s[int(index_of_median + 0.5)]] = g / 2
    grad_out[s[int(index_of_median - 0.5)]] = g / 2
  return grad_out

If you want to be fancy, you can make it divide the gradient evenly in the case where there’s more than one identical element of a that affects the median.

1reaction
dcasasor-purduecommented, May 28, 2020

@dcasasor-purdue that’s hard to fact check,

>>> x = np.random.randn(100, 100)
>>> l, r = x.sum(axis=0) - x.max(axis=0) - x.min(axis=0), np.median(x,axis=0)
>>> np.mean((l-r)**2)                                                       
100.7067891474971

btw, I saw that TFP implements a percentile function so I guess it’s a matter of spelunking in their source to see how the gradient is defined.

sorry @duvenaud the relu comment didn’t lead to enlightenment for me.

@maedoc you are absolutely right. My particular case is actually an implementation of the discontinuous mid function, which takes only three arguments and selects the middle value. This is intended for piecewise continuous process modeling, rather than for a general implementation of the median function. Thanks for the clarification.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RuntimeError: mean is not implemented for type torch ...
I am getting this error after running my code: "RuntimeError: mean is not implemented for type torch.ByteTensor ...
Read more >
NotImplementedError: mean is not implemented: Categorical ...
I am using TFP 0.8.0 with TF 2. The parameters mean and standard deviation are not usually defined for a categorical distribution and...
Read more >
The median outclasses the mean - Creative Maths
A median is intrinsically understandable. It is the middle number when the values are put in order. End of story. – Well not...
Read more >
Median - Wikipedia
In statistics and probability theory, the median is the value separating the higher half from the lower half of a data sample, a...
Read more >
Median Averaging and Error Analysis - Cross Validated
I am not sure if i should be asking this on the Mathematics of Physics Stack Exchange site. I am taking data for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found