Activation (SQNL , Soft Cliping , Gaussian)

2019. 6. 9. 23:20분석 Python/Tensorflow

728x90

 Activations 중에서 보통 Relu 계열들이 잘 되기 때문에 그 부분에 대해서 많이 발전을 한 것 같다.

 

하지만 나는 생성 모델에 관심을 가지고 있다 보니, Scaling을 특정 구간에 가두는 그런 함수들이 필요한데

 

그 예로는 tanh(-1, 1)와 sigmoid(0 , 1)  같은 함수가 있다. 

 

하지만 이 함수들은 미분한 것을 이용하다보면 많은 한계가 있다는 것을 알 수 있다.

 

이런 것들을 사용하면 Gradient Update 하는 방식으로 하는 것에서 큰 문제가 생긴다.

 

Layer를 깊게 쌓을 수록 Activation의 영향력이 점점 줄어든다는 단점이 분명히 존재한다.

 

이러한 것을 해결하기 위해 찾은 것들이 Square Nonlinearity (SQNL) ( -1 , 1) 과 Soft Cliping(0,1)  Gaussian(0,1)이다.

 

이런 것들이 따로 구현되어있지 않아서 찾다 보니 다음과 같은 것이 있고, 내가 따로 구현한 것도 있다.

 

일단 둘 다 잘 작동했다!

 

물론 저런 것이 잘 된다라는 보장은 없다! 새로운 Activation이 있으니 경우의 수를 늘려서 할 수 있을 것 같다.

import tensorflow as tf
import numpy as np
from scipy import arange


def gauss(x) :
    """ 0 ~ 1 로 하는 가우시안 함수 """
    return tf.exp(-x**2)

def soft_cliping(alpha = 0.5 , x=None ) :
    """ 0 ~ 1 로 하는 새로운 함수 alpha라는 하이퍼 파라미터가 존재함"""
    first = tf.div(1.0 , alpha)
    second = tf.log( tf.div(tf.add(1.0, tf.exp( tf.multiply(alpha , x) )) , 
                            tf.add(1.0 , tf.exp( tf.multiply(alpha, (tf.add(x , -1.0)) )))))
    return tf.multiply(first , second )

## https://cup-of-char.com/writing-activation-functions-from-mostly-scratch-in-python/

def tf_sqnlsig(x):   #tensorflow SQNLsigmoid
    """https://cup-of-char.com/writing-activation-functions-from-mostly-scratch-in-python/"""
    u=tf.clip_by_value(x,-2,2)
    a = u
    b= tf.negative(tf.abs(u))
    wsq = (tf.multiply(a,b))/4.0
    y = tf.add(tf.multiply(tf.add(u,wsq),0.5),0.5)
    return y

def tf_sqnl(x): #tensorflow SQNL
    """https://cup-of-char.com/writing-activation-functions-from-mostly-scratch-in-python/"""
    #tf.cond(x>2,lambda: tf.multiply(2,1),lambda:tf.multiply(x,1))
    #tf.cond(tf.less(x,-2),lambda: -2,lambda:tf.multiply(x,1))
    u=tf.clip_by_value(x,-2,2)
    a = u
    b= tf.negative(tf.abs(u))
    wsq = (tf.multiply(a,b))/4.0
    y = tf.add(u,wsq)
    return y

 

https://cup-of-char.com/writing-activation-functions-from-mostly-scratch-in-python/

 

Writing Activation Functions From (Mostly) Scratch in Python – Cup of Char

After working through Tariq Rashid’s Make Your Own Neural Network  book, my manager (the same one that gifted me the book) posed a question to me, “What if we want to use a different activation function than the sigmoid function?” I then knew that it was b

cup-of-char.com

https://en.wikipedia.org/wiki/Activation_function#cite_note-28

 

Activation function - Wikipedia

For the formalism used to approximate the influence of an extracellular electrical field on neurons, see activating function. For a linear system’s transfer function, see transfer function. Logistic activation function In artificial neural networks, the ac

en.wikipedia.org

 

 

https://ieeexplore.ieee.org/document/8489043

 

SQNL: A New Computationally Efficient Activation Function - IEEE Conference Publication

 

ieeexplore.ieee.org

 

https://github.com/cafornaca/MYONN/blob/master/Activation_Functions.py

 

cafornaca/MYONN

Code, applications, and notes from Make Your Own Neural Network by Tariq Rashid. - cafornaca/MYONN

github.com

 

728x90