首页 文章

使用theano功能在keras中定制丢失功能

提问于
浏览
3

我想使用自己的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(self,optimizer,loss,class_mode)418 else: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加权(y_true,y_pred,weight,mask)80'''81#score_array有ndim> = 2 ---> 82 score_array = fn(y_true, y_pred)83如果蒙版不是None:84#mask应该与custom_objective中的score_array具有相同的形状(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(result,y_pred)15 return K.mean(result,axis = -1)TypeError:不支持的操作数类型:'Function'和'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

有人可以帮我这个吗?!

谢谢

1 回答

  • 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))
    

相关问题