[AI] Pytorch Derivative, Partial derivative and Gradient

JAsmine_log·2026년 2월 5일

Derivative

f(x)=limh0f(x+h)f(x)hf'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}

함수 f(x)=x2f(x) = x^2에 대해:

dfdx=2x\frac{df}{dx} = 2x

x=3x=3일 때:

dfdxx=3=23=6\frac{df}{dx}\bigg|_{x=3} = 2 \cdot 3 = 6
import torch

# 1변수 함수: f(x) = x^2
x = torch.tensor(3.0, requires_grad=True)
y = x ** 2

# 미분 계산
y.backward()
print(f"f(x) = x^2, x=3일 때")
print(f"df/dx = {x.grad}")  # 2*3 = 6

Partial derivative

다변수 함수 f(x,y)f(x, y)에서 xx에 대한 편미분:

fx=limh0f(x+h,y)f(x,y)h\frac{\partial f}{\partial x} = \lim_{h \to 0} \frac{f(x+h, y) - f(x, y)}{h}

yy는 상수로 취급합니다.

함수 f(x,y)=x2+3xy+y2f(x, y) = x^2 + 3xy + y^2에 대해:

fx=2x+3y\frac{\partial f}{\partial x} = 2x + 3y
fy=3x+2y\frac{\partial f}{\partial y} = 3x + 2y

(x,y)=(2,3)(x, y) = (2, 3)일 때:

fx(2,3)=2(2)+3(3)=13\frac{\partial f}{\partial x}\bigg|_{(2,3)} = 2(2) + 3(3) = 13
fy(2,3)=3(2)+2(3)=12\frac{\partial f}{\partial y}\bigg|_{(2,3)} = 3(2) + 2(3) = 12
# 2변수 함수: f(x, y) = x^2 + 3xy + y^2
x = torch.tensor(2.0, requires_grad=True)
y = torch.tensor(3.0, requires_grad=True)

z = x**2 + 3*x*y + y**2

# 편미분 계산
z.backward()

print(f"\nf(x,y) = x^2 + 3xy + y^2, x=2, y=3일 때")
print(f"∂f/∂x = {x.grad}")  # 2x + 3y = 2*2 + 3*3 = 13
print(f"∂f/∂y = {y.grad}")  # 3x + 2y = 3*2 + 2*3 = 12

Gradient

nn변수 함수 f(x1,x2,,xn)f(x_1, x_2, \ldots, x_n)의 그래디언트는 모든 편미분을 벡터로 모은 것:

f=[fx1fx2fxn]\nabla f = \begin{bmatrix} \frac{\partial f}{\partial x_1} \\ \frac{\partial f}{\partial x_2} \\ \vdots \\ \frac{\partial f}{\partial x_n} \end{bmatrix}

그래디언트는 함수가 가장 빠르게 증가하는 방향을 나타냄

함수 f(x1,x2,x3)=x12+2x22+3x32f(x_1, x_2, x_3) = x_1^2 + 2x_2^2 + 3x_3^2에 대해:

f=[fx1fx2fx3]=[2x14x26x3]\nabla f = \begin{bmatrix} \frac{\partial f}{\partial x_1} \\ \frac{\partial f}{\partial x_2} \\ \frac{\partial f}{\partial x_3} \end{bmatrix} = \begin{bmatrix} 2x_1 \\ 4x_2 \\ 6x_3 \end{bmatrix}

(x1,x2,x3)=(1,2,3)(x_1, x_2, x_3) = (1, 2, 3)일 때:

f(1,2,3)=[2(1)4(2)6(3)]=[2818]\nabla f\bigg|_{(1,2,3)} = \begin{bmatrix} 2(1) \\ 4(2) \\ 6(3) \end{bmatrix} = \begin{bmatrix} 2 \\ 8 \\ 18 \end{bmatrix}
# 다변수 함수의 그래디언트
x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)

# f(x1, x2, x3) = x1^2 + 2*x2^2 + 3*x3^2
f = x[0]**2 + 2*x[1]**2 + 3*x[2]**2

f.backward()

print(f"\nf(x) = x1^2 + 2*x2^2 + 3*x3^2")
print(f"Gradient ∇f = {x.grad}")  # [2*x1, 4*x2, 6*x3] = [2, 8, 18]

Application

  • requires_grad=True: 이 텐서에 대해 그래디언트를 계산하겠다는 표시
  • .backward(): 자동 미분 수행
  • .grad: 계산된 그래디언트 확인
  • Gradient는 모든 편미분을 모은 벡터로, 함수가 가장 빠르게 증가하는 방향을 나타냄
import torch

# 신경망 파라미터 최적화 예제
# f(w, b) = (w*x - y)^2 (손실 함수)

w = torch.tensor(0.5, requires_grad=True)
b = torch.tensor(0.0, requires_grad=True)

# 데이터
x = torch.tensor(2.0)
y_true = torch.tensor(5.0)

# Forward pass
y_pred = w * x + b
loss = (y_pred - y_true) ** 2

# Backward pass (그래디언트 계산)
loss.backward()

print(f"\n최적화 예제:")
print(f"예측값: {y_pred.item()}")
print(f"손실: {loss.item()}")
print(f"∂L/∂w = {w.grad}")
print(f"∂L/∂b = {b.grad}")

# 그래디언트를 이용한 파라미터 업데이트
learning_rate = 0.01
with torch.no_grad():
    w -= learning_rate * w.grad
    b -= learning_rate * b.grad
    
print(f"업데이트된 w: {w.item()}")
print(f"업데이트된 b: {b.item()}")
profile
Everyday Research & Development

0개의 댓글