为了测试 Adam、RMSProp 和 Adagrad 算法的性能,你可以使用四个凸函数进行实验。以下是一些常用的凸函数示例:
Rosenbrock 函数:
Booth 函数:
Himmelblau 函数:
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;
}