[부스트캠프 AI Tech 5기] Pytorch Operation

박상우·2023년 3월 13일
0

부스트캠프

목록 보기
7/54
post-thumbnail

Pytorch

Pytorch Operation

사칙연산

+ : torch.add()
- : torch.sub()
* : torch.mul()
/ : torch.div()

Indexing

x = torch.randn(3, 4)

x
>> tensor([[ 0.1392, -0.4403, -0.1479, -1.8080],
        [ 0.0270,  0.2358, -0.5572, -0.9726],
        [-0.0528,  0.0768, -1.2052, -1.3905]])
        
indices = torch.tensor([1, 2])

torch.index_select(x, 0, indices)
>> tensor([[ 0.0270,  0.2358, -0.5572, -0.9726],
        [-0.0528,  0.0768, -1.2052, -1.3905]])

indices = torch.tensor([2,])

torch.index_select(x, 0, indices)
>> tensor([[-0.0528,  0.0768, -1.2052, -1.3905]])

indices = torch.tensor([0, 1, 3])

torch.index_select(x, 1, indices)
>> tensor([[ 0.1392, -0.4403, -1.8080],
        [ 0.0270,  0.2358, -0.9726],

Gather

torch.gather(input, dim,index) 

  • 너무 어려워서 모르겠음
  • 같이 공부할 예정..

from_numpy

a = numpy.array([1, 2, 3])
t = torch.from_numpy(a)
t
t[0] = -1
a
array([-1,  2,  3])
  • 본래 어레이랑 메모리 주소 공유

chunk

t = torch.tensor([[1, 2, 3],
                  [4, 5, 6]])

print(torch.chunk(t, 2, 0))
print(torch.chunk(t, 2, 1))

(tensor([[1, 2, 3]]), tensor([[4, 5, 6]]))
(tensor([[1, 2],
        [4, 5]]), tensor([[3],
        [6]]))
  • 텐서 분리

swapdims

x = torch.tensor([[[0,1],[2,3]],[[4,5],[6,7]]])
torch.swapdims(x, 0, 1)
torch.swapdims(x, 0, 2)
  • 전치

scatter

  • gather랑 유사 (헷갈림)

Random Sampling

  • 초기화, 샘플링 관련 함수

randn

torch.randn(4)
torch.randn(2, 3)

randperm

torch.randperm(4)
  • Returns a random permutation of integers from 0 to n - 1

Pointwise Ops

  • pointwise로 연산 처리

log1p

a = torch.randn(5)
torch.log1p(a)

rad2deg

a = torch.tensor([[3.142, -3.142], [6.283, -6.283], [1.570, -1.570]])
torch.rad2deg(a)
  • Input radian을 degree로

Reduction Ops

  • tensor에서 특정 값만 가져오거나 연산을 통해 축소

prod

a = torch.randn(1, 3)
a
tensor([[-0.8020,  0.5428, -1.5854]])
torch.prod(a)
tensor(0.6902)
  • tensor 내부 값을 모두 product

count_nonzero

torch.count_nonzero(x)
torch.count_nonzero(x, dim=0)
  • 0의 개수를 텐서로 반환

argmax

a = torch.randn(4, 4)
a
tensor([[ 1.3398,  0.2663, -0.2686,  0.2450],
        [-0.7401, -0.8805, -0.3402, -1.1936],
        [ 0.4907, -1.3948, -1.0691, -0.3132],
        [-1.6092,  0.5419, -0.2993,  0.3195]])
torch.argmax(a, dim=1)
tensor([ 0,  2,  0,  1])
  • 가장 큰 값의 index 반환

Comparison Ops

  • 비교 관련 ops

argsort

a = torch.randn(4, 4)
a
tensor([[ 0.0785,  1.5267, -0.8521,  0.4065],
        [ 0.1598,  0.0788, -0.0745, -1.2700],
        [ 1.2208,  1.0722, -0.7064,  1.2564],
        [ 0.0669, -0.2318, -0.8229, -0.9280]])


torch.argsort(a, dim=1)
tensor([[2, 0, 3, 1],
        [3, 2, 1, 0],
        [2, 1, 0, 3],
        [3, 2, 1, 0]])
  • sorting 한 인덱스를 반환

Other Ops

triu

a = torch.randn(3, 3)
a
tensor([[ 0.2309,  0.5207,  2.0049],
        [ 0.2072, -1.0680,  0.6602],
        [ 0.3480, -0.5211, -0.4573]])
torch.triu(a)
tensor([[ 0.2309,  0.5207,  2.0049],
        [ 0.0000, -1.0680,  0.6602],
        [ 0.0000,  0.0000, -0.4573]])
torch.triu(a, diagonal=1)
tensor([[ 0.0000,  0.5207,  2.0049],
        [ 0.0000,  0.0000,  0.6602],
        [ 0.0000,  0.0000,  0.0000]])
torch.triu(a, diagonal=-1)
tensor([[ 0.2309,  0.5207,  2.0049],
        [ 0.2072, -1.0680,  0.6602],
        [ 0.0000, -0.5211, -0.4573]])
  • 대각행렬 등 여러가지 ops

einsum

  • 좀 어려워서.. 천천히 공부
profile
세상아 덤벼라

0개의 댓글