λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

Deep Learning/PyTorch

ν…μ„œ(Tensor) μ‚¬μš©λ²•

νŒŒμ΄ν† μΉ˜μ˜ ν…μ„œ(Tensor)의 μ‚¬μš©λ²•μ— λŒ€ν•΄ μ•Œμ•„λ³΄μž. λ‹€λ£° λ‚΄μš©μ€ λ‹€μŒκ³Ό κ°™λ‹€.

 

1. ν…μ„œμ˜ 생성

2. ν…μ„œμ˜ μ—°μ‚°

3. ν…μ„œμ˜ λ³€ν™˜

 

1. ν…μ„œμ˜ 생성

 

In:

import torch

x = torch.rand(5, 3)

print(x)

 

Out:

tensor([[0.1501, 0.8814, 0.4848],
        [0.0723, 0.9468, 0.1327],
        [0.8581, 0.8050, 0.4441],
        [0.4888, 0.0157, 0.6959],
        [0.9666, 0.4729, 0.1983]])

 

β–· torch.rand()λ₯Ό μ΄μš©ν•˜μ—¬ 0κ³Ό 1 μ‚¬μ΄μ˜ μž„μ˜μ˜ μˆ˜κ°€ μ›μ†ŒμΈ 5×3 행렬이 λ§Œλ“€μ—ˆλ‹€. ν•¨μˆ˜ μ•ˆμ˜ 두 μΈμžλŠ” ν–‰κ³Ό 열을 λ‚˜νƒ€λ‚Έλ‹€.

 

In:

x = torch.rand(5, 3, 3)

print(x)

 

Out:

tensor([[[0.7756, 0.1651, 0.4810],
         [0.9195, 0.5424, 0.2873],
         [0.0130, 0.7445, 0.4326]],

        [[0.9263, 0.4488, 0.4638],
         [0.0491, 0.8813, 0.4010],
         [0.8378, 0.0515, 0.9314]],

        [[0.7421, 0.1748, 0.0500],
         [0.5526, 0.0150, 0.7348],
         [0.1633, 0.2840, 0.8414]],

        [[0.4233, 0.7684, 0.8441],
         [0.8695, 0.2692, 0.4962],
         [0.3590, 0.5625, 0.0550]],

        [[0.5758, 0.0916, 0.9793],
         [0.7746, 0.2130, 0.7856],
         [0.7545, 0.9474, 0.2108]]])

 

β–· torch.rand()에 μ„Έ 개 μ΄μƒμ˜ 인자λ₯Ό λ„£μ–΄ 닀차원 ν…μ„œλ₯Ό λ§Œλ“€ 수 μžˆλ‹€.

 

In:

x = torch.zeros(5, 3, dtype = torch.long)

print(x)

 

Out:

tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])

 

β–· torch.zeros()λ₯Ό μ΄μš©ν•˜μ—¬ λͺ¨λ“  μ›μ†Œκ°€ 0이며, dtype을 torch.long으둜 μ„€μ •ν•˜μ—¬ 데이터 νƒ€μž…μ΄ long인  5×3 행렬이 λ§Œλ“€μ—ˆλ‹€.

 

직접 μ›μ†Œλ₯Ό μž…λ ₯ν•˜μ—¬ ν…μ„œλ₯Ό μƒμ„±ν•΄λ³΄μž.

 

In:

x = torch.tensor([5.5, 3])

print(x)

 

Out:

tensor([5.5000, 3.0000])

 

β–· torch.tensor()에 μ›μ†Œλ‘œ κ΅¬μ„±λœ 리슀트λ₯Ό 인자둜 μ£Όμ–΄ ν…μ„œλ₯Ό 생성할 수 μžˆλ‹€.

 

In:

x = x.new_ones(5, 3, dtype = torch.double)

print(x)

 

Out:

tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)

 

