2017-10-19 13 views
1

몇 가지 tensorflow 함수를 사용하는 함수가 있습니다. Theano에서이 함수가 필요합니다. 왜냐하면 Platform에서이 코드를 사용하기 위해서 Theano가 설치되어 있고 tensorflow가 없기 때문입니다. 나는 케사스를 중심으로 일하고있어서 tensorflow는 나를 위해 아주 비밀 스럽다. 기능은 다음과 같습니다 플랫폼에 Tensorflow 설치tensorflow 코드를 theano 코드로 변환 할 수 있습니까?

class WeightedBinaryCrossEntropy(object): 

    def __init__(self, pos_ratio): 
     neg_ratio = 1. - pos_ratio 
     self.pos_ratio = tf.constant(pos_ratio, tf.float32) 
     self.weights = tf.constant(neg_ratio/pos_ratio, tf.float32) 
     self.__name__ = "weighted_binary_crossentropy({0})".format(pos_ratio) 

    def __call__(self, y_true, y_pred): 
     return self.weighted_binary_crossentropy(y_true, y_pred) 

    def weighted_binary_crossentropy(self, y_true, y_pred): 
     # Transform to logits 
     epsilon = tf.convert_to_tensor(K.common._EPSILON, y_pred.dtype.base_dtype) 
     y_pred = tf.clip_by_value(y_pred, epsilon, 1 - epsilon) 
     y_pred = tf.log(y_pred/(1 - y_pred)) 

     cost = tf.nn.weighted_cross_entropy_with_logits(y_true, y_pred, self.weights) 
     return K.mean(cost * self.pos_ratio, axis=-1) 

model.compile(loss=WeightedBinaryCrossEntropy(0.05), optimizer=optimizer, metrics=['accuracy']) 

이 불가능합니다. 여기에서 코드를 얻었습니다 https://github.com/fchollet/keras/issues/2115

그래서 Theano에는 Tensorflow의 기능처럼 작동하는 기능이 있습니까?

답변

3

어쩌면 당신은 keras을 사용하고 휴대용 모델이 있어야합니다
(Keras 기능 : https://keras.io/backend/)

class WeightedBinaryCrossEntropy(object): 

    def __init__(self, pos_ratio): 
     neg_ratio = 1. - pos_ratio 
     self.pos_ratio = K.constant([pos_ratio]) 
     self.weights = K.constant([neg_ratio/pos_ratio]) 
     self.__name__ = "weighted_binary_crossentropy({0})".format(pos_ratio) 

    def __call__(self, y_true, y_pred): 
     return self.weighted_binary_crossentropy(y_true, y_pred) 

    def weighted_binary_crossentropy(self, y_true, y_pred): 
     # Transform to logits 
     epsilon = K.epsilon() 
     y_pred = K.clip(y_pred, epsilon, 1 - epsilon) 
     y_pred = K.log(y_pred/(1 - y_pred)) 

     #for the crossentropy, you can maybe (make sure, please) 
     #use K.binary_crossentropy and multiply the weights later 
     cost = self.approach1(y_true,y_pred) 

     #or you could simulate the same formula as in tensorflow: 
     #https://www.tensorflow.org/api_docs/python/tf/nn/weighted_cross_entropy_with_logits 
     cost = self.approach2(y_true,y_pred) 

     return K.mean(cost * self.pos_ratio, axis=-1) 

    #I use a similar thing in my codes, but I'm not sure my weights are calculated the same way you do 
    def approach1(self,y_true,y_pred): 

     weights = (y_true * self.weights) + 1 #weights applied only to positive values 
     return K.binary_crossentropy(y_true, y_pred,from_logits=True)*weights 

    #seems more trustable, since it's exactly the tensorflow formula 
    def approach2(self,y_true,y_pred): 

     posPart = y_true * (-K.log(K.sigmoid(y_pred))) * self.weights 
     negPart = (1-y_true)*(-K.log(1 - K.sigmoid(y_pred))) 

     return posPart + negPart    


model.compile(loss=WeightedBinaryCrossEntropy(0.05), optimizer=optimizer, metrics=['accuracy']) 
+0

가 감사를, 그것을 테스트하는 시간이 좀 걸릴 것입니다 : 시험 후, 나는 아마 그것을 받아 들일 것입니다 . –