码迷,mamicode.com
首页 > 其他好文 > 详细

回归问题常用的损失函数总结

时间:2020-07-16 00:09:41      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:误差   image   sum   load   sel   col   font   rom   ret   

1. 均方误差MSE

技术图片

 2. 平均绝对误差MAE

技术图片

# true: 真目标变量的数组
# pred: 预测值的数组

def mse(true, pred): 
    return np.sum((true - pred)**2)
 
 def mae(true, pred):
  return np.sum(np.abs(true - pred))
 
 # 调用sklearn 
 from sklearn.metrics import mean_squared_error
 from sklearn.metrics import mean_absolute_error

3. Huber损失函数

技术图片

 4. Log-Cosh损失函数

 技术图片

# huber 损失
def huber(true, pred, delta):
    loss = np.where(np.abs(true-pred) < delta , 0.5*((true-pred)**2), delta*np.abs(true - pred) - 0.5*(delta**2))
    return np.sum(loss)

# log cosh 损失
def logcosh(true, pred):
    loss = np.log(np.cosh(pred - true))
return np.sum(loss)

5. 实例

import numpy as np
import math

true = [0,1,2,3,4]
pred = [0,0,1,5,-11]

# MSE
mse = mean_squared_error(true,pred)
print("RMSE: ",math.sqrt(mse))

loss =0 
for i,j in zip(true,pred):
    loss += mse(i,j)
mseloss = math.sqrt(loss / len(true))
print("RMSE: ",mseloss)

#MAE
mae = mean_absolute_error(true,pred)
print("MAE: ",mae)

loss = 0
for i,j in zip(true,pred):
    loss += mae(i,j)
maeloss = loss / len(true)
print("MAE: ",maeloss)

#Huber
loss = 0
for i,j in zip(true,pred):
    loss += huber(i,j,1)
loss = loss / len(true)
print("Huber: ",loss)

#Log-Cosh
loss = 0
for i,j in zip(true,pred):
    loss += logcosh(i,j)
loss = loss / len(true)
print("Log-Cosh: ",loss)

6. tanh

技术图片

Python中直接调用np.tanh() 即可计算。 

 

参考:https://zhuanlan.zhihu.com/p/39239829

回归问题常用的损失函数总结

标签:误差   image   sum   load   sel   col   font   rom   ret   

原文地址:https://www.cnblogs.com/HuangYJ/p/13307468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!