โ ๏ธ ๋ณธ ๋ด์ฉ์ PyTorch Korea์ ๊ณต์ ๋ฌธ์์ ๊ธฐ๋ฐํ์ฌ ๊ณต๋ถํ ๋ด์ฉ์ ์ ์๊ฒ์ด๋ ์ํด๋ฐ๋๋๋ค!
torch.autograd๋ฅผ ์ฌ์ฉํ ์๋ ๋ฏธ๋ถ
ํ์ดํ ์น(PyTorch) ๊ธฐ๋ณธ ์ตํ๊ธฐ|| ๋น ๋ฅธ ์์|| ํ ์(Tensor)|| Dataset๊ณผ Dataloader|| ๋ณํ(Transform)|| ์ ๊ฒฝ๋ง ๋ชจ๋ธ ๊ตฌ์ฑํ๊ธฐ|| Autograd|| ์ต์ ํ(Optimization)|| ๋ชจ๋ธ ์ ์ฅํ๊ณ ๋ถ๋ฌ์ค๊ธฐ ์ ๊ฒฝ๋ง์ ํ์ตํ ๋ ๊ฐ์ฅ
tutorials.pytorch.kr
torch.autograd๋ฅผ ์ฌ์ฉํ ์๋ ๋ฏธ๋ถ
์ผ๋ฐ์ ์ผ๋ก Neural Network(์ ๊ฒฝ๋ง)์ ํ์ตํ ๋ ๊ฐ์ฅ ์์ฃผ ์ฌ์ฉ๋๋ ์๊ณ ๋ฆฌ์ฆ์ Backpropagation(์ญ์ ํ)์ ๋๋ค.
- ์ด ์๊ณ ๋ฆฌ์ฆ์์, ๋งค๊ฐ๋ณ์(๋ชจ๋ธ ๊ฐ์ค์น)๋ ์ฃผ์ด์ง ๋งค๊ฐ๋ณ์์ ๋ํ ์์ค ํจ์์ ๋ณํ๋(gradient)์ ๋ฐ๋ผ ์กฐ์ ๋ฉ๋๋ค.
- ์ด๋ฌํ ๋ณํ๋๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด PyTorch์๋ 'torch.autograd' ๋ผ๊ณ ๋ถ๋ฆฌ๋ ์๋ ๋ฏธ๋ถ ์์ง์ด ๋ด์ฅ๋์ด ์์ต๋๋ค.
- ์ด๋ ๋ชจ๋ ๊ณ์ฐ ๊ทธ๋ํ์ ๋ํ ๋ณํ๋์ ์๋ ๊ณ์ฐ์ ์ง์ํฉ๋๋ค.
- ์๋๋ ์ ๋ ฅ ํ ์ x, ๋งค๊ฐ๋ณ์ w์ b, ๊ทธ๋ฆฌ๊ณ ์์ค ํจ์๊ฐ ์๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋จ์ผ ๊ณ์ธต ์ ๊ฒฝ๋ง์ ์ ์ํ๊ณ , Backpropagation(์ญ์ ํ)๋ฅผ ์ํํ๋ ์์ ์ ๋๋ค.
import torch
# ์
๋ ฅ ๋ฐ ์ถ๋ ฅ ํ
์ ์ ์
x = torch.ones(5) # input tensor
y = torch.zeros(3) # expected output
# ๋ชจ๋ธ ๊ฐ์ค์น ๋ฐ ํธํฅ ์ ์
w = torch.randn(5, 3, requires_grad=True) # requires_grad=True: ์๋ ๋ฏธ๋ถ์ ์ํด ํ์
b = torch.randn(3, requires_grad=True)
# ์ ํ ๋ณํ ๋ฐ ์์ค ํจ์ ๊ณ์ฐ
z = torch.matmul(x, w) + b
loss = torch.nn.functional.binary_cross_entropy_with_logits(z, y)
print(f"Loss: {loss}")
# ์ญ์ ํ ์ํ
loss.backward()
# ๋ณํ๋ ์ถ๋ ฅ
print(f"w.grad: {w.grad}")
print(f"b.grad: {b.grad}")
Loss: 0.5469902753829956
w.grad: tensor([[0.0285, 0.2291, 0.1074],
[0.0285, 0.2291, 0.1074],
[0.0285, 0.2291, 0.1074],
[0.0285, 0.2291, 0.1074],
[0.0285, 0.2291, 0.1074]])
b.grad: tensor([0.0285, 0.2291, 0.1074])
- x = torch.ones(5): ์ ๋ ฅ ํ ์ x๋ ๋ชจ๋ ์์๊ฐ 1์ธ ํฌ๊ธฐ 5์ ํ ์์ ๋๋ค.
- y = torch.zeros(3): ๊ธฐ๋ ์ถ๋ ฅ ํ ์ y๋ ๋ชจ๋ ์์๊ฐ 0์ธ ํฌ๊ธฐ 3์ ํ ์์ ๋๋ค.
- w = torch.randn(5, 3, requires_grad=True): ๊ฐ์ค์น ํ ์ w๋ 5x3 ํฌ๊ธฐ์ ๋๋ค ํ ์์ด๋ฉฐ, requires_grad=True๋ก ์ค์ ๋์ด ๋ณํ๋๋ฅผ ๊ณ์ฐํ ์ ์์ต๋๋ค.
- b = torch.randn(3, requires_grad=True): ํธํฅ ํ ์ b๋ ํฌ๊ธฐ 3์ ๋๋ค ํ ์์ด๋ฉฐ, requires_grad=True๋ก ์ค์ ๋์ด ๋ณํ๋๋ฅผ ๊ณ์ฐํ ์ ์์ต๋๋ค.
- z = torch.matmul(x, w) + b: ์ ํ ๋ณํ์ ์ํํ์ฌ ์ถ๋ ฅ ํ ์ z๋ฅผ ๊ณ์ฐํฉ๋๋ค.
- loss = torch.nn.functional.binary_cross_entropy_with_logits(z, y): ์์ค ํจ์๋ฅผ ๊ณ์ฐํฉ๋๋ค. ์ฌ๊ธฐ์๋ binary_cross_entropy_with_logits ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ์ญ์ ํ ์ํ: loss.backward(): ์ญ์ ํ๋ฅผ ์ํํ์ฌ ์์ค ํจ์์ ๋ณํ๋๋ฅผ ๊ณ์ฐํฉ๋๋ค.
- ๋ณํ๋ ์ถ๋ ฅ: w.grad: ๊ฐ์ค์น w์ ๋ํ ๋ณํ๋๋ฅผ ์ถ๋ ฅํฉ๋๋ค, b.grad: ํธํฅ b์ ๋ํ ๋ณํ๋๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
Tensor, Function๊ณผ ์ฐ์ฐ๊ทธ๋ํ(Computational graph)
์ด ์ฝ๋๋ ๋ค์์ ์ฐ์ฐ ๊ทธ๋ํ๋ฅผ ์ ์ํฉ๋๋ค:
- ์ด ์ ๊ฒฝ๋ง์์, w์ b๋ ์ต์ ํ๋ฅผ ํด์ผ ํ๋ ๋งค๊ฐ๋ณ์์ ๋๋ค.
- ๋ฐ๋ผ์ ์ด๋ฌํ ๋ณ์๋ค์ ๋ํ ์์ค ํจ์์ ๋ณํ๋๋ฅผ ๊ณ์ฐํ ์ ์์ด์ผ ํฉ๋๋ค.
- ์ด๋ฅผ ์ํด์ ํด๋น ํ ์์ requires_grad ์์ฑ์ ์ค์ ํฉ๋๋ค.
requires_grad ์ ๊ฐ์ ํ ์๋ฅผ ์์ฑํ ๋ ์ค์ ํ๊ฑฐ๋, ๋์ค์ x.requires_grad_(True) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋์ค์ ์ค์ ํ ์๋ ์์ต๋๋ค.
- ์ฐ์ฐ ๊ทธ๋ํ๋ฅผ ๊ตฌ์ฑํ๊ธฐ ์ํด ํ ์์ ์ ์ฉํ๋ ํจ์๋ ์ฌ์ค Function ํด๋์ค์ ๊ฐ์ฒด์ ๋๋ค.
- ์ด ๊ฐ์ฒด๋ ์์ ํ ๋ฐฉํฅ์ผ๋ก ํจ์๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ๊ณผ, ์ญ๋ฐฉํฅ ์ ํ ๋จ๊ณ์์ ๋ํจ์(derivative)๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ์๊ณ ์์ต๋๋ค.
- ์ญ๋ฐฉํฅ ์ ํ ํจ์์ ๋ํ ์ฐธ์กฐ(reference)๋ ํ ์์ grad_fn ์์ฑ์ ์ ์ฅ๋ฉ๋๋ค.
- Function์ ๋ํ ์์ธํ ์ ๋ณด๋ ์๋์ ๊ณต์๋ฌธ์ ์์ ์ฐพ์๋ณผ ์ ์์ต๋๋ค.
- PyTorch AutoGrad ๊ณต์๋ฌธ์
Automatic differentiation package - torch.autograd — PyTorch 2.4 documentation
Automatic differentiation package - torch.autograd torch.autograd provides classes and functions implementing automatic differentiation of arbitrary scalar valued functions. It requires minimal changes to the existing code - you only need to declare Tensor
pytorch.org
print(f"Gradient function for z = {z.grad_fn}")
print(f"Gradient function for loss = {loss.grad_fn}")
Gradient function for z = <AddBackward0 object at 0x781041f2d420>
Gradient function for loss = <BinaryCrossEntropyWithLogitsBackward0 object at 0x7810196afe20>
๋ณํ๋(Gradient) ๊ณ์ฐํ๊ธฐ
- ์ ๊ฒฝ๋ง์์ ๋งค๊ฐ๋ณ์์ ๊ฐ์ค์น๋ฅผ ์ต์ ํํ๋ ค๋ฉด ๋งค๊ฐ๋ณ์์ ๋ํ ์์คํจ์์ ๋ํจ์(derivative)๋ฅผ ๊ณ์ฐํด์ผ ํฉ๋๋ค.
- ์ฆ, x์ y์ ์ผ๋ถ ๊ณ ์ ๊ฐ์์ ∂๐๐๐ ๐ /∂๐ค ์ ∂๐๐๐ ๐ /∂๐ ๊ฐ ํ์ํฉ๋๋ค.
- ์ด๋ฌํ ๋ํจ์๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด, loss.backward()๋ฅผ ํธ์ถํ ๋ค์ w.grad ์ b.grad ์์ ๊ฐ์ ๊ฐ์ ธ์ต๋๋ค.
loss.backward()
print(w.grad)
print(b.grad)
tensor([[0.0556, 0.1949, 0.1666],
[0.0556, 0.1949, 0.1666],
[0.0556, 0.1949, 0.1666],
[0.0556, 0.1949, 0.1666],
[0.0556, 0.1949, 0.1666]])
tensor([0.0556, 0.1949, 0.1666])
์ฐ์ฐ ๊ทธ๋ํ์ ์(leaf) ๋ ธ๋๋ค ์ค `requires_grad` ์์ฑ์ด `True`๋ก ์ค์ ๋ ๋ ธ๋๋ค์ `grad` ์์ฑ๋ง ๊ตฌํ ์ ์์ต๋๋ค.
๊ทธ๋ํ์ ๋ค๋ฅธ ๋ชจ๋ ๋ ธ๋์์๋ ๋ณํ๋๊ฐ ์ ํจํ์ง ์์ต๋๋ค.
์ฑ๋ฅ ์์ ์ด์ ๋ก, ์ฃผ์ด์ง ๊ทธ๋ํ์์์ `backward` ๋ฅผ ์ฌ์ฉํ ๋ณํ๋ ๊ณ์ฐ์ ํ ๋ฒ๋ง ์ํํ ์ ์์ต๋๋ค.
๋ง์ฝ ๋์ผํ ๊ทธ๋ํ์์ ์ฌ๋ฌ๋ฒ์ `backward` ํธ์ถ์ด ํ์ํ๋ฉด, `backward` ํธ์ถ ์์ `retrain_graph=True`๋ฅผ ์ ๋ฌํด์ผ ํฉ๋๋ค.
๋ณํ๋(Gradient) ์ถ์ ๋ฉ์ถ๊ธฐ
- ๊ธฐ๋ณธ์ ์ผ๋ก, requires_grad=True์ธ ๋ชจ๋ ํ ์๋ค์ ์ฐ์ฐ ๊ธฐ๋ก์ ์ถ์ ํ๊ณ ๋ณํ๋ ๊ณ์ฐ์ ์ง์ํฉ๋๋ค.
- ๊ทธ๋ฌ๋ ๋ชจ๋ธ์ ํ์ตํ ๋ค ์ ๋ ฅ ๋ฐ์ดํฐ๋ฅผ ๋จ์ํ ์ ์ฉํ๊ธฐ๋ง ํ๋ ๊ฒฝ์ฐ์ ๊ฐ์ด ์์ ํ ์ฐ์ฐ๋ง ํ์ํ ๊ฒฝ์ฐ์๋, ์ด๋ฌํ ์ถ์ ์ด๋ ์ง์์ด ํ์ ์์ ์ ์์ต๋๋ค.
- ์ฐ์ฐ ์ฝ๋๋ฅผ torch.no_grad() ๋ธ๋ก์ผ๋ก ๋๋ฌ์ธ์ ์ฐ์ฐ ์ถ์ ์ ๋ฉ์ถ ์ ์์ต๋๋ค.
z = torch.matmul(x, w)+b
print(z.requires_grad)
with torch.no_grad():
z = torch.matmul(x, w)+b
print(z.requires_grad)
True
False
- ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ํ ์์ detach() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค:
z = torch.matmul(x, w)+b
z_det = z.detach()
print(z_det.requires_grad)
False
๋ํ ๋ณํ๋ ์ถ์ ์ ๋ฉ์ถฐ์ผ ํ๋ ์ด์ ๋ค์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
- ์ ๊ฒฝ๋ง์ ์ผ๋ถ ๋งค๊ฐ๋ณ์๋ฅผ ๊ณ ์ ๋ ๋งค๊ฐ๋ณ์(frozen parameter) ๋ก ํ์ํฉ๋๋ค.
- ๋ณํ๋๋ฅผ ์ถ์ ํ์ง ์๋ ํ ์์ ์ฐ์ฐ์ด ๋ ํจ์จ์ ์ด๊ธฐ ๋๋ฌธ์, ์์ ํ ๋จ๊ณ๋ง ์ํํ ๋ ์ฐ์ฐ ์๋๊ฐ ํฅ์๋ฉ๋๋ค.
์ฐ์ฐ ๊ทธ๋ํ์ ๋ํ ์ถ๊ฐ ์ ๋ณด
- ๊ฐ๋ ์ ์ผ๋ก, autograd๋ ๋ฐ์ดํฐ(ํ ์)์ ๋ฐ ์คํ๋ ๋ชจ๋ ์ฐ์ฐ๋ค(๋ฐ ์ฐ์ฐ ๊ฒฐ๊ณผ๊ฐ ์๋ก์ด ํ ์์ธ ๊ฒฝ์ฐ๋ ํฌํจํ์ฌ)์ ๊ธฐ๋ก์ Function ๊ฐ์ฒด๋ก ๊ตฌ์ฑ๋ ๋ฐฉํฅ์ฑ ๋น์ํ ๊ทธ๋ํ(DAG; Directed Acyclic Graph)์ ์ ์ฅ(keep)ํฉ๋๋ค.
- torch.autograd Function ๊ณต์๋ฌธ์
Automatic differentiation package - torch.autograd — PyTorch 2.4 documentation
Automatic differentiation package - torch.autograd torch.autograd provides classes and functions implementing automatic differentiation of arbitrary scalar valued functions. It requires minimal changes to the existing code - you only need to declare Tensor
pytorch.org
- ์ด ๋ฐฉํฅ์ฑ ๋น์ํ ๊ทธ๋ํ(DAG)์ ์(leave)์ ์ ๋ ฅ ํ ์์ด๊ณ , ๋ฟ๋ฆฌ(root)๋ ๊ฒฐ๊ณผ ํ ์์ ๋๋ค.
- ์ด ๊ทธ๋ํ๋ฅผ ๋ฟ๋ฆฌ์์๋ถํฐ ์๊น์ง ์ถ์ ํ๋ฉด ์ฐ์ ๋ฒ์น(chain rule)์ ๋ฐ๋ผ ๋ณํ๋๋ฅผ ์๋์ผ๋ก ๊ณ์ฐํ ์ ์์ต๋๋ค.
- ์์ ํ ๋จ๊ณ์์, autograd๋ ๋ค์ ๋ ๊ฐ์ง ์์ ์ ๋์์ ์ํํฉ๋๋ค:
- ์์ฒญ๋ ์ฐ์ฐ์ ์ํํ์ฌ ๊ฒฐ๊ณผ ํ ์๋ฅผ ๊ณ์ฐํ๊ณ ,
- DAG์ ์ฐ์ฐ์ ๋ณํ๋ ๊ธฐ๋ฅ(gradient function) ๋ฅผ ์ ์ง(maintain)ํฉ๋๋ค.
์ญ์ ํ ๋จ๊ณ๋ DAG ๋ฟ๋ฆฌ(root)์์ .backward()๊ฐ ํธ์ถ๋ ๋ ์์๋ฉ๋๋ค. autograd๋ ์ด๋
- ๊ฐ .grad_fn ์ผ๋ก๋ถํฐ ๋ณํ๋๋ฅผ ๊ณ์ฐํ๊ณ ,
- ๊ฐ ํ ์์ .grad ์์ฑ์ ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์๊ณ (accumulate),
- ์ฐ์ ๋ฒ์น์ ์ฌ์ฉํ์ฌ, ๋ชจ๋ ์(leaf) ํ ์๋ค๊น์ง ์ ํ(propagate)ํฉ๋๋ค.
PyTorch์์ DAG๋ค์ ๋์ (dynamic)์ ๋๋ค.
์ฃผ๋ชฉํด์ผ ํ ์ค์ํ ์ ์ ๊ทธ๋ํ๊ฐ ์ฒ์๋ถํฐ(from scratch) ๋ค์ ์์ฑ๋๋ค๋ ๊ฒ์ ๋๋ค.
๋งค๋ฒ `bachward()` ๊ฐ ํธ์ถ๋๊ณ ๋๋ฉด, autograd๋ ์๋ก์ด ๊ทธ๋ํ๋ฅผ ์ฑ์ฐ๊ธฐ(populate) ์์ํฉ๋๋ค.
์ด๋ฌํ์ ๋๋ถ์ ๋ชจ๋ธ์์ ํ๋ฆ ์ ์ด(control flow) ๊ตฌ๋ฌธ๋ค์ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ ๊ฒ์ ๋๋ค.
๋งค๋ฒ ๋ฐ๋ณต(iteration)ํ ๋๋ง๋ค ํ์ํ๋ฉด ๋ชจ์(shape)์ด๋ ํฌ๊ธฐ(size), ์ฐ์ฐ(operation)์ ๋ฐ๊ฟ ์ ์์ต๋๋ค.
์ ํ์ ์ผ๋ก ์ฝ๊ธฐ(Optional Reading): ํ ์ ๋ณํ๋์ ์ผ์ฝ๋น์ ๊ณฑ (Jacobian Product)
๋๋ถ๋ถ์ ๊ฒฝ์ฐ, ์ค์นผ๋ผ ์์ค ํจ์๋ฅผ ๊ฐ์ง๊ณ ์ผ๋ถ ๋งค๊ฐ๋ณ์์ ๊ด๋ จํ ๋ณํ๋๋ฅผ ๊ณ์ฐํด์ผ ํฉ๋๋ค.
๊ทธ๋ฌ๋ ์ถ๋ ฅ ํจ์๊ฐ ์์์ ํ ์์ธ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค.
์ด๋ด ๋, PyTorch๋ ์ค์ ๋ณํ๋๊ฐ ์๋ ์ผ์ฝ๋น์ ๊ณฑ(Jacobian product)์ ๊ณ์ฐํฉ๋๋ค.
- ๐ฅโ =โจ๐ฅ1,…,๐ฅ๐โฉ ์ด๊ณ , ๐ฆโ =โจ๐ฆ1,…,๐ฆ๐โฉ ์ผ ๋
- ๋ฒกํฐ ํจ์ ๐ฆโ =๐(๐ฅโ ) ์์ ๐ฅโ ์ ๋ํ ๐ฆโ ์ ๋ณํ๋๋ ์ผ์ฝ๋น์ ํ๋ ฌ(Jacobian matrix)๋ก ์ฃผ์ด์ง๋๋ค.
- ์ผ์ฝ๋น์ ํ๋ ฌ ์์ฒด๋ฅผ ๊ณ์ฐํ๋ ๋์ , PyTorch๋ ์ฃผ์ด์ง ์ ๋ ฅ ๋ฒกํฐ ๐ฃ=(๐ฃ1…๐ฃ๐)์ ๋ํ ์ผ์ฝ๋น์ ๊ณฑ(Jacobian Product) ๐ฃ๐⋅๐ฝ ์ ๊ณ์ฐํฉ๋๋ค.
- ์ด ๊ณผ์ ์ ๐ฃ๋ฅผ ์ธ์๋ก backward๋ฅผ ํธ์ถํ๋ฉด ์ด๋ค์ง๋๋ค.
- ๐ฃ์ ํฌ๊ธฐ๋ ๊ณฑ(product)์ ๊ณ์ฐํ๋ ค๊ณ ํ๋ ์๋ ํ ์์ ํฌ๊ธฐ์ ๊ฐ์์ผ ํฉ๋๋ค.
inp = torch.eye(4, 5, requires_grad=True)
out = (inp+1).pow(2).t()
out.backward(torch.ones_like(out), retain_graph=True)
print(f"First call\n{inp.grad}")
out.backward(torch.ones_like(out), retain_graph=True)
print(f"\nSecond call\n{inp.grad}")
inp.grad.zero_()
out.backward(torch.ones_like(out), retain_graph=True)
print(f"\nCall after zeroing gradients\n{inp.grad}")
First call
tensor([[4., 2., 2., 2., 2.],
[2., 4., 2., 2., 2.],
[2., 2., 4., 2., 2.],
[2., 2., 2., 4., 2.]])
Second call
tensor([[8., 4., 4., 4., 4.],
[4., 8., 4., 4., 4.],
[4., 4., 8., 4., 4.],
[4., 4., 4., 8., 4.]])
Call after zeroing gradients
tensor([[4., 2., 2., 2., 2.],
[2., 4., 2., 2., 2.],
[2., 2., 4., 2., 2.],
[2., 2., 2., 4., 2.]])
- ๋์ผํ ์ธ์๋ก backward๋ฅผ ๋์ฐจ๋ก ํธ์ถํ๋ฉด ๋ณํ๋ ๊ฐ์ด ๋ฌ๋ผ์ง๋๋ค.
- ์ด๋ ์ญ๋ฐฉํฅ ์ ํ๋ฅผ ์ํํ ๋, PyTorch๊ฐ ๋ณํ๋๋ฅผ ๋์ (accumulate)ํด์ฃผ๊ธฐ ๋๋ฌธ์ ๋๋ค.
- ์ฆ, ๊ณ์ฐ๋ ๋ณํ๋์ ๊ฐ์ด ์ฐ์ฐ ๊ทธ๋ํ์ ๋ชจ๋ ์(leaf) ๋ ธ๋์ grad ์์ฑ์ ์ถ๊ฐ๋ฉ๋๋ค.
- ๋ฐ๋ผ์ ์ ๋๋ก ๋ ๋ณํ๋๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด์๋ grad ์์ฑ์ ๋จผ์ 0์ผ๋ก ๋ง๋ค์ด์ผ ํฉ๋๋ค.
- ์ค์ ํ์ต ๊ณผ์ ์์๋ ์ตํฐ๋ง์ด์ (optimizer)๊ฐ ์ด ๊ณผ์ ์ ๋์์ค๋๋ค.
์ด์ ์๋ ๋งค๊ฐ๋ณ์ ์์ด `backward()' ํจ์๋ฅผ ํธ์ถํ์ต๋๋ค.
์ด๋ ๋ณธ์ง์ ์ผ๋ก `backward(torch.tensor(1.0))`์ ํธ์ถํ๋ ๊ฒ๊ณผ ๋์ผํ๋ฉฐ, ์ ๊ฒฝ๋ง ํ๋ จ ์ค์ ์์ค๊ณผ ๊ฐ์ ์ค์นผ๋ผ-๊ฐ ํจ์์ ๋ณํ๋๋ฅผ ๊ณ์ฐํ๋ ์ ์ฉํ ๋ฐฉ๋ฒ์ ๋๋ค.
๋ ์์ธํ ๋ด์ฉ์ ๋ณด๊ณ ์ถ์ผ์๋ฉด ์๋ ๋งํฌ์ ๋ค์ด๊ฐ์ ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ธ์!
- Autograd ๊ด๋ จ ๊ณต์๋ฌธ์.
Autograd mechanics — PyTorch 2.4 documentation
Autograd mechanics This note will present an overview of how autograd works and records the operations. It’s not strictly necessary to understand all this, but we recommend getting familiar with it, as it will help you write more efficient, cleaner progr
pytorch.org
'๐ฅ PyTorch' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[PyTorch] Model ์ ์ฅ & ๋ถ๋ฌ์ค๊ธฐ (0) | 2024.07.31 |
---|---|
[PyTorch] ๋ชจ๋ธ ๋งค๊ฐ๋ณ์ ์ต์ ํ(Optimization) ํ๊ธฐ (0) | 2024.07.30 |
[PyTorch] Neural Network Model (์ ๊ฒฝ๋ง ๋ชจ๋ธ) ๊ตฌ์ฑํ๊ธฐ (0) | 2024.07.26 |
[PyTorch] Transform (๋ณํ) (0) | 2024.07.26 |
[PyTorch] Dataset & DataLoader with CIFAR-10 (0) | 2024.07.26 |