機器學習關於固定學習率迭代的C++代碼

// 機器學習學習率問題.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <iostream>
#include <math.h>

using namespace std;

double f(double x)    //y
{
    return x*x*x*x;
    return x*x;
    return -cos(x);
}

double g(double x)    //y'
{
    return 4 * x*x*x;
    return 2 * x;
    return sin(x);
}

double g2(double x)    //y''
{
    return 12 * x*x;
    return 2;
    return cos(x);
}

double GetA_Armijo(double x, double d, double a)
{
    double c1 = 0.3;
    double now = f(x);
    double next = f(x - a*d);

    int count = 30;
    while (next < now)
    {
        a *= 2;
        next = f(x - a*d);
        count--;
        if (count == 0)
            break;
    }

    count = 50;
    while (next > now - c1*a*d*d)
    {
        a /= 2;
        next = f(x - a*d);
        count--;
        if (count == 0)
            break;
    }
    return a;
}

double GetA_Quad(double x, double d, double a)
{
    double c1 = 0.3;
    double now = f(x);
    double next = f(x - a*d);

    int count = 30;
    while (next < now)
    {
        a *= 2;
        next = f(x - a*d);
        count--;
        if (count == 0)
            break;
    }

    count = 50;
    double b;
    while (next > now - c1*a*d*d)
    {
        b = d * a * a / (now + d * a - next);
        b /= 2;
        if (b < 0)
            a /= 2;
        else
            a = b;
        next = f(x - a*d);
        count--;
        if (count == 0)
            break;
    }
    return a;
}


int _tmain(int argc, _TCHAR* argv[])
{
    double x = 1.5;
    double d;            //一階導
    double a = 0.01;    //學習率
    cout << "次數" << '\t' << "學習率" << '\t' << "x值" << '\n';
    for (int i = 0; i < 100; i++)//迭代100次
    {
        d = g(x);//導數
        a = 1 / g2(x);//GetA_Armijo(x, d, 10);
        x -= d * a;
        
        cout << i << '\t' << a << '\t' << x << '\n';
    }
    return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章