Graduate School/Neural Network
Linear Classifier 01
- -
728x90
반응형
In [1]:
import numpy as np
np.random.seed(101)
build your own data¶
x_train: [12, 4]
y_train: [12, ]
x_test: [4, 4]
y_test: [4, ]
[1, 0, 0, 0] --> 0
[0, 1, 0, 0] --> 0
[0, 0, 1, 0] --> 1
[0, 0, 0, 1] --> 1
In [2]:
x_train = np.zeros(shape=[12, 4], dtype=np.float32)
y_train = np.random.randint(0, 4, [12, ])
print(x_train)
print(y_train)
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [3 3 1 2 3 3 1 3 3 1 1 0]
In [3]:
x_train[np.arange(0, 12), y_train] = 1
print(x_train)
[[0. 0. 0. 1.] [0. 0. 0. 1.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.] [0. 0. 0. 1.] [0. 1. 0. 0.] [0. 0. 0. 1.] [0. 0. 0. 1.] [0. 1. 0. 0.] [0. 1. 0. 0.] [1. 0. 0. 0.]]
In [4]:
y_test = np.int32(np.arange(0, 4))
x_test = np.zeros(shape=[4, 4], dtype=np.float32)
x_test[np.arange(0, 4), y_test] = 1
y_train[y_train == 1] = 0
y_train[y_train > 0] = 1
y_test[y_test == 1] = 0
y_test[y_test > 0] = 1
print(x_test)
print(y_test)
[[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] [0 0 1 1]
Linear Classifier¶
$ f = W^T X + b $
In [5]:
class Linear:
def __init__(self, w_shape=[4, 2]):
self.w = np.random.random(size=w_shape)
self.w = np.float32(self.w)
self.b = np.random.random(size=[w_shape[1]])
self.b = np.float32(self.b)
self.x = None
pass
def forward(self, x):
self.x = x
z = np.matmul(x, self.w)+self.b # z = w*x+b
return z
def backward(self, dz):
"""
need dw, db, dx
x's shape = [n, 4]
b's shape = [2] <=> db's shape = [2]
z's shape = [n, 2] <=> dz's shape = [n, 2]
w's shape = [4, 2] <=> dw's shape = [4, 2]
"""
self.dw = np.matmul(self.x.T, dz) # shape = [4, 2]
self.db = np.sum(dz, axis=0) # [n, 2] --> [2]
dx = np.matmul(dz, self.w.T)
return dx
def update(self, step_size):
self.w = self.w-step_size*self.dw
self.b = self.b-step_size*self.db
In [6]:
lin = Linear()
print(lin.w.shape)
print(lin.b.shape)
(4, 2) (2,)
In [7]:
z = lin.forward(x_train)
print(z.shape)
print(z)
(12, 2) [[1.1473757 1.0179554 ] [1.1473757 1.0179554 ] [1.6870271 0.4222926 ] [1.5197108 0.58448565] [1.1473757 1.0179554 ] [1.1473757 1.0179554 ] [1.6870271 0.4222926 ] [1.1473757 1.0179554 ] [1.1473757 1.0179554 ] [1.6870271 0.4222926 ] [1.6870271 0.4222926 ] [1.2724495 1.1259668 ]]
In [8]:
class Loss:
def __init__(self):
pass
def forward(self, x, y):
"""
x's shape = [N,2]
y's shape = [N,]
"""
self.x, self.y = x, y
# step-1: choose the true class score
# inputs: x [N,2], (y), outputs: x_true [N,]
# find: dx, upstream: dx_true
x_true = x[np.arange(0,len(x)), y] #:[N,1], [N,]
# step-2: find the score difference
# inputs: x [N,2], x_true [N,], outputs: diff [N,2]
# find: dx [N,2], dx_true [N,], upstream: ddiff [N,2]
self.diff = diff = x - x_true[..., np.newaxis] + 1 #[N,2]
# step-3: clip the difference
# inputs: diff [N,2], outputs: clip_diff [N,2]
# find: ddiff [N,2], upstream: dclip_diff [N,2]
clip_diff = diff.copy()
clip_diff[clip_diff<0.0] = 0.0 #[N,2]
# step-4: sum along the col axis
# inputs: clip_diff [N,2], outputs: diff_sum [N,]
# find: dclip_diff, upstream: ddiff_sum [N,]
diff_sum = np.sum(clip_diff, axis=1) #[N,]
# step-5: find mean_loss
# inputs: diff_sum [N,] outputs: loss [1]
# find: ddiff_sum [N,] upstream: 1.0
loss = np.mean(diff_sum) # 1
return loss
def backward(self, dz=1.0):
# step-5: find ddiff_sum
ddiff_sum = self.diff[:,0].copy() #[N,]
ddiff_sum[:] = 1/(float(len(self.y)))
# step-4: find dclip_diff
ddiff_clip = self.diff.copy()
ddiff_clip[:] = 0.0
ddiff_clip = ddiff_clip + ddiff_sum[..., np.newaxis]
# step-3: find ddiff
ddiff = self.diff.copy()
ddiff[:] = 0.0
ddiff[self.diff>0.0] = ddiff_clip[self.diff>0.0]
# step-2: find dx, dx_true
dx1 = ddiff
dx_true = -np.sum(ddiff, axis=1)
# step-1: find dx
dx = self.x.copy()
dx[:] = 0.0
dx[np.arange(0,len(self.x)), self.y] = dx_true
dx += dx1
return dx
In [9]:
class Classifier:
def __init__(self, input_dim=4, n_class=2):
# initialize all the layers
self.linear = Linear(w_shape=[input_dim, n_class])
# self.linear2 = Linear(~~) # !!! for Midterm-HW
self.loss = Loss()
def forward(self, x, y):
# h = self.l1.f() # !!! for Midterm-HW
score = self.linear.forward(x)
loss = self.loss.forward(score, y)
return score, loss
def backward(self):
dz = self.loss.backward()
dx = self.linear.backward(dz)
def update(self, step_size):
self.linear.update(step_size)
def train(self, epoch=100, batch_size=4, step_size=0.1):
for ep in range(epoch):
# sample the batch from training data
idx = np.random.permutation(len(y_train))
batch_idx = np.arange(0, batch_size)
x = x_train[batch_idx]
y = y_train[batch_idx]
score, loss = c.forward(x,y)
c.backward()
c.update(step_size)
test_score, test_loss = c.forward(x_test, y_test)
print(ep, loss, test_loss)
In [10]:
c = Classifier()
score, loss = c.forward(x_test, y_test)
print(score)
print(loss)
[[0.27052885 1.5978663 ] [0.9159602 1.2705567 ] [0.87227374 1.5121853 ] [0.23545197 1.1321871 ]] 2.0363219
In [11]:
c.backward()
print(c.linear.dw)
print(c.linear.db)
print()
print(c.linear.w)
print(c.linear.b)
[[-0.25 0.25] [-0.25 0.25] [ 0.25 -0.25] [ 0.25 -0.25]] [0. 0.] [[0.08356144 0.6035484 ] [0.72899276 0.27623883] [0.6853063 0.51786745] [0.04848454 0.13786924]] [0.18696743 0.9943179 ]
In [12]:
c = Classifier()
c.train()
print()
print(c.linear.w)
print(c.linear.b)
0 2.3056033 1.9540684 1 2.1806033 1.9040685 2 2.0556033 1.8540685 3 1.9306033 1.8040686 4 1.8056033 1.7540685 5 1.6806033 1.7040684 6 1.5556033 1.6540685 7 1.4306034 1.6040685 8 1.3056033 1.590314 9 1.2530942 1.565314 10 1.2280942 1.540314 11 1.2030942 1.5153141 12 1.1780943 1.490314 13 1.1530943 1.4653141 14 1.1280943 1.440314 15 1.1030943 1.4153141 16 1.0780942 1.3991944 17 1.0619745 1.365314 18 1.0405942 1.3491943 19 1.0244746 1.3158395 20 1.0036197 1.3366944 21 1.0119746 1.3122199 22 1.0 1.3122199 23 1.0 1.3122199 24 1.0 1.3122199 25 1.0 1.3122199 26 1.0 1.3122199 27 1.0 1.3122199 28 1.0 1.3122199 29 1.0 1.3122199 30 1.0 1.3122199 31 1.0 1.3122199 32 1.0 1.3122199 33 1.0 1.3122199 34 1.0 1.3122199 35 1.0 1.3122199 36 1.0 1.3122199 37 1.0 1.3122199 38 1.0 1.3122199 39 1.0 1.3122199 40 1.0 1.3122199 41 1.0 1.3122199 42 1.0 1.3122199 43 1.0 1.3122199 44 1.0 1.3122199 45 1.0 1.3122199 46 1.0 1.3122199 47 1.0 1.3122199 48 1.0 1.3122199 49 1.0 1.3122199 50 1.0 1.3122199 51 1.0 1.3122199 52 1.0 1.3122199 53 1.0 1.3122199 54 1.0 1.3122199 55 1.0 1.3122199 56 1.0 1.3122199 57 1.0 1.3122199 58 1.0 1.3122199 59 1.0 1.3122199 60 1.0 1.3122199 61 1.0 1.3122199 62 1.0 1.3122199 63 1.0 1.3122199 64 1.0 1.3122199 65 1.0 1.3122199 66 1.0 1.3122199 67 1.0 1.3122199 68 1.0 1.3122199 69 1.0 1.3122199 70 1.0 1.3122199 71 1.0 1.3122199 72 1.0 1.3122199 73 1.0 1.3122199 74 1.0 1.3122199 75 1.0 1.3122199 76 1.0 1.3122199 77 1.0 1.3122199 78 1.0 1.3122199 79 1.0 1.3122199 80 1.0 1.3122199 81 1.0 1.3122199 82 1.0 1.3122199 83 1.0 1.3122199 84 1.0 1.3122199 85 1.0 1.3122199 86 1.0 1.3122199 87 1.0 1.3122199 88 1.0 1.3122199 89 1.0 1.3122199 90 1.0 1.3122199 91 1.0 1.3122199 92 1.0 1.3122199 93 1.0 1.3122199 94 1.0 1.3122199 95 1.0 1.3122199 96 1.0 1.3122199 97 1.0 1.3122199 98 1.0 1.3122199 99 1.0 1.3122199 [[ 0.5206654 0.57878953] [ 1.2598186 0.01696173] [ 0.4381539 1.2829198 ] [-0.04700221 0.80722445]] [0.55287653 0.7436317 ]
728x90
반응형
'Graduate School > Neural Network' 카테고리의 다른 글
LSTM을 이용한 주식 가격 예측 (6) | 2024.09.10 |
---|---|
Face Recognition (0) | 2024.09.10 |
Image Prediction (0) | 2024.09.10 |
Linear Classifier 02 (0) | 2024.09.10 |
Contents
소중한 공감 감사합니다