335 Star 1.5K Fork 865

MindSpore / docs

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

Function Differences with torch.meshgrid

View Source On Gitee

torch.meshgrid

torch.meshgrid(
    *tensors)

For more information, see torch.meshgrid.

mindspore.ops.meshgrid

mindspore.ops.meshgrid(*inputs, indexing='xy')

For more information, see mindspore.ops.meshgrid.

Differences

PyTorch: Generate the grid matrix from the given tensors. If it is a list of scalars, the scalar will automatically be considered as a tensor of size (1,).

MindSpore: MindSpore API basically implements the same function as TensorFlow. The inputs parameter is only supported for Tensor, not for scalar.

Categories Subcategories PyTorch MindSpore Difference
Parameters Parameter 1 tensors inputs Same function
Parameter 2 - indexing torch.meshgrid v1.8.1 has no parameter indexing, but has the same function as mindspore.ops.meshgrid when indexing parameter is set to 'ij'. From v1.10, torch.meshgrid supports indexing parameter.

Code Example 1

# PyTorch
import numpy as np
import torch

x = torch.tensor(np.array([1, 2, 3, 4]).astype(np.int32))
y = torch.tensor(np.array([5, 6, 7]).astype(np.int32))
z = torch.tensor(np.array([8, 9, 0, 1, 2]).astype(np.int32))
output = torch.meshgrid(x, y, z)
print(output)
# (tensor([[[1, 1, 1, 1, 1],
#          [1, 1, 1, 1, 1],
#          [1, 1, 1, 1, 1]],
#         [[2, 2, 2, 2, 2],
#          [2, 2, 2, 2, 2],
#          [2, 2, 2, 2, 2]],
#         [[3, 3, 3, 3, 3],
#          [3, 3, 3, 3, 3],
#          [3, 3, 3, 3, 3]],
#         [[4, 4, 4, 4, 4],
#          [4, 4, 4, 4, 4],
#          [4, 4, 4, 4, 4]]], dtype=torch.int32), tensor([[[5, 5, 5, 5, 5],
#          [6, 6, 6, 6, 6],
#          [7, 7, 7, 7, 7]],
#         [[5, 5, 5, 5, 5],
#          [6, 6, 6, 6, 6],
#          [7, 7, 7, 7, 7]],
#         [[5, 5, 5, 5, 5],
#          [6, 6, 6, 6, 6],
#          [7, 7, 7, 7, 7]],
#         [[5, 5, 5, 5, 5],
#          [6, 6, 6, 6, 6],
#          [7, 7, 7, 7, 7]]], dtype=torch.int32), tensor([[[8, 9, 0, 1, 2],
#          [8, 9, 0, 1, 2],
#          [8, 9, 0, 1, 2]],
#         [[8, 9, 0, 1, 2],
#          [8, 9, 0, 1, 2],
#          [8, 9, 0, 1, 2]],
#         [[8, 9, 0, 1, 2],
#          [8, 9, 0, 1, 2],
#          [8, 9, 0, 1, 2]],
#         [[8, 9, 0, 1, 2],
#          [8, 9, 0, 1, 2],
#          [8, 9, 0, 1, 2]]], dtype=torch.int32))


# MindSpore
import mindspore
import numpy as np
from mindspore import Tensor

x = Tensor(np.array([1, 2, 3, 4]).astype(np.int32))
y = Tensor(np.array([5, 6, 7]).astype(np.int32))
z = Tensor(np.array([8, 9, 0, 1, 2]).astype(np.int32))
output = mindspore.ops.meshgrid(x, y, z, indexing='ij')
print(output)
# (Tensor(shape=[4, 3, 5], dtype=Int32, value=
#     [[[1, 1, 1, 1, 1],
#       [1, 1, 1, 1, 1],
#       [1, 1, 1, 1, 1]],
#      [[2, 2, 2, 2, 2],
#       [2, 2, 2, 2, 2],
#       [2, 2, 2, 2, 2]],
#      [[3, 3, 3, 3, 3],
#       [3, 3, 3, 3, 3],
#       [3, 3, 3, 3, 3]],
#      [[4, 4, 4, 4, 4],
#       [4, 4, 4, 4, 4],
#       [4, 4, 4, 4, 4]]]), Tensor(shape=[4, 3, 5], dtype=Int32, value=
#     [[[5, 5, 5, 5, 5],
#       [6, 6, 6, 6, 6],
#       [7, 7, 7, 7, 7]],
#      [[5, 5, 5, 5, 5],
#       [6, 6, 6, 6, 6],
#       [7, 7, 7, 7, 7]],
#      [[5, 5, 5, 5, 5],
#       [6, 6, 6, 6, 6],
#       [7, 7, 7, 7, 7]],
#      [[5, 5, 5, 5, 5],
#       [6, 6, 6, 6, 6],
#       [7, 7, 7, 7, 7]]]), Tensor(shape=[4, 3, 5], dtype=Int32, value=
#     [[[8, 9, 0, 1, 2],
#       [8, 9, 0, 1, 2],
#       [8, 9, 0, 1, 2]],
#      [[8, 9, 0, 1, 2],
#       [8, 9, 0, 1, 2],
#       [8, 9, 0, 1, 2]],
#      [[8, 9, 0, 1, 2],
#       [8, 9, 0, 1, 2],
#       [8, 9, 0, 1, 2]],
#      [[8, 9, 0, 1, 2],
#       [8, 9, 0, 1, 2],
#       [8, 9, 0, 1, 2]]]))
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891