- 常见的损失函数
"""
@Title: loss_function
@Time: 2024/3/7
@Author: Michael Jie
"""
import numpy as np
# 0-1损失函数zero-one loss
# 常用于感知机
def zero_one_loss(h, y):
return 0 if h == y else 1
# 均方误差Mean Squared Error
# 常用于线性回归问题
def mse(h, y):
return np.sum((h - y) ** 2)
# 交叉熵损失函数Cross-entropy loss
# 常同于逻辑回归问题(分类)
def cross_entropy(h, y):
return np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))