当前位置: 首页 > news >正文

用c++用4个凸函数(觉得啥好用用啥)去测试adam,rmsprop,adagrad算法的性能(谁先找到最优点)

为了测试 Adam、RMSProp 和 Adagrad 算法的性能,你可以使用四个凸函数进行实验。以下是一些常用的凸函数示例:

  1. Rosenbrock 函数:

    Rosenbrock函数

  2. Booth 函数:

    Booth函数

  3. Himmelblau 函数:

    Himmelblau函数

  4. Beale 函数:

    Beale函数

你可以选择其中一个或多个函数来测试算法的性能。对于每个函数,你可以使用不同的初始点,并应用 Adam、RMSProp 和 Adagrad 算法来寻找最优点。最优点可以通过达到较低的函数值或满足预定精度条件来定义。

在实验过程中,你可以记录每个算法在不同函数和初始点上找到最优点的迭代次数、计算时间以及最终的函数值。通过比较这些指标,你可以评估每个算法的性能和效果。

请注意,算法的性能可能会因函数的形状和参数设置而有所不同。因此,建议你在不同的凸函数上进行多次实验,以获得更全面的性能比较结果。

#include <iostream>
#include <cmath>
#include <vector>// 定义凸函数类
class ConvexFunction {
public:virtual double evaluate(const std::vector<double>& x) = 0;
};// Rosenbrock 函数
class RosenbrockFunction : public ConvexFunction {
public:double evaluate(const std::vector<double>& x) override {double sum = 0.0;for (size_t i = 0; i < x.size() - 1; ++i) {double term1 = pow(x[i + 1] - pow(x[i], 2), 2);double term2 = pow(1 - x[i], 2);sum += 100 * term1 + term2;}return sum;}
};// Booth 函数
class BoothFunction : public ConvexFunction {
public:double evaluate(const std::vector<double>& x) override {double term1 = pow(x[0] + 2 * x[1] - 7, 2);double term2 = pow(2 * x[0] + x[1] - 5, 2);return term1 + term2;}
};// Himmelblau 函数
class HimmelblauFunction : public ConvexFunction {
public:double evaluate(const std::vector<double>& x) override {double term1 = pow(pow(x[0], 2) + x[1] - 11, 2);double term2 = pow(x[0] + pow(x[1], 2) - 7, 2);return term1 + term2;}
};// Beale 函数
class BealeFunction : public ConvexFunction {
public:double evaluate(const std::vector<double>& x) override {double term1 = pow(1.5 - x[0] + x[0] * x[1], 2);double term2 = pow(2.25 - x[0] + x[0] * pow(x[1], 2), 2);double term3 = pow(2.625 - x[0] + x[0] * pow(x[1], 3), 2);return term1 + term2 + term3;}
};// Adam 算法
std::vector<double> adam(const ConvexFunction& func, const std::vector<double>& initial_x, double learning_rate, int max_iterations) {std::vector<double> x = initial_x;std::vector<double> m(x.size(), 0.0);std::vector<double> v(x.size(), 0.0);double beta1 = 0.9;double beta2 = 0.999;double epsilon = 1e-8;for (int i = 0; i < max_iterations; ++i) {// 计算梯度std::vector<double> gradient(x.size(), 0.0);for (size_t j = 0; j < x.size(); ++j) {std::vector<double> x_plus_delta = x;x_plus_delta[j] += epsilon;double f_plus_delta = func.evaluate(x_plus_delta);gradient[j] = (f_plus_delta - func.evaluate(x)) / epsilon;}// 更新参数for (size_t j = 0; j < x.size(); ++j) {m[j] = beta1 * m[j] + (1 - beta1) * gradient[j];v[j] = beta2 * v[j] + (1 - beta2) * pow(gradient[j], 2);double m_hat = m[j] / (1 - pow(beta1, i + 1));double v_hat = v[j] / (1 - pow(beta2, i + 1));x[j] -= learning_rate * m_hat / (sqrt(v_hat) + epsilon);}}return x;
}// RMSProp 算法
std::vector<double> rmsprop(const ConvexFunction& func, const std::vector<double>& initial_x, double learning_rate, double decay_rate, int max_iterations) {std::vector<double> x = initial_x;std::vector<double> cache(x.size(), 0.0);double epsilon = 1e-8;for (int i = 0; i < max_iterations; ++i) {// 计算梯度std::vector<double> gradient(x.size(), 0.0);for (size_t j = 0; j < x.size(); ++j) {std::vector<double> x_plus_delta = x;x_plus_delta[j] += epsilon;double f_plus_delta = func.evaluate(x_plus_delta);gradient[j] = (f_plus_delta - func.evaluate(x)) / epsilon;}// 更新参数for (size_t j = 0; j < x.size(); ++j) {cache[j] = decay_rate * cache[j] + (1 - decay_rate) * pow(gradient[j], 2);x[j] -= learning_rate * gradient[j] / (sqrt(cache[j]) + epsilon);}}return x;
}// Adagrad 算法
std::vector<double> adagrad(const ConvexFunction& func, const std::vector<double>& initial_x, double learning_rate, int max_iterations) {std::vector<double> x = initial_x;std::vector<double> cache(x.size(), 0.0);double epsilon = 1e-8;for (int i = 0; i < max_iterations; ++i) {// 计算梯度std::vector<double> gradient(x.size(), 0.0);for (size_t j = 0; j < x.size(); ++j) {std::vector<double> x_plus_delta = x;x_plus_delta[j] += epsilon;double f_plus_delta = func.evaluate(x_plus_delta);gradient[j] = (f_plus_delta - func.evaluate(x)) / epsilon;}// 更新参数for (size_t j = 0; j < x.size(); ++j) {cache[j] += pow(gradient[j], 2);x[j] -= learning_rate * gradient[j] / (sqrt(cache[j]) + epsilon);}}return x;
}int main() {// 创建凸函数对象RosenbrockFunction rosenbrock;BoothFunction booth;HimmelblauFunction himmelblau;BealeFunction beale;// 设置算法参数double learning_rate = 0.01;double decay_rate = 0.9;int max_iterations = 1000;// 初始化初始点std::vector<double> initial_x = { 0.0, 0.0 };// 使用 Adam 算法找到最优点std::vector<double> adam_result = adam(rosenbrock, initial_x, learning_rate, max_iterations);std::cout << "Adam Result: (" << adam_result[0] << ", " << adam_result[1] << ")" << std::endl;// 使用 RMSProp 算法找到最优点std::vector<double> rmsprop_result = rmsprop(rosenbrock, initial_x, learning_rate, decay_rate, max_iterations);std::cout << "RMSProp Result: (" << rmsprop_result[0] << ", " << rmsprop_result[1] << ")" << std::endl;// 使用 Adagrad 算法找到最优点std::vector<double> adagrad_result = adagrad(rosenbrock, initial_x, learning_rate, max_iterations);std::cout << "Adagrad Result: (" << adagrad_result[0] << ", " << adagrad_result[1] << ")" << std::endl;return 0;
}

http://www.lryc.cn/news/354174.html

相关文章:

  • AJAX初级
  • 重载大于号运算符,比较复数大小
  • go ast语义分析实现指标计算器
  • 【Vue】组件间传参与方法调用
  • 类和对象2
  • Linux系统命令traceroute详解(语法、选项、原理和实例)
  • 中兴通讯助力中国移动,推动SPN AI节能技术于23省规模部署
  • SQL Server--死锁
  • 中科蓝讯AB32VG1中文寄存器说明GPIO端口操作
  • 如何查看热门GPT应用?
  • C++中的各种定义
  • Java面向对象-常用类(日期时间类)
  • Shell环境变量深入:自定义系统环境变量
  • 【C++课程学习】:命名空间的理解(图文详解)
  • 鸿蒙ArkUI-X平台差异化:【运行态差异化(@ohos.deviceInfo)】
  • 蓝牙Mesh模块组网时无线回程影响速率吗?
  • 将3D检测的box框投影到BEV图片上
  • Flutter 中的 ClipOval 小部件:全面指南
  • ubuntu 硬盘转移
  • three.js中使用CameraHelper来可视化调整阴影相机的范围
  • Golang发送GET请求并设置查询参数
  • c++笔记3
  • 唠唠叨叨,每日进度
  • Vulhub——CAS 4.1、AppWeb、apisix
  • Python Beautiful Soup 使用详解
  • Java进阶学习笔记29——Math、System、Runtime
  • TOTP 算法实现:双因素认证的基石(C/C++代码实现)
  • aws eks理解和使用podidentity为pod授权
  • 面向可复用性和可维护性的设计模式 课程学习总结
  • 修复谷歌 AdSense 的 Ads.Txt 无效的有收益损失风险提示