PyTorch torch.broadcast_shapes 関数
torch.broadcast_shapesPyTorch で複数の形状のブロードキャスト結果を計算するための関数です。入力された形状をブロードキャストして得られる形状を返し、実際にテンソルを作成することはありません。
関数定義
torch.broadcast_shapes(*shapes)
使用例
例
import torch
# 形状のブロードキャスト結果を計算
result = torch.broadcast_shapes((3, 1), (1, 4), (3, 4))
print("(3,1) + (1,4) + (3,4) ->", result)
# 出力: (3, 4)
# 複数の形状
result = torch.broadcast_shapes((5,), (1, 5), (3, 1, 5))
print("(5,) + (1,5) + (3,1,5) ->", result)
# 出力: (3, 1, 5)
# 単一の形状
result = torch.broadcast_shapes((2, 3))
print("(2,3) ->", result)
# 出力: (2, 3)
# ブロードキャストできない場合はエラーになります
try:
result = torch.broadcast_shapes((3,), (4,))
except RuntimeError as e:
print("ブロードキャストエラー:", e)
# 形状のブロードキャスト結果を計算
result = torch.broadcast_shapes((3, 1), (1, 4), (3, 4))
print("(3,1) + (1,4) + (3,4) ->", result)
# 出力: (3, 4)
# 複数の形状
result = torch.broadcast_shapes((5,), (1, 5), (3, 1, 5))
print("(5,) + (1,5) + (3,1,5) ->", result)
# 出力: (3, 1, 5)
# 単一の形状
result = torch.broadcast_shapes((2, 3))
print("(2,3) ->", result)
# 出力: (2, 3)
# ブロードキャストできない場合はエラーになります
try:
result = torch.broadcast_shapes((3,), (4,))
except RuntimeError as e:
print("ブロードキャストエラー:", e)
その他の拡張機能
Pytorch torch リファレンスマニュアル