Stochastic Gradient Descent method ( SGD ) 확률적 경사하강법 1.기능 W 는 가중치 매개변수이고 (eta)는 학습률, 라운드디L / 라운드디W 는 손실함수의 기울기를 뜻한다. 즉 W값을…
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import numpy as np class relu: def __init__(self): self.grad = None def Relu(self,x): self.grad = (x <= 0) out = x.copy() out[self.grad] = 0.0 return out def Relu_grad(self,dout): dout[self.grad] = 0.0 dx = dout return dx dout = np.array([-2.0,4,-1.1,5]) func = relu() x = np.array([-1.0,2.1,-0.3,4.2]) print(func.Relu(x)) print(func.Relu_grad(dout)) |
Relu function으로 입력 값 x <= 0 이면 0을 , x > 0 이면 x 자체를 출력하는 function이다. 순전파 시에는 x <= 0 에…
|
1 2 3 4 5 6 7 8 9 10 11 |
import numpy as np x = np.array([[1,2,3],[4,5,6]]) def sigmoid(x): return 1 / (1 + np.exp(-x)) def sigmoid_grad(x): return sigmoid(x) * (1 - sigmoid(x)) def sigmoid_grad_1(x): return sigmoid(x) * (1 - sigmoid(x)) print(sigmoid_grad_1(x)) print(sigmoid_grad(x)) |
sigmoid = 순전파 : 1 / (1 + exp(-x) ) 역전파 : y(1-y) 로 y는 sigmoid에 해당 행렬이라 연산 위치에 따라 값이 바뀔 줄…
sotfmax-with-loss 계층 그래프 softmax 와 cross entropy error 를 한꺼번에 순전파와 역전파로 구현한다. 순전파의 (3 layer에 한에)마지막 출력 값은 각 입력 값 y1, y2, y3…
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import numpy as np class Affine: def __init__(self,W,b): self.W = W self.b = b self.x = None self.dW = None self.db = None def forward(self,x): self.x = x out = np.dot(x,self.W) + self.b return out def backward(self,dout): dx = np.dot(dout,self.W.T) self.dW = np.dot(self.x.T,dout) self.db = np.sum(dout, axis=0) return dx |
Affine 계층에 대한 순전파와 역전파에 대한 코드이다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import numpy as np X_dot_W = np.array([[0,0,0], [10,10,10]]) B = np.array([1,2,3]) print(X_dot_W) print(B) result = X_dot_W + B print(result) dY = np.array([[1,2,3], [4,5,6]]) print(dY) dB = np.sum(dY,axis=0) print(dB) |
Affine 계층에서 bias 에 대한 연산에 대한 코드이해다. 순전파에선 XW + b 연산이 수행되는데 python 에선 broad cast 기능때문에 행렬의 형태를 맞추어 주지 않아도…
Affine transformation 신경망에서의 행렬곱에 대하여 주요 문장 ( ” 편미분 과정에서 적당히 행렬을 맞춰준다 “) 대응하는 차원의 원소 수를 일치시킨다. 끝에 알 수 없는 라운드…
Sigmoid function 의 순전파와 역전파를 계산그래프로 이해해보겠다. sigmoid 식이다. 함수 그래프 순전파 식 전개 역전파 전개 : ‘∂(’라운드 디‘라고 읽음) 편의 상 ‘라운드’ 라…
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import numpy as np class Relu: def __init__(self): self.mask = None def forward(self,x): self.mask = (x <= 0) out = x.copy() out[self.mask] = 0 return out def backward(self,dout): dout[self.mask] = 0 dx = dout return dx x = np.array([[1.0, -0.5], [-2.0, 3.0]]) print(x) mask = (x <= 0) print(mask) out = x.copy() out[mask] = 0 print(out) |
ReLu function 을 numpy array로 순전파와 역전파를 구현하는 코드이다. ReLu의 식이며, x > 0 일때는 x 자체를 0보다 작을때는 0을 출력하는 function 이다. 역전파에…
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import numpy as np class MulLayer: def __init__(self): self.x = None self.y = None def forward(self,x,y): self.x = x self.y = y out = x * y return out def backward(self,dout): dx = dout * self.y dy = dout * self.x return dx,dy class AddLayer: def __init__(self): pass def forward(self,x,y): out = x + y return out def backward(self,dout): dx = dout dy = dout return dx,dy #variable apple_price = 100 apple_num = 2 orange_price = 150 orange_num = 3 tax = 1.1 apple_mul_layer = MulLayer() orange_mul_layer = MulLayer() tax_mul_layer = MulLayer() fruit_add_layer = AddLayer() #forward apple_total = apple_mul_layer.forward(apple_price,apple_num) print(apple_total) orange_total = orange_mul_layer.forward(orange_price,orange_num) print(orange_total) fruit_total = fruit_add_layer.forward(apple_total,orange_total) print(fruit_total) fruit_tax = tax_mul_layer.forward(fruit_total,tax) print(fruit_tax) #backward d_total_price = 1 d_fruit_total, d_tax = tax_mul_layer.backward(d_total_price) print(d_fruit_total,d_tax) d_apple_total, d_orange_total = fruit_add_layer.backward(d_fruit_total) print(d_apple_total,d_orange_total) d_apple_price, d_apple_num = apple_mul_layer.backward(d_apple_total) print(d_apple_price,d_apple_num) d_orange_price, d_orange_num = orange_mul_layer.backward(d_orange_total) print(d_orange_price,d_orange_num) |
다음은 사과 2개와 귤 3개 소비세를 포함한 총 가격이 역전파를 통해 각각의 변수가 얼마나 총 가격에 얼마나 영향을 미치는지 파악해 보는 코드이다. 구현한 코드가…