【问题标题】:customised loss function in keras using theano function使用 theano 函数在 keras 中自定义损失函数
【发布时间】:2016-12-07 09:26:24
【问题描述】:

我想使用我自己的 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(最近一次调用 最后)在() 36 37 新元 = 新元 (lr=0.001) ---> 38 model.compile(loss = custom_objective, 优化器 = sgd) 39 # ===============================================

C:\Program Files (x86)\Anaconda3\lib\site-packages\keras\models.py 在 编译(自我,优化器,损失,class_mode) 418 其他: 419 掩码 = 无 --> 420 train_loss = weighted_loss(self.y, self.y_train, self.weights, mask) 第421章 第422章

C:\Program Files (x86)\Anaconda3\lib\site-packages\keras\models.py 在 加权(y_true,y_pred,权重,掩码) 80''' 81 # score_array 有 ndim >= 2 ---> 82 score_array = fn(y_true, y_pred) 83 如果掩码不是无: 84 # 掩码应该和 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 结果 = second_log + first_log 14 #result = np.multiply(结果,y_pred) 15 return K.mean(result, axis=-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

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

有人可以帮我解决这个问题吗?!

谢谢

【问题讨论】:

  • 错误消息似乎表明 result = second_log + first_log 是错误,因为这两个函数都是错误的。你检查过K.log的输出了吗?
  • @M.T 是的,K.log 的输出是“Elemwise{log,no_inplace}.0”。我刚刚更新了问题(添加了另一个模型编译但损失为 nan 的场景)

标签: python theano keras


【解决方案1】:

我发现了问题。我必须将返回值乘以“-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)) 

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 2017-12-18
    • 2020-03-27
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 2018-07-22
    • 2018-10-28
    • 2017-12-29
    相关资源
    最近更新 更多