335 Star 1.5K Fork 862

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mindspore.numpy.cross.md 2.37 KB
一键复制 编辑 原始数据 按行查看 历史
宦晓玲 提交于 2023-02-07 09:20 . delete the spaces in code block in 2.0

Function Differences with torch.cross

torch.cross

class torch.cross(
    input,
    other,
    dim=-1,
    out=None
)

For more information, see torch.cross.

mindspore.numpy.cross

class mindspore.numpy.cross(
    a,
    b,
    axisa=-1,
    axisb=-1,
    axisc=-1,
    axis=None
)

For more information, see mindspore.numpy.cross.

Differences

PyTorch: Returns the cross product of vectors in dimension dim of input and other. The inputs must have the same size, and the size of their dim dimension should be 3. If dim is not given, it defaults to the first dimension found with the size 3.

MindSpore: If a and b are arrays of vectors, the vectors are defined by the last axis of a and b by default, and these axes can have dimensions 2 or 3. Where the dimension of either a or b is 2, the third component of the input vector is assumed to be zero and the cross product calculated accordingly. In cases where both input vectors have dimension 2, the z-component of the cross product is returned.

Code Example

import mindspore.numpy as np
import torch

# MindSpore
x = np.array([[1,2,3], [4,5,6]])
y = np.array([[4,5,6], [1,2,3]])
output = np.cross(x, y)
print(output)
# [[-3  6 -3]
# [ 3 -6  3]]
output = np.cross(x, y, axisc=0)
print(output)
# [[-3  3]
# [ 6 -6]
# [-3  3]]
x = np.array([[1,2], [4,5]])
y = np.array([[4,5], [1,2]])
print(np.cross(x, y))
# Tensor(shape=[2], dtype=Int32, value= [-3,  3])

# PyTorch
a = torch.tensor([[1,2,3], [4,5,6]], dtype=torch.int8)
b = torch.tensor([[4,5,6], [1,2,3]], dtype=torch.int8)
print(torch.cross(a, b, dim=1))
# tensor([[-3,  6, -3],
#         [ 3, -6,  3]], dtype=torch.int8)
print(torch.cross(a, b))
# tensor([[-3,  6, -3],
#         [ 3, -6,  3]], dtype=torch.int8)
a = torch.tensor([[1,2], [4,5]], dtype=torch.int8)
b = torch.tensor([[4,5], [1,2]], dtype=torch.int8)
print(torch.cross(a, b))
# RuntimeError: no dimension of size 3 in input
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0.0-alpha

搜索帮助

53164aa7 5694891 3bd8fe86 5694891