Pytorch median - 是 bug 还是我用错了

Pytorch median - is it bug or am I using it wrong

我正在尝试获取 2D 的每一行的中位数 torch.tensor。但是与使用标准数组或 numpy

相比,结果并不是我所期望的
import torch
import numpy as np
from statistics import median

print(torch.__version__)
>>> 0.4.1

y = [[1, 2, 3, 5, 9, 1],[1, 2, 3, 5, 9, 1]]
median(y[0])
>>> 2.5

np.median(y,axis=1)
>>> array([2.5, 2.5])

yt = torch.tensor(y,dtype=torch.float32)
yt.median(1)[0]
>>> tensor([2., 2.])

看起来这是本期提到的 Torch 的预期行为

https://github.com/pytorch/pytorch/issues/1837
https://github.com/torch/torch7/pull/182

上面link中提到的推理

Median returns 'middle' element in case of odd-many elements, otherwise one-before-middle element (could also do the other convention to take mean of the two around-the-middle elements, but that would be twice more expensive, so I decided for this one).

您可以使用 pytorch 模拟 numpy 中位数:

import torch
import numpy as np
y =[1, 2, 3, 5, 9, 1]
print("numpy=",np.median(y))
print(sorted([1, 2, 3, 5, 9, 1]))
yt = torch.tensor(y,dtype=torch.float32)
ymax = torch.tensor([yt.max()])
print("torch=",yt.median())
print("torch_fixed=",(torch.cat((yt,ymax)).median()+yt.median())/2.)