[Tensor]#6 where, argwhere, nonzero

Clay Ryu's sound lab·2023년 7월 14일
0

Framework

목록 보기
16/48

So I decided to choose using argwhere to avoid confusions of using other methods. They are quite similar for actual purpose.

Numpy

arr = np.array([1, 2, 3, 4, 5])
indices_w = np.where(arr > 2)
indices_aw = np.argwhere(arr > 2)
indices_nz = np.nonzero(arr > 2)

indices_w, indices_aw, indices_nz
# output
((array([2, 3, 4]),),
 array([[2],
        [3],
        [4]]),
 (array([2, 3, 4]),))

Torch

import torch
arr = torch.tensor([1, 2, 3, 4, 5])
indices_w = torch.where(arr > 2)
indices_aw = torch.argwhere(arr > 2)
indices_nz = torch.nonzero(arr > 2)

indices_w, indices_aw, indices_nz
# outputs
((tensor([2, 3, 4]),),
 tensor([[2],
         [3],
         [4]]),
 tensor([[2],
         [3],
         [4]]))
profile
chords & code // harmony with structure

0개의 댓글