PyTorch torch.cross 関数
torch.crossは、PyTorch において2つの3次元ベクトル(またはバッチの3次元ベクトル)の外積を計算するための関数です。外積は2つの入力ベクトルに垂直な新しいベクトルを生成します。
関数定義
torch.cross(input, other, dim=-1)
使用例
実例
import torch
# 2つの3次元ベクトルの外積
a = torch.tensor([1, 0, 0])
b = torch.tensor([0, 1, 0])
c = torch.cross(a, b)
print("a:", a)
print("b:", b)
print("a x b:", c)
# 出力: tensor([0, 0, 1])
# 外積をバッチ計算する
a = torch.tensor([[1, 0, 0], [0, 1, 0]])
b = torch.tensor([[0, 1, 0], [1, 0, 0]])
result = torch.cross(a, b)
print("バッチ外積:")
print(result)
# tensor([[0, 0, 1],
# [0, 0, -1]])
# 次元を指定
a = torch.randn(3, 4, 3)
b = torch.randn(3, 4, 3)
result = torch.cross(a, b, dim=2)
print("指定次元の外積形状:", result.shape)
# 2つの3次元ベクトルの外積
a = torch.tensor([1, 0, 0])
b = torch.tensor([0, 1, 0])
c = torch.cross(a, b)
print("a:", a)
print("b:", b)
print("a x b:", c)
# 出力: tensor([0, 0, 1])
# 外積をバッチ計算する
a = torch.tensor([[1, 0, 0], [0, 1, 0]])
b = torch.tensor([[0, 1, 0], [1, 0, 0]])
result = torch.cross(a, b)
print("バッチ外積:")
print(result)
# tensor([[0, 0, 1],
# [0, 0, -1]])
# 次元を指定
a = torch.randn(3, 4, 3)
b = torch.randn(3, 4, 3)
result = torch.cross(a, b, dim=2)
print("指定次元の外積形状:", result.shape)
その他の拡張
Pytorch torch リファレンスマニュアル