2016-08-02 102 views
3

我想使用我自己的binary_crossentropy而不是使用Keras庫自帶的函數。這裏是我的自定義函數:使用theano函數keras中的自定義丟失函數

import theano 
    from keras import backend as K 

    def elementwise_multiply(a, b): # a and b are tensors 
     c = a * b 
     return theano.function([a, b], c) 

    def custom_objective(y_true, y_pred): 
     first_log = K.log(y_pred) 
     first_log = elementwise_multiply(first_log, y_true) 
     second_log = K.log(1 - y_pred) 
     second_log = elementwise_multiply(second_log, (1 - y_true)) 
     result = second_log + first_log 
     return K.mean(result, axis=-1) 

note: This is for practice. I'm aware of T.nnet.binary_crossentropy(y_pred, y_true)

但是,當我編譯模型:

sgd = SGD(lr=0.001) 
model.compile(loss = custom_objective, optimizer = sgd) 

我得到這個錯誤:

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in() 36 37 sgd = SGD(lr=0.001) ---> 38 model.compile(loss = custom_objective, optimizer = sgd) 39 # ==============================================

C:\Program Files (x86)\Anaconda3\lib\site-packages\keras\models.py in compile(self, optimizer, loss, class_mode) 418 else: 419 mask = None --> 420 train_loss = weighted_loss(self.y, self.y_train, self.weights, mask) 421 test_loss = weighted_loss(self.y, self.y_test, self.weights, mask) 422

C:\Program Files (x86)\Anaconda3\lib\site-packages\keras\models.py in weighted(y_true, y_pred, weights, mask) 80 ''' 81 # score_array has ndim >= 2 ---> 82 score_array = fn(y_true, y_pred) 83 if mask is not None: 84 # mask should have the same shape as score_array

in custom_objective(y_true, y_pred) 11 second_log = K.log(1 - K.clip(y_true, K.epsilon(), np.inf)) 12 second_log = elementwise_multiply(second_log, (1-y_true)) ---> 13 result = second_log + first_log 14 #result = np.multiply(result, y_pred) 15 return K.mean(result, axis=-1)

TypeError: unsupported operand type(s) for +: 'Function' and 'Function'

,當我和內聯函數代替elementwise_multiply :

def custom_objective(y_true, y_pred): 
    first_log = K.log(y_pred)  
    first_log = first_log * y_true 
    second_log = K.log(1 - y_pred) 
    second_log = second_log * (1-y_true) 
    result = second_log + first_log 
    return K.mean(result, axis=-1) 

模型編譯,但損失值是

Epoch 1/1 945/945 [==============================] - 62s - loss: nan - acc: 0.0011 - val_loss: nan - val_acc: 0.0000e+00

可能有人能幫我解決這個請?

謝謝

+0

錯誤消息以指示'結果= second_log + first_log'作爲錯誤,因爲這些是兩種功能。你檢查過'K.log'的輸出嗎? –

+0

@ M.T是的,K.log的輸出是「Elemwise {log,no_inplace} .0」。我剛剛更新了這個問題(增加了模型編譯但損失爲nan的另一個場景) – Nejla

回答

4

我發現了這個問題。我必須將返回值乘以「-1」,因爲我使用隨機梯度繼承者(sgd)作爲優化器而不是隨機梯度上升!

下面是代碼和它就像一個魅力:似乎

import theano 
from keras import backend as K 

def custom_objective(y_true, y_pred): 
    first_log = K.log(y_pred)  
    first_log = first_log * y_true 
    second_log = K.log(1 - y_pred) 
    second_log = second_log * (1 - y_true) 
    result = second_log + first_log 
    return (-1 * K.mean(result))