[Tensor]#10 tile & repeat in ndarray and tensor

Clay Ryu's sound lab·2023년 8월 12일
0

Framework

목록 보기
23/48

The functions tile and repeat are used to create a new array or tensor by replicating the elements in an existing array or tensor. Basically only repeat function in ndarray is different from other 3 methods.

each numbers in dimension array for other 3 methods specify the number of repetitions for each axis. if there is no dimension or axis in the number of input array, then it generates new dimension or axis. Therefore, you should care which dimension to add and what elements to repeat.

in Numpy

sample_np = np.arange(10).reshape(2,5)

np.tile(sample_np, (3,2,2))

# --------------output------------------
array([[[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9, 5, 6, 7, 8, 9],
        [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]],

       [[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9, 5, 6, 7, 8, 9],
        [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]],

       [[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9, 5, 6, 7, 8, 9],
        [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]]])

sample_np.repeat(3, axis=0)
# --------------output------------------
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9],
       [5, 6, 7, 8, 9],
       [5, 6, 7, 8, 9]])

Tensor

sample_tensor = torch.arange(0, 10).reshape(2, 5)

torch.tile(sample_tensor, (3,2,2))
# --------------output------------------
tensor([[[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9],
         [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]],

        [[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9],
         [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]],

        [[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9],
         [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]]])
         
sample_tensor.repeat(3, 2, 2)
# --------------output------------------
tensor([[[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9],
         [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]],

        [[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9],
         [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]],

        [[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9],
         [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
         [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]]])
profile
chords & code // harmony with structure

0개의 댓글