【发布时间】:2019-04-23 00:45:56
【问题描述】:
假设y_true 和y_pred 在[-1,1] 中。我想要一个加权均方误差损失函数,其中y_true 中为正而y_pred 中为负或反之亦然的样本的损失由exp(alpha) 加权。这是我的代码:
import keras.backend as K
alpha = 1.0
def custom_loss(y_true, y_pred):
se = K.square(y_pred-y_true)
true_label = K.less_equal(y_true,0.0)
pred_label = K.less_equal(y_pred,0.0)
return K.mean(se * K.exp(alpha*K.cast(K.not_equal(true_label,pred_label), tf.float32)))
这里是这个损失函数的图。不同的曲线代表y_true 的不同值。
我想知道:
- 这是否是一个有效的损失函数,因为它在 0 中不可微分?
- 我的代码正确吗?
【问题讨论】:
标签: python keras classification regression loss-function