β–· x에 torch.new_ones()λ₯Ό μ΄μš©ν•˜μ—¬ 데이터 νƒ€μž…μ΄ double이고 5×3 ν–‰λ ¬λ‘œ λ°”κΎΈμ—ˆλ‹€. κΈ°μ‘΄ ν…μ„œλ₯Ό μ΄μš©ν•˜μ—¬ ν…μ„œμ˜ 정보λ₯Ό λ³€κ²½ν•  수 μžˆλ‹€.

 

2. ν…μ„œμ˜ μ—°μ‚°

 

In:

x = torch.tensor([[1, 2], [3, 4]])
y = torch.tensor([[5, 6], [7, 8]])

print(x)
print(y)
print(x + y)

 

Out:

tensor([[1, 2],
        [3, 4]])
tensor([[5, 6],
        [7, 8]])
tensor([[ 6,  8],
        [10, 12]])

 

β–· +λ₯Ό μ΄μš©ν•˜μ—¬ 두 ν…μ„œμ˜ λ§μ…ˆμ΄ κ°€λŠ₯ν•˜λ‹€.

 

In:

print(torch.add(x, y))

 

Out:

tensor([[ 6,  8],
        [10, 12]])

 

β–· torch.add()λ₯Ό μ΄μš©ν•˜μ—¬ 인자둜 받은 두 ν…μ„œμ˜ λ§μ…ˆμ΄ κ°€λŠ₯ν•˜λ‹€.

 

In:

print(x)

x.add_(y)

print(x)

 

Out:

tensor([[1, 2],
        [3, 4]])
tensor([[ 6,  8],
        [10, 12]])

 

β–· torch.add_()λ₯Ό μ΄μš©ν•˜μ—¬ 두 ν…μ„œμ˜ λ§μ…ˆμ΄ κ°€λŠ₯ν•˜λ‹€. μ΄λ•Œ, 연산을 μˆ˜ν–‰ν•˜λŠ” ν…μ„œλŠ” λ§μ…ˆμ΄ μˆ˜ν–‰λœ 결과둜 바뀐닀.

 

β–Ά 끝에 _κ°€ 뢙은 ν•¨μˆ˜λ₯Ό μ΄μš©ν•  경우, 이λ₯Ό μˆ˜ν–‰ν•˜λŠ” ν…μ„œκ°€ μ—°μ‚°μ˜ 결과둜 바뀐닀.

예) x.copy_(y), x.t_()

 

In:

print(x[:, 1])

 

Out:

tensor([ 8, 12])

 

β–· λ„˜νŒŒμ΄(NumPy)처럼 인덱슀λ₯Ό μ΄μš©ν•˜μ—¬ λΆ€λΆ„ 선택이 κ°€λŠ₯ν•˜λ‹€.

 

In:

print(x.size())

 

Out:

torch.Size([5, 3])

 

β–· torch.size()λ₯Ό μ΄μš©ν•˜μ—¬ ν…μ„œμ˜ 크기λ₯Ό 확인할 수 μžˆλ‹€. μ—°μ‚° κ²°κ³ΌλŠ” νŠœν”Œκ³Ό κ΄€λ ¨λœ 연산을 μ μš©ν•  수 μžˆλ‹€.

 

In:

x = torch.randn(4, 4)

print(x.view(1, 16))
print(x.view(4, 4))
print(x.view(8, 2))

print(x.view(-1, 8))

 

Out:

tensor([[-1.0937, -0.0562, -0.1371, -1.5621,  1.2284, -0.3012,  0.9080,  2.5218,
         -1.1621, -1.6657, -1.1432,  1.5718, -2.8027, -0.4603,  0.0681,  0.4639]])
tensor([[-1.0937, -0.0562, -0.1371, -1.5621],
        [ 1.2284, -0.3012,  0.9080,  2.5218],
        [-1.1621, -1.6657, -1.1432,  1.5718],
        [-2.8027, -0.4603,  0.0681,  0.4639]])
tensor([[-1.0937, -0.0562],
        [-0.1371, -1.5621],
        [ 1.2284, -0.3012],
        [ 0.9080,  2.5218],
        [-1.1621, -1.6657],
        [-1.1432,  1.5718],
        [-2.8027, -0.4603],
        [ 0.0681,  0.4639]])
