在使用theano函数的keras中自定义损失函数

时间:2016-08-02 06:43:08

标签: python theano keras

我想使用自己的binary_crossentropy而不是使用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)
  

注意:这是为了练习。我知道   T.nnet.binary_crossentropy(y_pred,y_true)

但是,当我编译模型时:

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

我收到此错误:

  

----------------------------------------------- ---------------------------- TypeError Traceback(最近一次调用   最后)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   编译(self,optimizer,loss,class_mode)       418否则:       419 mask =无    - > 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   加权(y_true,y_pred,权重,掩码)        80'''        81#score_array有ndim> = 2   ---> 82 score_array = fn(y_true,y_pred)        83如果面具不是无:        84#mask应该与score_array具有相同的形状

     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返回K.mean(结果,轴= -1)

     

TypeError:+:'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)

模型编译但损失值 nan

  

大纪元1/1 945/945 [==============================] - 62s - 损失:nan -   acc:0.0011 - val_loss:nan - val_acc:0.0000e + 00

有人可以帮我这个吗?!

由于

1 个答案:

答案 0 :(得分: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)) 
相关问题