νμ΄ν μΉμ ν μ(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 |
---|