1.2 신경망 추론

Untitled

**** 요약 ****

역전파는 Loss function(L)을 ouptut(y)로 미분한 값에서부터 각 구간의 미분 값을 곱해줍니다. 이때 output과 label의 오차를 앞 계층에 전해줍니다. 미분(기울기)를 이용해서 신경망의 매개변수를 갱신합니다.

[계층으로 클래스화 및 순전파 구현]

<aside> 💡 TwoLayerNet 처리: x → Affine → Sigmoid → Affine → S

</aside>

$$ y = \frac{1}{1 + exp(-x)} $$

class Sigmoid:
    def __init__(self):
        self.params = [] # 매개변수 따로 X
    
    def forward(self,x):
        return 1 / (1 + np.exp(-x))

class Affine:
    def __init__(self, W, b): 
        # 초기화될 때 가중치, 편향 받는다
        self.params = [W,b]

    def forward(self,x):
        # 순전파 처리 구현
        W,b = self.params
        out = np.matmul(x,W) + b
        return out
class TwoLayerNet:
    def __init__(self, input_size, hidden_size, output_size):
        I, H, O = input_size, hidden_size, output_size

        # 가중치와 편향 초기화
        W1 = np.random.randn(I,H)
        b1 = np.random.random(H)
        W2 = np.random.randn(H,O)
        b2 = np.random.random(O)

        # 계층 생성
        self.layers = [
            Affine(W1, b1),
            Sigmoid(),
            Affine(W2, b2)
        ]

        # 모든 가중치를 리스트에 모은다.
        self.params = []
        for layer in self.layers:
            self.params += layer.params

    # 주 추론 처리
    def predict(self,x):
        for layer in self.layers:
            x = layer.forward(x) # 각 레이어에 대해서 순전파 적용
        return x
x = np.random.randn(10,2)
model = TwoLayerNet(2,4,3)
s = model.predict(x)