梯度下降算法

批量梯度下降,随机梯度下降,微批量梯度下降

Posted by Felix on December 1, 2017
//##############################################################
//#
//# 批量梯度下降算法实例:求解线性回归问题
//#
//##############################################################
#include <iostream>
using namespace std;
#define n_samples 4
#define n_features 2
int main()\{
    //自变量 x1, x2
    float mat[n_samples][n_features]=\{\{1,4}, \{2,5}, \{5,1}, \{4,2}};
    //因变量,y
    float results[n_samples] = \{19, 26, 19, 20};
    //权重,weights, 初始值设为
    float weights[n_features] = \{0, 0};
    //损失函数值
    float loss = 1000;
    //学习率
    float leanring_rate = 0.01;
    //迭代500次或损失收敛
    for(int i=0; i<500 && loss > 0.001; ++i)\{
        //梯度
        float err_sum[n_features]=\{0.0, 0.0};
        //遍历样本
        for(int j=0; j < n_samples; ++j)\{
            //wx
            float h = 0.0;
            //遍历特征
            for(int k=0; k < n_features; ++k)\{
                h+=mat[j][k]*weights[k];
            }
            //计算梯度
            for(int k=0; k < n_features; ++k)\{
                err_sum[k] += (results[j] -h ) * mat[j][k];
            }
        }
        //更新权重
        for(int k=0; k < n_features; ++k)\{
            weights[k] += err_sum[k] * leanring_rate;
        }
        cout<<"weights-->"<<weights[0]<<","<<weights[1]<<endl;
        //更新损失
        for(int j=0; j < n_samples; ++j)\{
            float h = 0;
            for(int k = 0; k < n_features; ++k)\{
                h += mat[j][k] * weights[k];
            }
            loss += (results[j]-h)*(results[j]-h);
        }
    }
    return 0;
}

flow
st=>start: Start
op=>operation: Your Operation
cond=>condition: Yes or No?
e=>end
st->op->cond
cond(yes)->e
cond(no)->op

本文首次发布于 Felix Blog, 作者 @longwind09(Felix) ,转载请保留原文链接.