xxxx18一60岁hd中国/日韩女同互慰一区二区/西西人体扒开双腿无遮挡/日韩欧美黄色一级片 - 色护士精品影院www

資源簡(jiǎn)介

利用梯度下降算法,進(jìn)行機(jī)器學(xué)習(xí).利用C++實(shí)現(xiàn)下降算法.

資源截圖

代碼片段和文件信息

#include?
#include?
#include?
#include?
using?namespace?std;

//Term存放每條數(shù)據(jù)flag為True表示1false表示-1
struct?Term
{
bool?flag;
map?pairs;
double?score;
};

//損失函數(shù)
int?lostFunction(Term[]?int?map);
//進(jìn)行梯度下降的迭代過(guò)程
void?doGradient(int?T?int?sampleNum?Term[]);
//得到梯度的值
void?changeGradient(Term[]?int?map*);
//給每條數(shù)據(jù)打分如果分?jǐn)?shù)大于0那么認(rèn)為是+1的;小于0認(rèn)為是-1的
double?getScore(Term?map*);
void?testTestData(map);
void?inputData(char*?int?Term[]);
void?getTerm(string?Term*);
void?getKeyValue(string?int*?double*);

map?weight;

int?main()
{
Term?trainData[2000];
cout?< inputData(“train.dat“?2000?trainData);

cout?< for?(int?i=1;?i<10000;?i++)
weight[i]?=?1.0;

cout?< doGradient(25?2000?trainData);

cout?< testTestData(weight);
//從結(jié)果看有overfitting的存在
//該梯度算法實(shí)現(xiàn)中沒(méi)有加入Regularization一項(xiàng)

return?0;
}

void?inputData(char*?file?int?n?Term?terms[])
{
ifstream?infile;
string?s;
Term?term;
char?cs[7000];
infile.open(file);
for?(int?i=0;?i {
infile.getline(cs?7000);
if?(cs[0]?==?‘#‘)
{
i--;
continue;
}
s?=?cs;
terms[i].pairs.clear();
getTerm(s?&terms[i]);
}
infile.close();
}

void?doGradient(int?T?int?sampleNum?Term?terms[])
{
map?gradient;
map?tempWeight;
int?error?=?0;
bool?goOn?=?false;

error?=?lostFunction(terms?sampleNum?weight);
for?(int?t=0;?t {
//求得梯度
gradient.clear();
changeGradient(terms?sampleNum?&gradient);

goOn?=?false;
double?alpha?=?4.5;
//不斷變小步長(zhǎng)直到小于.000001為止求得合理的步長(zhǎng)
while?(alpha?>?0.0000001)
{
map::iterator?it?=?weight.begin();
for?(;?it!=weight.end();?it++)
tempWeight[it->first]?=?it->second?+?alpha?*?gradient[it->first];
int?error1?=?lostFunction(terms?sampleNum?tempWeight);
if?(error1? {
cout?< cout?< cout?< weight?=?tempWeight;
error?=?error1;
goOn?=?true;
break;
}
alpha?/=?2.0;
}
if?(!goOn)
break;
cout?< }
}

int?lostFunction(Term?terms[]?int?sampleNum?map?w)
{
int?wrong?=?0;
for?(int?i=0;?i {
terms[i].score?=?getScore(terms[i]?&w);
if?(terms[i].flag?&&?terms[i].score<0)
{
wrong++;
}
else?if?(terms[i].flag==false?&&?terms[i].score>0)
{
wrong++;
}
}
return?wrong;
}

void?changeGradient(Term?terms[]?int?sampleNum?map*?g)
{
for?(int?i=0;?i {
map::iterator?it?=?terms[i].pairs.begin();
for?(;?it!=terms[i].pairs.end();?it++)
{
//根據(jù)梯度下降的公式應(yīng)該是(*g)[it->first]+=it-second
//由于it->second都是值比較小的數(shù)導(dǎo)致梯度下降不明顯
//所以這里使用?+=和-=?

?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----

?????文件???????4671??2011-06-16?16:39??機(jī)器學(xué)習(xí)_梯度下降算法實(shí)現(xiàn)\gradient.cpp

?????文件?????536475??1997-11-17?18:11??機(jī)器學(xué)習(xí)_梯度下降算法實(shí)現(xiàn)\test.dat

?????文件????2154343??1997-11-17?18:12??機(jī)器學(xué)習(xí)_梯度下降算法實(shí)現(xiàn)\train.dat

?????文件???????1869??2011-06-16?17:22??機(jī)器學(xué)習(xí)_梯度下降算法實(shí)現(xiàn)\實(shí)驗(yàn)報(bào)告.txt

?????目錄??????????0??2011-06-16?17:22??機(jī)器學(xué)習(xí)_梯度下降算法實(shí)現(xiàn)

-----------?---------??----------?-----??----

??????????????2697358????????????????????5


評(píng)論

共有 條評(píng)論

相關(guān)資源