๋ฅ๋ฌ๋์ ์ํ ์คํ์์ค ๋จธ์ ๋ฌ๋ ํ๋ ์์ํฌ by Meta(facebook)
GPU ๊ธฐ๋ณธ ๊ฐ๋ ๋ฐ Colab์์ GPU ์ฌ์ฉํ๊ธฐ ์์ฝ ์ ๋ฆฌ ๊ธ
| ๊ธฐ๋ฅ | ์ฝ๋ ์์ | ์ค๋ช |
|---|---|---|
| GPU ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ | torch.cuda.is_available() | GPU ์ฌ์ฉ ๊ฐ๋ฅํ ํ๊ฒฝ์ธ์ง ํ์ธ |
| ํ ์๋ฅผ GPU๋ก ์ด๋ | x.cuda() ๋๋ x.to('cuda') | CPU โ GPU |
| ํ ์๋ฅผ CPU๋ก ์ด๋ | x.cpu() ๋๋ x.to('cpu') | GPU โ CPU |
| ์ฅ์น ํ์ธ | x.device | ํ ์์ ํ์ฌ ์ฅ์น ์ ๋ณด ์ถ๋ ฅ |
| ์ฅ์น ์ผ์น ํ์ | x + y ์ฐ์ฐ ์ | x์ y๋ ๋์ผ device์ ์์ด์ผ ํจ |
๋ค์ฐจ์ ๋ฐฐ์ด์ ์๋ฃ๊ตฌ์กฐ



