335 Star 1.5K Fork 862

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
median.md 2.72 KB
一键复制 编辑 原始数据 按行查看 历史
luojianing 提交于 2023-07-21 15:16 . replace target=blank

Function Differences with torch.median

View Source On Gitee

The following mapping relationships can be found in this file.

PyTorch APIs MindSpore APIs
torch.median mindspore.ops.median
torch.Tensor.median mindspore.Tensor.median

torch.median

torch.median(input, dim=-1, keepdim=False, *, out=None) -> Tensor

For more information, see torch.median.

mindspore.ops.median

mindspore.ops.median(input, axis=-1, keepdims=False) -> Tensor

For more information, see mindspore.ops.median.

Differences

PyTorch: Output the median and index of input according to the specified dim. keepdim controls whether the output and input have the same dimension. Return the median of all elements when the input has only input, or the median and index of the specified dimension when the input contains dim. out can get the output.

MindSpore: Output the median and index of input according to the specified axis. The keepdims function is identical to PyTorch. Unlike Pytorch, MindSpore returns the median and index in the specified dimension, regardless of whether the input contains axis or not. MindSpore does not have out parameter.

Categories Subcategories PyTorch MindSpore Difference
Parameters Parameter 1 input input Consistent
Parameter 2 dim axis Same function, different parameter names
Parameter 3 keepdim keepdims Same function, different parameter names
Parameter 4 out - PyTorch's out can get the output. MindSpore does not have this parameter

Code Example

# PyTorch
import torch

input = torch.tensor([[1, 2.5, 3, 1], [2.5, 3, 2, 1]], dtype=torch.float32)
print(torch.median(input))
# tensor(2.)
print(torch.median(input, dim=1, keepdim=True))
# torch.return_types.median(
# values=tensor([[1.],
#         [2.]]),
# indices=tensor([[3],
#         [2]]))

# MindSpore
import mindspore

x = mindspore.Tensor([[1, 2.5, 3, 1], [2.5, 3, 2, 1]], dtype=mindspore.float32)
print(mindspore.ops.median(x, axis=1, keepdims=True))
# (Tensor(shape=[2, 1], dtype=Float32, value=
# [[ 1.00000000e+00],
#  [ 2.00000000e+00]]), Tensor(shape=[2, 1], dtype=Int64, value=
# [[3],
#  [2]]))
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891