gradient descent implementation
octave:
function parameters = gd(input, output, theta, step, eps)
% function that implement gradient descent algorithm
col = length(output);
do
% j = 1 / 2 / col * sum((theta' * input - output) .^ 2);
% 上行注释计算costfunction
update = 1 / col * input * (theta' * input - output)';
theta = theta - step * update;
until max(abs(update)) <= eps
parameters = theta;值得一提的是,由于线性代数拙计,这里的更新规则我推了好久。。。
c:
void gd(trainingset *t, hypothesis *h, double step, double eps) {
double * temp = MA(double, t->features + 1); //给偏导数向量申请空间
double sum = 0;
double sum_temp = 0;
do {
for (int i = 0; i < t->features + 1; i++) {
sum = 0;
for (int j = 0; j < t->count; j++) {
sum_temp = 0;
for (int k = 0; k < t->features + 1; k++) {
//计算h(x)
sum_temp += h->p[k] * t->p[j*(t->features + 2) + k];
}
//h(x)-y
sum_temp -= t->p[j*(t->features + 2) + t->features + 1];
//(h(x)-y)*xj
sum_temp *= t->p[j*(t->features + 2) + i];
//从1-m对上式求和
sum += sum_temp;
}
sum /= t->count; //求和之后除样本总数
sum *= step; //乘学习速率
temp[i] = sum; //将偏导数项赋值给偏导数向量,计算完成之后一起更新
}
for (int i = 0; i < t->features + 1; i++) {
h->p[i] -= temp[i]; //更新参数Theta
}
} while (!is_converge(temp, t->features + 1, eps)); //判断是否收敛
free(temp);
}