ํ ์์ ๊ธฐ๋ณธ ์์ฑ์ผ๋ก๋ ๋ค์๊ณผ ๊ฐ์ ๊ฒ๋ค์ด ์๋ค.
Pandas Dataframe๊ณผ ๋งค์ฐ ์ ์ฌ.
import torch
tensor = torch.rand(3, 4) # 3 by 4
print(tensor)
print(f"Shape: {tensor.shape}") # ์ฐจ์ ๊ฒฝ์ฐ์๋ ๊ฐ์ฅ ๋ฐ๊นฅ ๋๊ดํธ๋ถํฐ ์ธ๋ฉด ๋จ
print(f"Data type: {tensor.dtype}") # ๊ธฐ๋ณธ์ ์ผ๋ก float
print(f"Device: {tensor.device}") # ๊ธฐ๋ณธ์ ์ผ๋ก CPU
x = torch.FloatTensor([[[1, 2, 3],
[3, 4, 5]],
[[5, 6, 7],
[7, 8, 9]],
[[9, 10, 11],
[11, 12, 13]]])
print(x.shape) #torch.Size([3, 2, 3]) โ ์ ์ผ ํฐ ๊ป๋ฐ๊ธฐ์ ์ฐจ์์ด ๋งจ ์์ผ๋ก
data = [
[1, 2],
[3, 4]
]
x = torch.tensor(data)
print(x)
ft = torch.FloatTensor(data)
print(ft)
lt = torch.LongTensor(data)
print(lt)
bt = torch.ByteTensor(data) #0๋ถํฐ 255๊น์ง์ ๋ฒ์ โ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ ์ฝ
print(bt)
bool_tensor = torch.tensor([True, False, True, False], dtype=torch.bool)
print(bool_tesnor)
a = torch.tensor([5])
b = torch.tensor([7])
c = (a + b).numpy()
print(c)
print(type(c))
result = c * 10
tensor = torch.from_numpy(result)
print(tensor)
print(type(tensor))
x = torch.tensor([
[5, 7],
[1, 2]
])
# x์ ๊ฐ์ ๋ชจ์, ๊ฐ์ด 0์ผ๋ก ์ฑ์์ง ํ
์
x_zeros = torch.zeros_like(x)
print(x_zeros)
# x์ ๊ฐ์ ๋ชจ์, ๊ฐ์ด 1์ธ ํ
์ ์์ฑ
x_ones = torch.ones_like(x)
print(x_ones)
# x์ ๊ฐ์ ๋ชจ์, ์ด๊ธฐํ๋ ๊ฐ์ ๋ฌด์์ (๋ฉ๋ชจ๋ฆฌ ์ํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง)
x_empty = torch.empty_like(x)
print(x_empty)
# x์ ๊ฐ์ ๋ชจ์, ๋ชจ๋ ๊ฐ์ ์ง์ ๋ ๊ฐ(์: 42)์ผ๋ก ์ฑ์
x_full = torch.full_like(x, fill_value=42)
print(x_full)
# x์ ๊ฐ์ ๋ชจ์, ์๋ฃํ์ float์ผ๋ก ๋ณ๊ฒฝํ๊ณ ๋๋ค ๊ฐ ์์ฑ
x_rand = torch.rand_like(x, dtype=torch.float32) # uniform distribution [0, 1)
print(x_rand)
x = torch.randperm(10)
print(x) # tensor([8, 1, 0, 5, 6, 4, 3, 7, 2, 9])
tensor = torch.tensor([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
])
print(tensor[0]) # ์ฒซ ๋ฒ์งธ ํ, tensor[0], :)๋ ๊ฐ๋ฅ
print(tensor[:, 0]) # ์ฒซ ๋ฒ์งธ ์ด
print(tensor[..., -1]) # ๋ง์ง๋ง ์ด ...(ellipsis, ์๋ต)๋ ๋ชจ๋ ์ ์ฐจ์์ ์๋ฏธ, PyTorch์์ ๊ณ ์ฐจ์ ๋ฐฐ์ด์ ๋ค๋ฃฐ ๋ ์์ฃผ ์ฐ์ด๋ ์ฌ๋ผ์ด์ฑ ๋ฌธ๋ฒ
x = torch.FloatTensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]],
[[9, 10],
[11, 12]]])
print(x.size())
print(x[0])
print(x[0, :])
print(x[0, :, :])
print(x[1:3, :, :].size()) #indexing์ด ์๋๋ผ sliciing์ผ๋ก ํ๋ฉด dimension reduction ๋ฐ์ x
print(x[:, :1, :].size())
print(x[:, :-1, :].size())
x = torch.FloatTensor(10, 4)
splits = x.split(4, dim=0)
for s in splits:
print(s.size())
# torch.Size([4, 4])
# torch.Size([4, 4])
# torch.Size([2, 4])
x = torch.FloatTensor(8, 4)
chunks = x.chunk(3, dim=0)
for c in chunks:
print(c.size())
# torch.Size([3, 4])
# torch.Size([3, 4])
# torch.Size([2, 4])
tensor = torch.tensor([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
])
result = torch.cat([tensor, tensor, tensor], dim=0) #dim์ ์ฐจ์ ๋์ค๋ ์์ == ๊ฐ์ฅ ๋ฐ๊นฅ ๋๊ดํธ๋ถํฐ
print(result) # ํ ๋ฐฉํฅ์ผ๋ก ๋ถ์ด๊ธฐ
result = torch.cat([tensor, tensor, tensor], dim=1)
print(result) # ์ด ๋ฐฉํฅ์ผ๋ก ๋ถ์ด๊ธฐ
unsqueeze: ์ฐจ์์ด 1์ธ ์ถ(axis)์ ์์ฑํ๋ ํจ์
- ์ง์ ํ ์์น(dim)์ ํฌ๊ธฐ 1์ธ ์ฐจ์์ ์ถ๊ฐํ๋ ํจ์
squeeze: ์ฐจ์์ด 1์ธ ์ถ(axis)์ ์ ๊ฑฐํ๋ ํจ์
- dim ์ง์ ์ ํ๋ฉด ๋ชจ๋ 1์ง๋ฆฌ ์ฐจ์์ ๋ค ์ ๊ฑฐํจ.
x = torch.FloatTensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
y = torch.FloatTensor([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
print(x.size(), y.size())
z = torch.stack([x, y])
print(z.size()) # torch.Size([2, 3, 3])
# z = torch.cat([x.unsqueeze(0), y.unsqueeze(0)], dim=0)์ ๋์ผํ ๊ฒ
z = torch.stack([x, y], dim=-1)
print(z.size()) # torch.Size([3, 3, 2])
z = torch.stack([x, y], dim=1)
print(z.size()) # torch.Size([3, 2, 3])
x = torch.FloatTensor([[[1, 2]],
[[3, 4]]])
print(x.size()) # torch.Size([2, 1, 2])
y = x.expand(*[2, 3, 2])
print(y)
print(y.size()) # torch.Size([2, 3, 2])
a = torch.tensor([2], dtype=torch.int)
b = torch.tensor([5.0])
print(a.dtype)
print(b.dtype)
print(a.float())
print(b.int())
print(a + b) # ์๋ ํ๋ณํ (a๊ฐ float์ผ๋ก ๋ฐ๋)
print(a + b.type(torch.int32)) # b๋ฅผ int32๋ก ๋ฐ๊ฟ์ ์ฐ์ฐ
a = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8])
b = a.view(4, 2)
b = a.reshape(4, 2)
print(b.shape)
b = b.reshape(2,-1)
print(b.shape)
a[0] = 7
print(b) # ์์ ๋ณต์ฌ ๋ฌธ์ ๋ฐ์
c = a.clone().view(4, 2) # ๊น์ ๋ณต์ฌ๋ก๋ ํ์ต ๊ทธ๋ํ ๋์ผ๋ฉด์ ์นดํผํ ์ ์๋ clone ์ ์ผ ๋ง์ด ์ฌ์ฉ
a[0] = 9
print(c)
a = torch.rand((64, 32, 3))
print(a.shape)
b = a.permute(2, 1, 0)
print(b.shape)
batch: ๋ฅ๋ฌ๋ ํ์ต ์ ํ ๋ฒ์ ๋ชจ๋ธ์ ๋ฃ๋ ๋ฐ์ดํฐ ๋ฌถ์
a = torch.Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]])
print(a.shape) # torch.Size([1, 1, 2, 4])
print(a.unsqueeze(0).shape) # dimension arg ํ์
print(a.unsqueeze(3).shape)
print(a.squeeze(0).shape)
print(a.squeeze().shape) # ํฌ๊ธฐ 1์ง๋ฆฌ ์ฐจ์ ๋ชจ๋ ์ ๊ฑฐ
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])
# Arithmetic operations
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a ** b)
# Logical operation
print(a == b)
# Inplace Operation
print(a)
print(a.mul(b)) # ๊ทธ๋ฅ ๊ณฑํ๊ธฐ
print(a)
print(a.mul_(b)) # ์ฃผ์๊ฐ์ ๋ฎ์ด์์ฐ๋ ๊ฒ โ ๋ฉ๋ชจ๋ฆฌ ์ ์ฝํ๋ ๋ฐฉ๋ฒ์ด๊ธด ํ๋ฐ Pytorch๊ฐ ์ด๋ฏธ ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ ์ ํด๋
print(a) # a ๋ฎ์ด์์์ง
# Broadcast in Operations
x = torch.FloatTensor([[1, 2],
[4, 8]])
y = torch.FloatTensor([3,
5])
print(x.size()) # torch.Size([2, 2])
print(y.size()) # torch.Size([2])
z = x + y
print(z)
print(z.size()) # torch.Size([2, 2])
x = torch.FloatTensor([[1, 2]])
y = torch.FloatTensor([[3],
[5]])
print(x.size()) # torch.Size([1, 2])
print(y.size()) # torch.Size([2, 1])
z = x + y
print(z)
print(z.size()) torch.Size([2, 2])
# ์ฌ์ค scalar 1์ ๋ํ๋ ๊ฒ๋, 1์ด ์ฐจ์์ด ๋ง์ถฐ์ง๋ฉด์ ์ ์ฒด๋ก ๋ํด์ง๋ ๊ฒ
# ์๋ฌ๊ฐ ์ ๋๋ ๊ฒ ์ข์ ๊ฒ๋ง์ ์๋ ์ ์๋ค โ ์๋์น ์์ broadcasting์ ์กฐ์ฌํ๋ผ!
# broadcasting์ด ๋ถ๊ฐ๋ฅํ๋ฉด ์๋ฌ ๋ฐ์
print(a.matmul(b))
print(torch.matmul(a, b))
a = torch.Tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
print(a.mean()) # ์ ์ฒด ํ๊ท
print(a.mean(dim=0)) # ํ ์ฐ์ฐ
print(a.mean(dim=1)) # ์ด ์ฐ์ฐ
print(a.sum())
print(a.sum(dim=0))
print(a.sum(dim=1))
x = torch.randperm(3**3).reshape(3, 3, -1)
values, indices = torch.sort(x, dim=-1, descending=True)
print(values)
print(indices)
print(a.max())
print(a.max(dim=0)) ์ด๋ค ์ฐจ์์ ๊ธฐ์ค์ผ๋ก ์ํํ ์ง
print(a.max(dim=1))
print(a.argmax())
print(a.argmax(dim=0))
print(a.argmax(dim=1))
# print(x)
x = torch.tensor([
[[18, 9, 25],
[ 0, 16, 8],
[24, 20, 14]],
[[ 1, 4, 17],
[ 2, 22, 7],
[ 5, 10, 12]],
[[15, 13, 23],
[ 3, 21, 19],
[26, 6, 11]]])
print(x.size())
# torch.Size([3, 3, 3])
y = x.argmax(dim=-1)
print(y)
# tensor([[2, 1, 0],
# [2, 1, 2],
# [2, 1, 0]])
print(y.size())
# torch.Size([3, 3])
values, indices = torch.topk(x, k=1, dim=-1)
print(values.size()) # torch.Size([3, 3, 1]) # k=1์ด์ฌ๋ ์ฐจ์์ด ์ด์์์
print(indices.size()) # torch.Size([3, 3, 1])
_, indices = torch.topk(x, k=2, dim=-1)
print(indices.size()) torch.Size([3, 3, 2])
print(x.argmax(dim=-1) == indices[:, :, 0])
# tensor([[True, True, True],
# [True, True, True],
# [True, True, True]])
# Top K๋ก Sortingํ๊ธฐ
target_dim = -1
values, indices = torch.topk(x,
k=x.size(target_dim),
largest=True)
print(values)
x = torch.FloatTensor([i for i in range(3**2)]).reshape(3, -1)
# ๋ฐฉ๋ฒ 1: torch.where(์กฐ๊ฑด, ์ฐธ์ผ ๋, ๊ฑฐ์ง์ผ ๋)
y = torch.where(x > 4, torch.tensor(-1.0), x)
print(y)
# ๋ฐฉ๋ฒ 2: tensor ํด๋์ค์ masked_fill ๋ด์ฅ ๋ฉ์๋ ์ฌ์ฉ
mask = x > 4
print(mask)
y = x.masked_fill(mask, value=-1)
print(y)
์ด๋ ๋ฐฉํฅ์ผ๋ก, ์ผ๋ง๋ ๋ฐ๊ฟ์ผ ํ ๊น?โ ๊ธฐ์ธ๊ธฐ๋?
์ด๋ค ๊ฐ(x)์ ์กฐ๊ธ ๋ฐ๊ฟจ์ ๋, ์ถ๋ ฅ(y)์ด ์ผ๋ง๋ ๋ฐ๋๋์ง๋ฅผ ์๋ ค์ฃผ๋ ๋ฐฉํฅ + ๋ฏผ๊ฐ๋
์์ ํ (forward)
์์ค ๊ณ์ฐ (loss)
์ญ์ ํ (backward) โ โ
์ฌ๊ธฐ์ autograd๊ฐ ๋ฑ์ฅ!
๊ฐ์ค์น ์
๋ฐ์ดํธ
๋์ ๊ทธ๋ํ ๊ธฐ๋ฐ์ด๋ผ ์ง๊ด์ ์ด๊ณ ์ ์ฐํจleaf variable: ์ฐ์ฐ์ ํตํด ๋ง๋ค์ด์ง์ง ์๊ณ , requires_grad=True๋ก ์์ฑ๋ ์ฌ์ฉ์ ์ ์ ํ
์ (์ฃผ๋ก Input parameter)x = torch.tensor([3.0, 4.0], requires_grad=True)
y = torch.tensor([1.0, 2.0], requires_grad=True)
z = x + y
print(z)
print(z.grad_fn)
out = z.mean()
print(out)
print(out.grad_fn)
out.backward() # scalar์ ๋ํ์ฌ ๊ฐ๋ฅ, not scalar ์ถ๋ ฅ์ผ ๋๋ ๋ฐ๋์ gradient๋ฅผ ์ค์ผ ํจ (out.shape๊ณผ ๋์ผํด์ผ ํจ)
print(x.grad) # x๊ฐ์ด ๋ฐ๋ ๋ z๋ ์ผ๋ง๋ ๋ฐ๋๋์ง
print(y.grad) # y๊ฐ์ด ๋ฐ๋ ๋ z๋ ์ผ๋ง๋ ๋ฐ๋๋์ง
print(z.grad) # leaf variable(์ด๊ธฐ ๋ณ์)์ ๋ํด์๋ง gradient ์ถ์ ์ด ๊ฐ๋ฅํ๋ค. ๋ฐ๋ผ์ None.
requires_grad=True๋ก ๊ธฐ์ธ๊ธฐ ์ถ์ with torch.no_grad()temp = torch.tensor([3.0, 4.0], requires_grad=True)
print(temp.requires_grad)
print((temp ** 2).requires_grad)
# ๊ธฐ์ธ๊ธฐ ์ถ์ ์ ํ์ง ์๊ธฐ ๋๋ฌธ์ ๊ณ์ฐ ์๋๊ฐ ๋ ๋น ๋ฅด๋ค.
with torch.no_grad():
temp = torch.tensor([3.0, 4.0], requires_grad=True)
print(temp.requires_grad)
print((temp ** 2).requires_grad)