Pytorch 中位数 - 是错误还是我使用错误 [英] Pytorch median - is it bug or am I using it wrong

查看:125
本文介绍了Pytorch 中位数 - 是错误还是我使用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取每行 2D torch.tensor 的中位数.但是与使用标准数组或 numpy 相比,结果并不是我所期望的

I am trying to get median of each row of 2D torch.tensor. But the result is not what I expect when compared to working with standard array or 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 的预期行为

Looks like this is the intended behaviour of Torch as mentioned in this issue

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

上面链接中提到的推理

中位数在元素为奇数的情况下返回中间"元素,否则中间元素前一个(也可以执行其他约定以取两个围绕中间的元素的平均值,但这会多两倍很贵,所以我决定买这个).

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 中位数 - 是错误还是我使用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