我尝试为某些样本数据使用梯度下降来制作用于线性回归的程序。我得到的theta值不能最适合这些数据。我已经将数据标准化了。
public class OneVariableRegression {
public static void main(String[] args) {
double x1[] = {-1.605793084, -1.436762233, -1.267731382, -1.098700531, -0.92966968, -0.760638829, -0.591607978, -0.422577127, -0.253546276, -0.084515425, 0.084515425, 0.253546276, 0.422577127, 0.591607978, 0.760638829, 0.92966968, 1.098700531, 1.267731382, 1.436762233, 1.605793084};
double y[] = {0.3, 0.2, 0.24, 0.33, 0.35, 0.28, 0.61, 0.38, 0.38, 0.42, 0.51, 0.6, 0.55, 0.56, 0.53, 0.61, 0.65, 0.68, 0.74, 0.87};
double theta0 = 0.5;
double theta1 = 0.5;
double temp0;
double temp1;
double alpha = 1.5;
double m = x1.length;
System.out.println(m);
double derivative0 = 0;
double derivative1 = 0;
do {
for (int i = 0; i < x1.length; i++) {
derivative0 = (derivative0 + (theta0 + (theta1 * x1[i]) - y[i])) * (1/m);
derivative1 = (derivative1 + (theta0 + (theta1 * x1[i]) - y[i])) * (1/m) * x1[i];
}
temp0 = theta0 - (alpha * derivative0);
temp1 = theta1 - (alpha * derivative1);
theta0 = temp0;
theta1 = temp1;
//System.out.println("Derivative0 = " + derivative0);
//System.out.println("Derivative1 = " + derivative1);
}
while (derivative0 > 0.0001 || derivative1 > 0.0001);
System.out.println();
System.out.println("theta 0 = " + theta0);
System.out.println("theta 1 = " + theta1);
}
}
您使用的导数来自平方误差函数,该函数是凸的,因此除一个全局最小值外,不接受任何局部最小值。(实际上,这类问题甚至可以接受称为正态方程的闭式解,对于大问题而言,它在数值上不是易处理的,因此使用梯度下降法)
正确的答案在theta0 = 0.4895
和周围theta1 = 0.1652
,这对于在任何统计计算环境中进行检查都是微不足道的。(如果您对此表示怀疑,请参见答案底部)
下面我指出您的代码中的错误,更正错误后,您会在4个小数位后得到上面的正确答案。
因此,您期望它收敛全局最小值是正确的,但是在实现中存在问题
每次重新计算时derivative_i
,您都忘记将其重置为0(您正在做的是在do{}while()
您需要在do while循环中使用它
do {
derivative0 = 0;
derivative1 = 0;
...
}
接下来是这个
derivative0 = (derivative0 + (theta0 + (theta1 * x1[i]) - y[i])) * (1/m);
derivative1 = (derivative1 + (theta0 + (theta1 * x1[i]) - y[i])) * (1/m) * x1[i];
该x1[i]
因子应单独应用(theta0 + (theta1 * x1[i]) - y[i]))
。
您的尝试有点令人困惑,因此让我们以一种更清晰的方式编写它,如下所示,这与它的数学方程式非常接近(1/m)sum(y_hat_i - y_i)x_i
:
// You need fresh vars, don't accumulate the derivatives across gradient descent iterations
derivative0 = 0;
derivative1 = 0;
for (int i = 0; i < m; i++) {
derivative0 += (1/m) * (theta0 + (theta1 * x1[i]) - y[i]);
derivative1 += (1/m) * (theta0 + (theta1 * x1[i]) - y[i])*x1[i];
}
那应该使您足够接近,但是,我发现您的学习率alpha有点大。当它太大时,您的梯度下降将无法在没有全局最优的情况下归零,它会徘徊在那儿,但不会完全在那里。
double alpha = 0.5;
运行它,并将其与统计软件中的答案进行比较
➜ ~ javac OneVariableRegression.java && java OneVariableRegression
20.0
theta 0 = 0.48950064086914064
theta 1 = 0.16520139788757973
我将其与R进行了比较
> x
[1] -1.60579308 -1.43676223 -1.26773138 -1.09870053 -0.92966968 -0.76063883
[7] -0.59160798 -0.42257713 -0.25354628 -0.08451543 0.08451543 0.25354628
[13] 0.42257713 0.59160798 0.76063883 0.92966968 1.09870053 1.26773138
[19] 1.43676223 1.60579308
> y
[1] 0.30 0.20 0.24 0.33 0.35 0.28 0.61 0.38 0.38 0.42 0.51 0.60 0.55 0.56 0.53
[16] 0.61 0.65 0.68 0.74 0.87
> lm(y ~ x)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
0.4895 0.1652
现在,您的代码至少给出了4个小数的正确答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句