PyTorch torch.unravel_index 関数
torch.unravel_indexはPyTorchにおいて、フラット化されたインデックスを多次元インデックスに展開するために使用される関数です。torch.ravel_index逆に、フラット化された配列内の位置を多次元配列の座標に変換し直します。
関数定義
torch.unravel_index(indices, shape)
パラメータ説明
indices: フラット化されたインデックスshape: 多次元配列の形状
使用例
実例
import torch
# フラット化されたインデックス
indices = torch.tensor([0, 1, 5, 6, 7])
# 配列の形状
shape = (2, 4)
# 多次元インデックスに展開
result = torch.unravel_index(indices, shape)
print("フラット化されたインデックス:", indices)
print("配列の形状:", shape)
print("多次元インデックス:")
print(result)
# フラット化されたインデックス
indices = torch.tensor([0, 1, 5, 6, 7])
# 配列の形状
shape = (2, 4)
# 多次元インデックスに展開
result = torch.unravel_index(indices, shape)
print("フラット化されたインデックス:", indices)
print("配列の形状:", shape)
print("多次元インデックス:")
print(result)
出力結果は次のとおりです:
展平索引: tensor([0, 1, 5, 6, 7])
数组形状: (2, 4)
多维索引:
tensor([[0, 0, 1, 1, 1],
[0, 1, 1, 0, 1]])
その他の拡張
Pytorch torch リファレンスマニュアル