개발/AI

[Python] Affine, Sigmoid, BCELoss 구현

jykim23 2023. 11. 14. 21:31

Model = Affine + Sigmoid

BCELoss 함수로 Model w,b 업데이트

 

import numpy as np
class AffineFunction:
    def __init__(self):
        self.w = np.random.randn(2)
        self.b = np.random.randn()
    def forward(self, x):
        self.x = x
        z = np.dot(self.w, x) + self.b
        return z
    def backward(self, dJ_dz, LR):
        dz_dw = self.x
        dz_db = 1
        dJ_dw = dJ_dz * dz_dw
        dJ_db = dJ_dz * dz_db
        self.w -= dJ_dw * LR
        self.b -= dJ_db * LR

class Sigmoid:
    def forward(self, z):
        self.pred = 1 / (1 + np.exp(-z))
        return self.pred
    def backward(self, dJ_dp):
        dz_dp = (self.pred * (1 - self.pred))
        dJ_dz = dJ_dp * dz_dp
        return dJ_dz

class Model:
    def __init__(self):
        self.affine = AffineFunction()
        self.sigmoid = Sigmoid()

    def forward(self, x):
        z = self.affine.forward(x)
        pred = self.sigmoid.forward(z)
        return pred

    def backward(self, dJ_dp, LR):
        dJ_dz = self.sigmoid.backward(dJ_dp)
        self.affine.backward(dJ_dz, LR)

class BCELoss:
    def forward(self, y, pred):
        self.y, self.pred = y, pred
        J = -(self.y * np.log(self.pred) + (1 - self.y) * np.log(1 - self.pred))
        return J
    def backward(self):
        dJ_dp = (self.pred - self.y)/(self.pred * (1 - self.pred))
        return dJ_dp

'개발 > AI' 카테고리의 다른 글

[Pytorch] XOR 학습  (0) 2023.11.20
[Python] ANN : XOR - 2 layer 구현하기  (0) 2023.11.16
[Python] GBL : Gradient-based Learning  (1) 2023.11.13
[Python] Backpropagation  (1) 2023.11.13
[Python] Logic Gate 클래스 만들기  (0) 2023.11.07