<수업 내용>
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
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):
pred = model.forward(X_)
J = bce.forward(pred, y_)
loss = bce.backward()
w, b = model.backward(loss, lr)
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)