[청년취업사관학교 새싹]핀테커스 수업 13주차(11/14)

장민정·2023년 11월 14일
0

<수업 내용>

# 딥러닝 모델
class Affine:

  def __init__(self):
    np.random.seed(8)
    self.w = np.random.randn(2)
    self.b = np.random.randn(1)

  def forward(self, x):
    self.x = x
    z = np.dot(self.w, self.x) + self.b
    return z

  def backward(self, dJ_dz, lr):
    self.dJ_dw = dJ_dz * self.x
    self.dJ_db = dJ_dz *1 
    self.w -= lr*self.dJ_dw
    self.b -= lr*self.dJ_db
    return self.w, self.b



class Sigmoid:
  def forward(self, z):
    self.z = z
    y_= 1/(1+np.exp(-self.z))
    return y_

  def bakcward(self, dJ_dy_):
    dJ_dz = dJ_dy_ * ((1+np.exp(-self.z))**(-2))*np.exp(-self.z)
    return dJ_dz  

class Model:
  def __init__(self):
    self.affine = Affine()
    self.sigmoid = Sigmoid()
    
  def forward(self, x):
    z = self.affine.forward(x)
    pred = self.sigmoid.forward(z)
    return pred

  def backward(self, loss, lr):
    dJ_dz = self.sigmoid.bakcward(loss)
    self.w , self.b= self.affine.backward(dJ_dz, lr)

    return self.w, self.b
    
# loss함수
class BCE:
  def forward(self, y_, y):
    self.y_ = y_
    self.y = y
    J = -(self.y*np.log(self.y_) + (1-self.y)*np.log(1-self.y_))
    return J

  def backward(self):
    dJ_dy_ =(self.y_ - self.y)/(self.y_*(1-self.y_))
    return dJ_dy_
    
# 데이터 학습

from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

N_samples = 100
lr = 0.001
EPOCHS =30

X, y = make_blobs(n_samples=N_samples, centers =2, n_features =2, cluster_std = 0.5, random_state = 0)
# 객체 생성
model = Model()
bce= BCE()
accuracy = []
bce_loss = []

for epoch in range(EPOCHS):
  accuracy_ = []
  bce_loss_ = []
  for X_, y_ in zip(X, y):
    # training
    pred = model.forward(X_)
    J = bce.forward(pred, y_)
    loss = bce.backward()
    w, b = model.backward(loss, lr)
    # metric
    if pred >0.5 :
      proba = 1
      
    else: proba =0

    if proba == y_ :
      a =1
    else : a =0 
    accuracy_.append(a)
    bce_loss_.append(J)
 
  accuracy.append(np.sum(accuracy_)/len(y))

  bce_loss.append(np.mean(bce_loss_))
  
# 시각화  
x=np.linspace(0,EPOCHS+1,30)
fig, ax = plt.subplots(2,1,figsize =(20,10))

ax[0].plot(x, bce_loss)
ax[1].plot(x, accuracy)

0개의 댓글