Machine Learning系列实验--梯度下降(批量梯度,随机梯度)

看了3集斯坦福大学的机器学习课程,很有意思,开始着手回顾以及一些代码的实现,这次做的是批量梯度下降以及随机梯度下降。讲义在 http://cs229.stanford.edu/notes/cs229-notes1.pdf
1)批量梯度下降:
 
2)随机梯度下降:
 

实验内容借鉴了pennyliang 在CSDN上的博文http://blog.csdn.net/pennyliang/article/details/6998517,
假定有这样一个对衣服估价的训练样本,代码中matrix表示,第一列表示色彩的评分,第二列表示对料子质地的评分,例如第一个样本1,4表示这件衣服色彩打1分,料子打4分。我们需要训练的是theta,其表示在衣服的估价中,色彩和料子的权重,这个权重是未知量,是需要训练的,训练的依据是这四个样本的真实价格已知,分别为19元,...20元。
通过批量梯度下降和随机梯度下降的方法均可得到theta_C={3,4}T

 

#include <iostream>
using namespace std;

float h(float x[2], float  q[3])
{
	return   q[0] + q[1] * x[1] + q[2] * x[2];
}

float batch(int u, int v)
{
	float samples[4][3] = {{1, 1, 4}, {1, 2, 5}, {1, 5, 1}, {1, 4, 2}};
	float y[] = {19, 26, 19, 20};//样本输出
	float q[] = {1, 2, 5};//权重
	float a = 0.01;

	float ht = 10000;
	int k;
	for (k = 0; k < 100000 && ht > 0.000000001; k++)
	{
		for (int j =0 ;j < 3; j++)
		{
			float sum = 0;
			for (int i = 0; i <4; i++)
			{
				sum += (y[i] - h(samples[i], q)) * samples[i][j];
			}
			cout << "q" << j << ":" << q[j];
			q[j] = q[j] + a * sum;
			cout << "->" << q[j] <<endl;
		}
		
		float s = 0;
		for (int i=0; i < 4; i++)
		{
			s += (h(samples[i], q) - y[i]) * (h(samples[i], q) - y[i]);
		}
		cout << "J:" << ht << "->" << s <<endl;
		ht = s;
	}
	cout << k << endl;
	float re[] = {1, u, v};
	return h(re, q);
}

float stochastic(int u, int v)
{
	float samples[4][3] = {{1, 1, 4}, {1, 2, 5}, {1, 5, 1}, {1, 4, 2}};
	float y[] = {19, 26, 19, 20};//样本输出
	float q[] = {0.01, 2, 5};//权重
	float a = 0.01;

	float ht = 10000;
	int k;
	for (k = 0; k < 100000 && ht > 0.000000000001; k++)
	{
		for (int j =0 ;j < 4; j++)
		{
			for (int i =0; i < 3; i++)
			{
				cout << "q" << i << ":" << q[i];
				q[i] = q[i] + a * (y[j] - h(samples[j], q)) * samples[j][i];
				cout << "->" << q[i] <<endl;
			}
		}
		
		float s = 0;
		for (int i=0; i < 4; i++)
		{
			s += (h(samples[i], q) - y[i]) * (h(samples[i], q) - y[i]);
		}
		cout << "J:" << ht << "->" << s <<endl;
		ht = s;
	}
	cout << k << endl;
	float re[] = {1, u, v};
	return h(re, q);
}

int main(void)
{
	cout << stochastic(3, 4) << endl;
	
	//cout << batch(3, 4) << endl;
}
 

你可能感兴趣的:(机器学习,批量梯度下降-batch)