当前位置:   article > 正文

keras常用损失函数_keras损失函数

keras损失函数
均方误差损失
y_pred.shape=[num,]
num为输出个数,第一维度其实是batch_size,进行忽略。
def mse(y_true,y_pred):
    return K.mean(K.square(y_pred-y_true),axis=-1)


绝对值误差损失
def mae(y_true,y_pred):
    return K.mean(K.abs(y_pred - y_true),axis =-1)

多分类损失 one-hot编码
output.shape = [None,num_class,]
def categorical_crossentropy(y_true,y_pred):
    y_pred /= tf.reduce_sum(y_pred,axis=-1,keep_dims=True)
    _epsilon = 1e-4
######进行裁剪,防止log(0) = None
    y_pred = tf.clip_by_value(y_pred,_epsilon,1.-_epsilon)
    return -tf.reduce_sum(y_true*tf.log(y_pred),-1)

KL散度距离
def KLD(y_true,y_pred):    
   _epsilon = 1e-4
   y_true = K.clip(y_true,_epsilon,1)
   y_pred = K.clip(y_pred,_epsilon,1)
   return K.sum(y_true*K.log(y_true/y_pred),axis=-1)
交叉熵损失
y_pred.shape = [num,] 取值范围0-1
def binary_crossentropy(y_true,y_pred):

   _epsilon = 1e-4
   final = y_true*tf.log(y_pred+_epsilon)+(1-y_true)*tf.log(1-y_pred+_epsilon)
   return -tf.reduce_mean(final,axis=-1) 

smooth_L1_loss损失

def smooth_l1_loss(y_true,y_pred):
    absolute_loss = tf.abs(y_true-y_pred)
    square_loss = 0.5*tf.square(absolute)
    return tf.reduce_sum(tf.where(tf.less(absolute_loss,1.0),square_loss,absolute_loss-0.5)),axis=-1)
    
     



 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/133159
推荐阅读
相关标签
  

闽ICP备14008679号