使用梯度下降找到二维函数的局部最小值

时间:2019-07-15 10:52:23

标签: gradient-descent

我是机器学习的新手,我尝试使用2D函数的梯度下降来找到局部最小值。但是我使用梯度下降找到的值与我期望的值不同。我使用起点将其接近我期望的值,但梯度算法找到了另一个值。我执行算法错误吗?

Point gradient_descent(double dx, double dy , double error, double gamma, unsigned int max_iters) {

double c_error_x = error + 1;
double c_error_y = error + 1;

unsigned int iters = 0;
double p_error_x;
double p_error_y;

std::ofstream writeFile("D:\\GradientDescent.csv");
writeFile << "dx,dy,c_error_x,c_error_y,E(dx;dy)\n";

while ((error < c_error_x && error< c_error_y) && iters < max_iters){

    p_error_x = dx;
    p_error_y = dy;
    dx -= dfx(p_error_x, p_error_y) * gamma;
    dy -= dfy(p_error_x, p_error_y) * gamma;
    c_error_x = abs_val(p_error_x - dx);
    c_error_y = abs_val(p_error_y - dy);

    printf("\nc_error x= %f\n", c_error_x);
    printf("\nc_error y= %f\n", c_error_y);
    printf("\n==================================\n");
    printf("\n E(dx,dy)= %f\n", cost_func(dx, dy));
    printf("\n==================================\n");
    writeFile << dx << "," << dy << "," << c_error_x << "," << c_error_y << "," << cost_func(dx, dy) << std::endl;

    iters++;
}

writeFile.close();
return Point(dx,dy);

}

int main(){

//double dx=Sax, dy=Sby;
double dx =1.6 , dy =0.04;
double error =1e-7;

double gamma = 1e-7;
unsigned int max_iters = 10000;
Point r = gradient_descent(dx,dy, error, gamma, max_iters);
printf("\nThe local minimum is: %f , %f\n",r.x,r.y);
std::cout << "\n E= " << cost_func(r.x, r.y) << std::endl;

printf("\nThe local minimum solver: %f , %f\n", Sax, Sby);
std::cout << "\n E= " << cost_func(Sax, Sby) << std::endl;

return 0;

}

0 个答案:

没有答案