layout: post
title: 梯度下降算法
subtitle: 批量梯度下降求解线下回归问题
date: 2017-12-01
author: Felix
catalog: true
tags:
- 机器学习
- 优化算法
---
//##############################################################
//#
//# 批量梯度下降算法实例:求解线性回归问题
//#
//##############################################################
#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;
}
时间: 2024-11-08 14:34:05