tensor([[-1.0937, -0.0562, -0.1371, -1.5621,  1.2284, -0.3012,  0.9080,  2.5218],
        [-1.1621, -1.6657, -1.1432,  1.5718, -2.8027, -0.4603,  0.0681,  0.4639]])

 

β–· torch.view()λ₯Ό μ΄μš©ν•˜λ©΄ ν…μ„œμ˜ ν˜•νƒœ(Shape)λ₯Ό λ°”κΏ€ 수 μžˆλ‹€. μ„Έ λ²ˆμ§ΈκΉŒμ§€μ˜ 좜λ ₯값은 이λ₯Ό 톡해 ν˜•νƒœκ°€ λ³€κ²½λœ ν…μ„œμ˜ λͺ¨μŠ΅μ΄λ‹€. torch.view()의 인자둜 -1이 λ“€μ–΄κ°„ 경우, λ‹€λ₯Έ 인자λ₯Ό κ³ λ €ν•˜μ—¬ μžλ™μœΌλ‘œ ν…μ„œμ˜ 크기가 λ³€κ²½λœλ‹€. λ”°λΌμ„œ λ§ˆμ§€λ§‰ μ½”λ“œμ˜ κ²°κ³ΌλŠ” μ—΄μ˜ 크기가 8둜 μ •ν•΄μ‘ŒκΈ° λ•Œλ¬Έμ— μžλ™μœΌλ‘œ ν–‰μ˜ 크기가 2둜 정해진닀.

 

In:

x = torch.rand(1)

print(x)
print(x.item())

 

Out:

tensor([-0.7020])
-0.7020143270492554

 

β–· ν…μ„œμ— ν•˜λ‚˜μ˜ μ›μ†Œλ§Œ μžˆλ‹€λ©΄, torch.item()을 μ΄μš©ν•˜μ—¬ 슀칼라(Scalar) κ°’μœΌλ‘œ λ³€κ²½ν•  수 μžˆλ‹€.

 

μ΄μ™Έμ˜ ν…μ„œλ₯Ό μ΄μš©ν•œ λ‹€μ–‘ν•œ 연산에 λŒ€ν•œ μ •λ³΄λŠ” μ—¬κΈ°(https://pytorch.org/docs/stable/torch.html)μ—μ„œ 확인할 수 μžˆλ‹€.

 

3. ν…μ„œμ˜ λ³€ν™˜

 

In:

x = torch.ones(5)
y = x.numpy()

print(x)
print(type(x))
print(y)
print(type(y))

 

Out:

tensor([1., 1., 1., 1., 1.])
<class 'torch.Tensor'>
[1. 1. 1. 1. 1.]
<class 'numpy.ndarray'>

 

β–· torch.numpy()λ₯Ό μ΄μš©ν•˜μ—¬ ν…μ„œλ₯Ό λ„˜νŒŒμ΄μ˜ λ°°μ—΄λ‘œ λ°”κΏ€ 수 μžˆλ‹€.

 

In:

import numpy as np

x = np.ones(5)
y = torch.from_numpy(a)

print(x)
print(type(x))
print(y)
print(type(y))

 

Out:

[1. 1. 1. 1. 1.]
<class 'numpy.ndarray'>
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
<class 'torch.Tensor'>
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

 

β–· torch.from_numpy()λ₯Ό μ΄μš©ν•˜μ—¬ λ„˜νŒŒμ΄λ₯Ό ν…μ„œλ‘œ λ°”κΏ€ 수 μžˆλ‹€.

 


Reference:

"WHAT IS PYTORCH?," PyTorch, https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#sphx-glr-beginner-blitz-tensor-tutorial-py.

 

'Deep Learning > PyTorch' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

μžλ™ λ―ΈλΆ„(Automatic differentiation) μ‚¬μš©λ²•  (0) 2020.07.25