딥러닝에서 많이 사용되는 활성화 함수 (activation function)와 비활성화 함수(deactivation function)

 본 글에는 딥러닝에서 사용하는 활성화 함수와 역전파(backward propagation)과정에서 사용되는 비활성화 함수의 python, c 코드가 실려 있다. 


1. relu , leaky-relu


python code

def relu(x):
    if x > 0:
        return x
    return 0

def deactive_relu(x):
    if x > 0:
        return 1
    return 0

def leaky_relu(x):
    if x > 0:
        return x
    return x*0.01

def deactive_leaky_relu(x):
    if x > 0:
        return 1
    
    return 0.01


c code

static double ReLU(double x)
{
   if (x > 0) return x;
   return 0;
}

static double DeActivateReLU(double x)
{
   if (x > 0) return 1;
   return 0;
}

그래프로 표시하면 아래와 같다.


def show_relu():
    x = np.arange(-10,10,0.1)
    y = [relu(i) for i in x]
    
    plt.title('relu')
    plt.plot(x,y)
    plt.show()
    
    y = [deactive_relu(relu(i)) for i in x]
    plt.title('deactivated relu')
    plt.plot(x,y)
    plt.show()


2. sigmoid


python code

def sigmoid(x):
    return 1.0/(1+np.exp(-x))

def deactive_sigmoid(x):
    return x*(1-x)


c code

static double Sigmoid(double x)
{
    return 1.0 / (1 + exp(-x));
}

static double DeActivateSigmoid(double x)
{
    return x * (1.0 - x);
}


sigmoid의 Deactivation함수는 sigmoid(x)(1.0-sigmoid(x))가 된다. 이를 그래프로 표시하면 아래와 같다.


def show_sigmoid():
    x = np.arange(-10,10,0.1)
    y = [sigmoid(i) for i in x]
    plt.title('sigmoid')
    plt.plot(x,y)
    plt.show()
    
    y = [deactive_sigmoid(sigmoid(i)) for i in x]
    plt.title('deactivated sigmoid')
    plt.plot(x,y)
    plt.show()


3. tanh


python code

def tanh(x):
    return np.tanh(x)

def deactive_tanh(x):
    return 1-x*x


c code

static double ActivateTanh(double x)
{
   return tanh(x);
}

static double DeActivateTanh(double x)
{
   return 1.0 - (x*x);
}


tanh의 Deactivation 함수는 1.0-tanh(x)^2이 되며, 그래프는 아래와 같다.

def show_tanh():
    x = np.arange(-10,10,0.1)
    y = [tanh(i) for i in x]
    plt.title('tanh')
    plt.plot(x,y)
    plt.show()
    
    y = [np.tanh(i) for i in x]
    y = [deactive_tanh(tanh(i)) for i in x]
    plt.title('deactivated tanh')
    plt.plot(x,y)
    plt.show()


댓글

이 블로그의 인기 게시물

windows에서 간단하게 크롬캐스트(Chromecast)를 통해 윈도우 화면 미러링 방법

쉽게 설명한 파티클 필터(particle filter) 동작 원리와 예제

이미지의 가장자리 경계면 검출 (edge detection) 원리와 예제

간단한 cfar 알고리즘에 대해

VLC media player 오디오 끊어질 때 해결법