|
|
|
@ -583,66 +583,69 @@ def t(input, name=None):
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cross(input, other, dim=None):
|
|
|
|
|
def cross(x, y, axis=None, name=None):
|
|
|
|
|
"""
|
|
|
|
|
:alias_main: paddle.cross
|
|
|
|
|
:alias: paddle.cross,paddle.tensor.cross,paddle.tensor.linalg.cross
|
|
|
|
|
|
|
|
|
|
Returns the cross product of vectors in dimension `dim` of the `input` and `other` tensor.
|
|
|
|
|
Inputs must have the same shape, and the size of their dim-th dimension should be equla to 3.
|
|
|
|
|
If `dim` is not given, it defaults to the first dimension found with the size 3.
|
|
|
|
|
Computes the cross product between two tensors along an axis.
|
|
|
|
|
Inputs must have the same shape, and the length of their axes should be equal to 3.
|
|
|
|
|
If `axis` is not given, it defaults to the first axis found with the length 3.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
input (Variable): The first input tensor variable.
|
|
|
|
|
other (Variable): The second input tensor variable.
|
|
|
|
|
dim (int): The dimension to take the cross-product in.
|
|
|
|
|
x (Variable): The first input tensor variable.
|
|
|
|
|
y (Variable): The second input tensor variable.
|
|
|
|
|
axis (int, optional): The axis along which to compute the cross product. It defaults to the first axis found with the length 3.
|
|
|
|
|
name (str, optional): The default value is None. Normally there is no need for
|
|
|
|
|
user to set this property. For more information, please refer to :ref:`api_guide_Name`
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Variable: A Tensor with same data type as `input`.
|
|
|
|
|
Variable: A Tensor with same data type as `x`.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
import paddle
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
from paddle.imperative import to_variable
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
paddle.enable_imperative()
|
|
|
|
|
|
|
|
|
|
data_x = np.array([[1.0, 1.0, 1.0],
|
|
|
|
|
[2.0, 2.0, 2.0],
|
|
|
|
|
[3.0, 3.0, 3.0]])
|
|
|
|
|
data_y = np.array([[1.0, 1.0, 1.0],
|
|
|
|
|
[1.0, 1.0, 1.0],
|
|
|
|
|
[1.0, 1.0, 1.0]])
|
|
|
|
|
x = to_variable(data_x)
|
|
|
|
|
y = to_variable(data_y)
|
|
|
|
|
|
|
|
|
|
with fluid.dygraph.guard():
|
|
|
|
|
x = fluid.dygraph.to_variable(data_x)
|
|
|
|
|
y = fluid.dygraph.to_variable(data_y)
|
|
|
|
|
out_z1 = paddle.cross(x, y)
|
|
|
|
|
print(out_z1.numpy())
|
|
|
|
|
z1 = paddle.cross(x, y)
|
|
|
|
|
print(z1.numpy())
|
|
|
|
|
# [[-1. -1. -1.]
|
|
|
|
|
# [ 2. 2. 2.]
|
|
|
|
|
# [-1. -1. -1.]]
|
|
|
|
|
out_z2 = paddle.cross(x, y, dim=1)
|
|
|
|
|
print(out_z2.numpy())
|
|
|
|
|
|
|
|
|
|
z2 = paddle.cross(x, y, axis=1)
|
|
|
|
|
print(z2.numpy())
|
|
|
|
|
# [[0. 0. 0.]
|
|
|
|
|
# [0. 0. 0.]
|
|
|
|
|
# [0. 0. 0.]]
|
|
|
|
|
"""
|
|
|
|
|
helper = LayerHelper("cross", **locals())
|
|
|
|
|
if in_dygraph_mode():
|
|
|
|
|
if dim:
|
|
|
|
|
return core.ops.cross(input, other, 'dim', dim)
|
|
|
|
|
if axis:
|
|
|
|
|
return core.ops.cross(x, y, 'dim', axis)
|
|
|
|
|
else:
|
|
|
|
|
return core.ops.cross(input, other)
|
|
|
|
|
return core.ops.cross(x, y)
|
|
|
|
|
|
|
|
|
|
out = helper.create_variable_for_type_inference(input.dtype)
|
|
|
|
|
helper = LayerHelper("cross", **locals())
|
|
|
|
|
out = helper.create_variable_for_type_inference(x.dtype)
|
|
|
|
|
attrs = dict()
|
|
|
|
|
if dim:
|
|
|
|
|
attrs['dim'] = dim
|
|
|
|
|
attrs['dim'] = axis
|
|
|
|
|
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type='cross',
|
|
|
|
|
inputs={'X': input,
|
|
|
|
|
'Y': other},
|
|
|
|
|
inputs={'X': x,
|
|
|
|
|
'Y': y},
|
|
|
|
|
outputs={'Out': out},
|
|
|
|
|
attrs=attrs)
|
|
|
|
|
return out
|
|
|
|
|