Created
April 9, 2017 00:43
-
-
Save oiehot/a2125527129af15428a3e2989a925ecc to your computer and use it in GitHub Desktop.
오차역전파법을 위한 Relu층, Sigmoid층
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
''' | |
class ReluLayer: | |
def __init__(self): | |
self.x = None | |
def forward(self, x): | |
self.x = x | |
if x > 0: | |
return x | |
else: | |
return 0 | |
def backward(self, dout): | |
if self.x > 0: | |
return dout | |
else: | |
return 0 | |
''' | |
class ReluLayer: | |
def __init__(self): | |
self.mask = None | |
def forward(self, x): | |
# 0 이하는 True가 되고, | |
self.mask = (x <= 0) | |
out = x.copy() | |
# True인 부분은 0 처리한다. | |
out[self.mask] = 0 | |
return out | |
def backward(self, dout): | |
# 0 이하인 부분은 0 처리. 나머지는 그대로. | |
dout[self.mask] = 0 | |
dx = dout | |
return dx | |
class SigmoidLayer: | |
def __init__(self): | |
self.y = None | |
def forward(self, x): | |
self.y = 1 / (1 + np.exp(-x)) | |
return self.y | |
def backward(self, dout): | |
# 최대한 단축 유도된 공식. 유도과정은 책을 참고. (167-169p) | |
dx = dout * self.y * (1 - self.y) | |
return dx | |
# Relu층 테스트. | |
r = ReluLayer() | |
a = np.array([-1,0,1]) | |
d = np.empty(3) | |
d.fill(1.3) | |
print(a) | |
print(r.forward(a)) | |
print(r.backward(d)) | |
# Sigmoid층 테스트. | |
s = SigmoidLayer() | |
b = np.arange(-1.0, 1.1, 0.1) | |
print(b) | |
print(s.forward(b)) | |
print(s.backward(b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment