YouTip LogoYouTip

Pytorch Torch Cross

PyTorch torch.cross Function

\\n\\n

torch.cross is a function in PyTorch used to calculate the cross product of two 3-dimensional vectors (or batches of 3-dimensional vectors). The cross product produces a new vector perpendicular to both input vectors.

\\n\\n

Function Definition

\\n\\n

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

\\n\\n
\\n\\n

Usage Examples

\\n\\n

Example

\\n\\n
import torch\\n\\n# Cross Product of Two 3D Vectors\\na = torch.tensor([1,0,0])\\nb = torch.tensor([0,1,0])\\nc = torch.cross(a, b)\\n\\nprint("a:", a)\\nprint("b:", b)\\nprint("a x b:", c)\\n# Output: tensor([0, 0, 1])\\n\\n# Batch Cross Product Computation\\na = torch.tensor([[1,0,0],[0,1,0]])\\nb = torch.tensor([[0,1,0],[1,0,0]])\\nresult = torch.cross(a, b)\\n\\nprint("Batch Cross Product:")\\nprint(result)\\n# tensor([[0, 0, 1],\\n#  [0, 0, -1]])\\n\\n# Specify Dimension\\na = torch.randn(3,4,3)\\nb = torch.randn(3,4,3)\\nresult = torch.cross(a, b, dim=2)\\n\\nprint("Specify DimensionCross Product Shape:", result.shape)\\n
← Pytorch Torch CudaPytorch Torch Count_Nonzero β†’