
Affine
class Affine:
#파라미터 정의
def __init__(self, w, b): #if Input x = 6, 5
self.params = [w, b] #w, b = 5, 5
self.grads = [np.zeros_like(w), np.zeros_like(b)]
self.x = None
#순전파
def forward(self, x):
w, b = self.params
out = np.matmul(x, w) + b #=> b는 broadcast (1, 5) -> (5 by 5)
self.x = x
return out
#역전파
def backward(self, dout):
w, b = self.params
dx = np.matmul(dout, w.T)
dw = np.matmul(self.x.T, dout)
db = np.sum(dout, axis=0) #5 by 5 => 1 by 5
self.grads[0] = dw
self.grads[1] = db
return dx
'머신러닝&딥러닝' 카테고리의 다른 글
| [머신러닝/딥러닝]Softmax와 Crossentropy (0) | 2024.02.16 |
|---|---|
| [머신러닝/딥러닝] pytorch로 구현하는 MLP (0) | 2024.02.11 |
| [머신러닝 / 딥러닝] Gradient and Newton's method (2) | 2024.01.10 |
| [머신러닝/딥러닝] 오차역전파와 딥러닝 기초 코드 (1) | 2023.10.21 |
| [머신러닝/딥러닝] 퍼셉트론과 MLP (1) | 2023.10.19 |



