Cost Function

本文介绍了如何通过平方误差函数(均方误差)衡量假设函数准确性,重点讲解了J(θ0,θ1)的计算公式,并阐述了它在预测值与实际值差异中的作用。此外,我们讨论了平方误差函数在梯度下降算法中的便利性,以及其在机器学习中的广泛应用。

数据集分布

We can measure the accuracy of our hypothesis function by using a cost function. This takes an average difference (actually a fancier version of an average) of all the results of the hypothesis with inputs from x’s and the actual output y’s.

J(θ0,θ1)=12m∑i=1m(y^i−yi)2=12m∑i=1m(hθ(xi)−yi)2J(\theta_0, \theta_1)= \dfrac {1}{2m} \displaystyle \sum _{i=1}^m \left ( \hat{y}_{i}- y_{i} \right)^2 = \dfrac {1}{2m} \displaystyle \sum _{i=1}^m \left (h_\theta (x_{i}) - y_{i} \right)^2J(θ0,θ1)=2m1i=1m(y^iyi)2=2m1i=1m(hθ(xi)yi)2

To break it apart, it is 12xˉ\frac{1}{2} \bar{x}21xˉ where $ \bar{x}$ is the mean of the squares of hθ(xi)−yih_\theta (x_{i}) - y_{i}hθ(xi)yi , or the difference between the predicted value and the actual value.

This function is otherwise called the “Squared error function”-平方误差函数, or “Mean squared error”-均方误差. The mean is halved (12)\left(\frac{1}{2}\right)(21) as a convenience for the computation of the gradient descent, as the derivative term of the square function will cancel out the 12\frac{1}{2}21 term. The following image summarizes what the cost function does:

平方差函数

