PyTorch torch.broadcast_shapes 関数


Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

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)

Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

その他の拡張機能