2018.08.28 ali 梯度下降法实现最小二乘
Posted ranjiewen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018.08.28 ali 梯度下降法实现最小二乘相关的知识,希望对你有一定的参考价值。
- 要理解梯度下降和牛顿迭代法的区别
#include<stdio.h> // 1. 线性多维函数原型是 y = f(x1,x2,x3) = a * x1 + b * x2 + c * x3 // // 2. 用牛顿迭代法(或者梯度下降) 对 f(x1, x2, x3) 做求解。 // 即找到一组[a,b,c], 使得: argMin E[(f(x1,x2,x3) - y)^2]。 其中E为sigma。这个loss func就是最小二乘法 int main() { float arr_x1[50] = {0.21181724817,0.860486757342,0.956633419758,0.85786906396,0.404354018005,0.738141237944,0.335462707932,0.855141891858,0.176675723263,0.907416965072,0.0647332687491,0.915688359157,0.388378883996,0.551590696253,0.252475234136,0.324609487448,0.591531727445,0.759460513689,0.455169604119,0.0114764793446,0.616362135632,0.520994743026,0.763576588722,0.252922367859,0.818731143784,0.201276597938,0.409109798669,0.837302929696,0.540137128835,0.640703160538,0.261071730719,0.60358418799,0.030230766662,0.375696128756,0.617101348229,0.99638041503,0.453397032368,0.697677962738,0.242718172939,0.694031285076,0.884499352369,0.1246164663,0.906583423555,0.610205156141,0.285249324615,0.443180950782,0.596761799846,0.634037833318,0.719341876615,0.899475824374}; float arr_x2[50] = {0.949950993199,0.00348950593906,0.0995940827871,0.181585781352,0.245298785684,0.829835408054,0.46656568642,0.442514659946,0.218085519565,0.175017901553,0.137705361599,0.312323402349,0.570600016776,0.731451385259,0.469180900687,0.91530892778,0.153035033536,0.346216813834,0.469174138374,0.79429539697,0.0755359387891,0.461955557514,0.914175817629,0.208472593729,0.468003630737,0.626639369858,0.253670292305,0.7493809477,0.322969279989,0.0305604977161,0.414165510912,0.450261998842,0.332274387177,0.560607807568,0.947039157859,0.366802949708,0.961887248758,0.13159797726,0.16114436169,0.552001416778,0.820164333967,0.354051649783,0.203087374238,0.573542775689,0.763082181161,0.47376657072,0.727405079739,0.967446971274,0.00134569831871,0.345040334393}; float arr_x3[50] = {0.66411293783,0.0107546377332,0.0548870004442,0.720490687668,0.571440908761,0.433915862985,0.884647342299,0.639299914284,0.603025573277,0.419795660433,0.153465995146,0.503899737771,0.286040546608,0.940436883787,0.651074548109,0.745058907863,0.0356272620881,0.894616931387,0.726118880129,0.518993827948,0.786363964595,0.222242295981,0.0338350110881,0.448694787879,0.472714446765,0.266573405515,0.754216764327,0.333970079798,0.950908128673,0.433706942368,0.650219610743,0.0619899568179,0.20517216498,0.183507724831,0.179897813532,0.88001043946,0.935991402921,0.929004108888,0.700231732841,0.326770142533,0.516633979936,0.685462636021,0.88460477795,0.8850575409,0.0368877767664,0.0791417202543,0.970113022457,0.158824081205,0.318974735222,0.504788407121}; float arr_y[50] = {3.19986397889,2.59951370428,3.12490306905,3.65818074077,2.27578741609,4.30852373796,2.8246394015,4.09022421593,1.56985189029,3.4930805541,0.624040036155,3.87635573087,2.59304482554,4.05881562909,2.34757909085,3.54990861588,2.11692801283,3.86581396674,3.03088945846,2.14247671464,2.78700502684,2.70984886589,4.15389074944,1.62535051756,3.8650212131,2.12406010007,2.48954342068,4.3446533577,3.2173108431,2.41765575668,2.26257330317,2.77411897078,0.960613946438,2.43186832632,3.92609440302,4.60315121046,4.21998299775,3.28535511407,1.75098266657,3.51380638512,4.81098457321,1.76833993158,4.0108419056,3.86281812345,2.4193713593,2.35659224015,4.21597033336,3.99665270207,2.48028794333,3.89358362917}; double a=1.0,b=1.0,c=1.0; double lr=0.0005; int i,j=0; while(j<1e6) { double temp=0.0,sum=0.0; double da=0.0,db=0.0,dc=0.0; for( i=0;i<50;i++) { temp=a*arr_x1[i]+b*arr_x2[i]+c*arr_x3[i]-arr_y[i]; sum+=temp*temp; da+=temp*2*arr_x1[i]; db+=temp*2*arr_x2[i]; dc+=temp*2*arr_x3[i]; } a=a-lr*da; b=b-lr*db; c=c-lr*dc; j++; if(j%100==0) printf("iter: %d ;sum :%f ",j,sum); } printf("%f,%f,%f ",a,b,c); }
- 第一题就二维网格上A->B的路径数量,其中有mask限制!
以上是关于2018.08.28 ali 梯度下降法实现最小二乘的主要内容,如果未能解决你的问题,请参考以下文章
最小二乘法+牛顿法+拟牛顿法+梯度下降法+梯度上升法+共轭梯度法