<think>我们正在讨论机器学习与优化中的成本函数(cost function)。成本函数是机器学习模型训练的核心,它衡量模型预测值与真实值之间的差异,并通过优化算法(如梯度下降)来最小化这个差异,从而训练出最优模型参数。 ### 1. 成本函数的定义 在机器学习中,成本函数(也称为损失函数或目标函数)是一个将模型预测值与真实值之间的差异量化为单个实值的函数。给定一个模型 $f(\mathbf{x}; \theta)$,其中 $\theta$ 表示模型参数,成本函数 $J(\theta)$ 定义为: $$J(\theta) = \frac{1}{m} \sum_{i=1}^{m} L\left(y^{(i)}, f(\mathbf{x}^{(i)}; \theta)\right)$$ 其中: - $m$ 是训练样本数量 - $L$ 是损失函数,计算单个样本的误差 - $y^{(i)}$ 是第 $i$ 个样本的真实标签 - $\mathbf{x}^{(i)}$ 是第 $i$ 个样本的特征向量 ### 2. 常见的损失函数 #### 2.1 回归问题 - **均方误差(MSE)**: $$L(y, \hat{y}) = (y - \hat{y})^2$$ 对应的成本函数为: $$J(\theta) = \frac{1}{m} \sum_{i=1}^{m} \left(y^{(i)} - f(\mathbf{x}^{(i)}; \theta)\right)^2$$ - **平均绝对误差(MAE)**: $$L(y, \hat{y}) = |y - \hat{y}|$$ $$J(\theta) = \frac{1}{m} \sum_{i=1}^{m} \left| y^{(i)} - f(\mathbf{x}^{(i)}; \theta) \right|$$ #### 2.2 分类问题 - **交叉熵损失(Cross-Entropy Loss)**: 对于二分类问题(使用sigmoid激活函数): $$L(y, \hat{y}) = - \left[ y \log(\hat{y}) + (1 - y) \log(1 - \hat{y}) \right]$$ 其中 $\hat{y}$ 是模型预测的概率值($0 \leq \hat{y} \leq 1$)。 对于多分类问题(使用softmax激活函数): $$L(y, \hat{y}) = - \sum_{c=1}^{C} y_c \log(\hat{y}_c)$$ 其中 $C$ 是类别数,$y_c$ 是真实标签的one-hot表示(如果样本属于类别 $c$ 则为1,否则为0),$\hat{y}_c$ 是模型预测样本属于类别 $c$ 的概率。 - **铰链损失(Hinge Loss)**: 常用于支持向量机(SVM): $$L(y, \hat{y}) = \max(0, 1 - y \cdot \hat{y})$$ 其中 $y \in \{-1, 1\}$,$\hat{y}$ 是模型预测的未经过概率化的分数(即决策函数的输出)。 ### 3. 成本函数在优化中的作用 在模型训练过程中,优化算法(如梯度下降)通过计算成本函数关于参数 $\theta$ 的梯度,并沿梯度反方向更新参数,以最小化成本函数: $$\theta := \theta - \alpha \nabla_\theta J(\theta)$$ 其中 $\alpha$ 是学习率。 ### 4. 正则化 为了防止过拟合,常在成本函数中加入正则化项: - **L2正则化(Ridge)**: $$J(\theta) = J_0(\theta) + \lambda \sum_{j=1}^{n} \theta_j^2$$ 其中 $J_0(\theta)$ 是原始成本函数,$\lambda$ 是正则化系数。 - **L1正则化(Lasso)**: $$J(\theta) = J_0(\theta) + \lambda \sum_{j=1}^{n} |\theta_j|$$ ### 5. 代码示例:线性回归的成本函数(MSE) 以下是一个使用均方误差作为成本函数的线性回归示例(Python): ```python import numpy as np # 定义均方误差成本函数 def cost_function(X, y, theta): m = len(y) predictions = X.dot(theta) # 计算预测值 error = predictions - y cost = (1/(2*m)) * np.sum(error**2) # MSE (注意这里乘以1/(2m)是为了后续梯度计算方便,不影响优化结果) return cost # 梯度下降 def gradient_descent(X, y, theta, alpha, iterations): m = len(y) cost_history = np.zeros(iterations) for i in range(iterations): predictions = X.dot(theta) errors = predictions - y # 计算梯度 gradient = (1/m) * X.T.dot(errors) # 更新参数 theta = theta - alpha * gradient cost_history[i] = cost_function(X, y, theta) return theta, cost_history # 示例数据 X = np.array([[1, 1], [1, 2], [1, 3]]) # 添加偏置列 y = np.array([1, 2, 3]) theta = np.array([0, 0]) # 初始参数 # 运行梯度下降 theta_opt, cost_hist = gradient_descent(X, y, theta, alpha=0.1, iterations=100) print("优化后的参数:", theta_opt) ``` ### 6. 成本函数的选择 选择成本函数时需考虑: - 问题类型(回归、分类) - 模型类型(线性模型、神经网络等) - 异常值敏感性(如MSE对异常值敏感,MAE相对鲁棒) - 优化难度(如交叉熵损失在逻辑回归中通常比平方误差更有效) ### 7. 总结 成本函数是机器学习模型训练的核心,它指导模型参数的学习方向。理解不同成本函数的特性以及它们在优化过程中的行为,对于构建高效、准确的机器学习模型至关重要。 §§ 相关问题 §§ 1. 如何选择适合的损失函数? 2. 梯度下降算法有哪些变体? 3. 正则化是如何防止过拟合的? 4. 成本函数与模型评估指标有何区别?</think>### 成本函数(Cost Function)详解 成本函数(也称损失函数)是机器学习和优化问题的核心概念,用于量化模型预测值与真实值之间的误差。其数学定义为: $$J(\theta) = \frac{1}{m} \sum_{i=1}^{m} L(h_\theta(x^{(i)}), y^{(i)})$$ 其中: - $J(\theta)$ 是参数 $\theta$ 的成本函数 - $m$ 是训练样本数量 - $h_\theta(x)$ 是模型预测函数 - $L$ 是单个样本的损失函数 #### 一、常见损失函数类型 1. **回归问题**: - **均方误差(MSE)**: $$L(y, \hat{y}) = (y - \hat{y})^2$$ 梯度计算:$\frac{\partial J}{\partial \theta_j} = \frac{2}{m} \sum (\hat{y}_i - y_i)x_j^{(i)}$ - **平均绝对误差(MAE)**: $$L(y, \hat{y}) = |y - \hat{y}|$$ 2. **分类问题**: - **交叉熵损失(Cross-Entropy)**: $$L(y, \hat{y}) = -[y \log(\hat{y}) + (1-y)\log(1-\hat{y})]$$ 适用于逻辑回归和神经网络 - **铰链损失(Hinge Loss)**: $$L(y, \hat{y}) = \max(0, 1 - y \cdot \hat{y})$$ 用于支持向量机(SVM) 3. **正则化项**(防止过拟合): - L2正则化:$\frac{\lambda}{2m} \sum_{j=1}^{n} \theta_j^2$ - L1正则化:$\frac{\lambda}{m} \sum_{j=1}^{n} |\theta_j|$ #### 二、优化方法 通过梯度下降最小化成本函数: $$\theta_j := \theta_j - \alpha \frac{\partial J(\theta)}{\partial \theta_j}$$ 其中 $\alpha$ 为学习率,迭代更新直至收敛。 **代码示例**(线性回归的MSE实现): ```python import numpy as np def cost_function(X, y, theta): """ 计算均方误差成本函数 X: 特征矩阵 (m x n) y: 标签向量 (m x 1) theta: 参数向量 (n x 1) """ m = len(y) predictions = X.dot(theta) error = predictions - y cost = (1/(2*m)) * np.sum(error**2) # MSE公式 return cost def gradient_descent(X, y, theta, alpha, iterations): """ 梯度下降优化 alpha: 学习率 iterations: 迭代次数 """ m = len(y) cost_history = [] for _ in range(iterations): predictions = X.dot(theta) errors = predictions - y gradient = (1/m) * X.T.dot(errors) theta = theta - alpha * gradient cost_history.append(cost_function(X, y, theta)) return theta, cost_history ``` #### 三、关键特性 1. **凸函数性质**:如MSE是凸函数,保证梯度下降能找到全局最优解 2. **损失函数选择依据**: - MSE对异常值敏感 - MAE更鲁棒但梯度不平滑 - 交叉熵提供概率解释 3. **正则化影响**: - L2(岭回归)使参数趋近于零但不为零 - L1(Lasso)可产生稀疏解(部分参数归零) #### 四、应用场景 1. 模型训练:指导参数更新方向 2. 模型诊断:过拟合时训练损失<<验证损失 3. 超参数调优:如正则化系数 $\lambda$ 的选择 > 机器学习模型通过最小化成本函数来学习数据中的模式,这是监督学习的核心机制[^1]。成本函数的设计直接影响模型性能和泛化能力,需结合具体任务特性选择[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值