无约束优化方法
无约束优化从目标梯度构造搜索方向,再决定步长。随机一阶方法适合小批量目标,牛顿与拟牛顿方法利用曲率改善确定性平滑目标的局部求解。
2. 基础与定义
Section titled “2. 基础与定义”2.1 目标与方向
Section titled “2.1 目标与方向”设 ,。迭代写为 ; 表示局部下降方向。
3. 核心原理
Section titled “3. 核心原理”3.1 梯度与动量
Section titled “3.1 梯度与动量”梯度下降取 。动量累积历史方向:
对 光滑目标, 可使精确梯度步满足相应下降界;随机梯度还涉及估计噪声。
3.2 自适应梯度
Section titled “3.2 自适应梯度”Adam 维护一阶矩与二阶矩:
偏差修正为 、,逐元素更新
AdamW 额外施加独立衰减 ,使衰减项与自适应梯度缩放分开。
3.3 牛顿与拟牛顿
Section titled “3.3 牛顿与拟牛顿”牛顿方向解 ;海塞正定时为下降方向。BFGS 用 与 更新曲率近似。L-BFGS 保存有限历史,通过双循环计算近似逆海塞与梯度的乘积。
梯度下降界
光滑性给出 。代入 得 。强凸性进一步把梯度范数与最优误差联系起来,得到线性收敛界。
4. 实现与应用
Section titled “4. 实现与应用”4.1 更新实现
Section titled “4.1 更新实现”一阶更新核
输入向量尺寸一致,矩估计初始化为零,AdamW 的步数从一开始。以下核函数由调用方提供梯度与学习率。
#include <vector>void update_SGD(std::vector<double>& theta, const std::vector<double>& grad, double lr) { for (std::size_t i = 0; i < theta.size(); ++i) theta[i] -= lr * grad[i];}#include <vector>#include <cmath>void update_Momentum(std::vector<double>& theta, const std::vector<double>& grad, std::vector<double>& velocity, double lr, double gamma = 0.9) { for (std::size_t i = 0; i < theta.size(); ++i) { velocity[i] = gamma * velocity[i] + lr * grad[i]; theta[i] -= velocity[i]; }}#include <vector>#include <cmath>void update_RMSProp(std::vector<double>& theta, const std::vector<double>& grad, std::vector<double>& v, double lr, double beta = 0.9, double epsilon = 1e-8) { for (std::size_t i = 0; i < theta.size(); ++i) { v[i] = beta * v[i] + (1.0 - beta) * grad[i] * grad[i]; theta[i] -= (lr / (std::sqrt(v[i]) + epsilon)) * grad[i]; }}#include <vector>#include <cmath>void update_AdamW(std::vector<double>& theta, const std::vector<double>& grad, std::vector<double>& m, std::vector<double>& v, int t, double lr, double weight_decay, double beta1 = 0.9, double beta2 = 0.999, double epsilon = 1e-8) { for (std::size_t i = 0; i < theta.size(); ++i) { m[i] = beta1 * m[i] + (1.0 - beta1) * grad[i]; v[i] = beta2 * v[i] + (1.0 - beta2) * grad[i] * grad[i]; double m_hat = m[i] / (1.0 - std::pow(beta1, t)); double v_hat = v[i] / (1.0 - std::pow(beta2, t)); theta[i] -= lr * weight_decay * theta[i]; theta[i] -= lr * m_hat / (std::sqrt(v_hat) + epsilon); }}L-BFGS 双循环
历史按时间顺序存储,保留正曲率样本。返回近似逆海塞与梯度的乘积,搜索方向取其负值;步长由线搜索确定。
#include <vector>#include <cmath>#include <stdexcept>double dot_product(const std::vector<double>& a, const std::vector<double>& b) { if (a.size() != b.size()) throw std::invalid_argument("dimension mismatch"); double result = 0; for (std::size_t i = 0; i < a.size(); ++i) result += a[i] * b[i]; return result;}
std::vector<double> two_loop_recursion( std::vector<double> q, const std::vector<std::vector<double>>& s_hist, const std::vector<std::vector<double>>& y_hist) { if (s_hist.size() != y_hist.size()) throw std::invalid_argument("history mismatch"); int m = s_hist.size(); if (m == 0) return q; for (int i = 0; i < m; ++i) { if (s_hist[i].size() != q.size() || y_hist[i].size() != q.size()) throw std::invalid_argument("dimension mismatch"); double curvature = dot_product(y_hist[i], s_hist[i]); if (!std::isfinite(curvature) || curvature <= 0) throw std::invalid_argument("nonpositive curvature"); } std::vector<double> alpha(m, 0.0), rho(m, 0.0);
for (int i = m - 1; i >= 0; --i) { rho[i] = 1.0 / dot_product(y_hist[i], s_hist[i]); alpha[i] = rho[i] * dot_product(s_hist[i], q); for (std::size_t j = 0; j < q.size(); ++j) q[j] -= alpha[i] * y_hist[i][j]; }
double gamma = dot_product(s_hist.back(), y_hist.back()) / dot_product(y_hist.back(), y_hist.back()); std::vector<double> r = q; for (std::size_t j = 0; j < r.size(); ++j) r[j] *= gamma;
for (int i = 0; i < m; ++i) { double beta = rho[i] * dot_product(y_hist[i], r); for (std::size_t j = 0; j < r.size(); ++j) r[j] += s_hist[i][j] * (alpha[i] - beta); } return r;}5. 分析与总结
Section titled “5. 分析与总结”条件数影响不同方向的收敛速度。曲率近似改善尺度,但需要可靠的梯度差;随机梯度、裁剪和正则化都会改变实际更新。
6. 关联与参考
Section titled “6. 关联与参考”Boyd 与 Vandenberghe:Convex Optimization,第 9 章。
关联:数值优化。