[ Python ] Tensorflow max norm 적용하기

2019. 9. 28. 22:35분석 Python/Tensorflow

728x90

맥스 노름 규제 주기

각 가중치에 특정 threshold로 제한하는 방식

threshold를 1을 줘서 최대 노름이 1이 되도록 가중치를 clipping 하는 연산 수행

clip_by_value로 줄 수도 있지만, norm으로 주는 것도 좋음.

맥스 노름 규제 같은 경우에는 전체 손실 함수에 규제 손실 항을 추가할 필요 없음

그래도 훈련이 끝나고 clip_weghts를 실행시켜야 하므로 값을 얻어야 함. 

그래서 훈련이 끝나고 실행시켜줘야함.

def max_norm_regularizer(threshold, axes=1, name="max_norm",collection="max_norm"):
         def max_norm(weights):
            clipped = tf.clip_by_norm(weights, clip_norm=threshold, axes=axes)
            clip_weights = tf.assign(weights, clipped, name=name)
            tf.add_to_collection(collection, clip_weights)
            return None # there is no regularization loss term
         return max_norm

max_norm_reg = max_norm_regularizer(threshold = 1.0)
Dis_Weight = tf.get_variable("DWeight_" + str(idx) , dtype = tf.float32 , 
                                                 shape = [self.Dim * 2 , h_dim] , 
                                                 regularizer = max_norm_reg ,
                                                 initializer = init)

 

http://www.openwith.net/wp-content/uploads/2017/04/TensorFlow%ED%99%9C%EC%9A%A9v3.pdf

불러오는 중입니다...

 

728x90