我想使用我自己的二进制交叉熵,而不是使用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_交叉熵(y_pred,y_true)
但是,当我编译模型时:
sgd = SGD(lr=0.001)
model.compile(loss = custom_objective, optimizer = sgd)
我得到这个错误:
---------------------------------------------------------------------------TypeError Traceback(最近的调用最后)在()3637 sgd=sgd(lr=0.001)--->38模型.编译(loss=custom_objective,优化器=sgd)39#==============================================
C: \Program Files(x86)\Anaconda3\lib\site packages\keras\models.pycompile(self、优化器、loss、class_mode)418其他:419掩码=无-->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,权重,掩码)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秒_log=K.log(1-K.clip(y_true,K.epsilon(),np.inf))12 second_log=elementwise_multiply(second_log,(1-y_true))--->13结果=第二日志+第一日志14#结果=np.multy(结果,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:
Epoch 1/1945/945〔=========================〕-62s-损失:nan-acc:0.0011-val_loss:nan-val_acc:0.0000e+00
有人能帮我做这个吗?!
感谢
我发现了问题。我不得不将返回值乘以"-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))