梯度下降法在线性回归中效果不佳?
Gradient descent method does not work well in linear regression?
在 R 中,我生成了一些人工数据以使用梯度下降法执行线性回归
Y = c0 + c1 * x1 + c2 * x2 + 噪音
我也是用解析的方法计算参数theta = [c0, c1, c2]。
以下是带有变量注释的 R 代码。
我用梯度下降法来计算theta。公式取自下面的link。
slides from Stanford - Andrew Ng
但是,该方法无法收敛。我的 R 代码如下。 theta与R代码中的解析解k大不相同
rm(list = ls())
n=500
x1=rnorm(n,mean=4,sd=1.6)
x2=rnorm(n,mean=4,sd=2.5)
X=cbind(x1,x2)
A=as.matrix(cbind(rep(1,n),x1,x2))
Y=-3.9+3.8*x1-2.4*x2+rnorm(n,mean=0,sd=1.5);
k=solve(t(A)%*%A,t(A)%*%Y) # k is the parameters determined by analytical method
MSE=sum((A%*%k-Y)^2)/(n);
iterations=3000 # total number of step
epsilon = 0.0001 # set precision
eta=0.0001 # step size
t1=integer(iterations)
e1=integer(iterations)
X=as.matrix(X)# convert data table X into a matrix
N=dim(X)[1] # total number of observations
X=as.matrix(cbind(rep(1,length(N)),X))# add a column of ones to represent intercept
np=dim(X)[2] # number of parameters to be determined
theta=matrix(rnorm(n=np,mean=0,sd=1),1,np) # Initialize theta:1 x np matrix
for(i in 1:iterations){
error =theta%*%t(X)-t(Y) # error = (theta * x' -Y'). Error is a 1xN row vector;
grad=(1/N)*error%*%X # Gradient grad is 1 x np vector
theta=theta-eta*grad # updating theta
L=sqrt(sum((eta*grad)^2)) # calculating the L2 norm
e1[i]=sum((error)^2)/(2*N) # record the cost function in each step (value=2*MSE)
t1[i]=L # record the L2 norm in each step
if(L<=epsilon){ # checking whether convergence is obtained or not
break
}
}
plot(e1*2,type="l",ylab="MSE",lwd=2,col=rgb(0,0,1))
abline(h=MSE)
text(x=1000,y=MSE+1,labels = "Actual MSE",adj=1)
text(x=500,y=15,labels = "Gradient Descent",adj=0.4)
theta
k
我试过代码,问题是在 3000 次迭代后,L2 范数 L 仍然大于精度 epsilon。我可以通过 运行 获得可重现的示例数据
set.seed(5556)
第一。在 7419 次迭代后,L = 9.99938 e-5 < epsilon。
但是theta还是和预期的结果不一样,对于L2范数和正常数据当然可以计算运行lm(Y ~ X1+X2)
。
您的问题是您将 eta
设置得非常保守。因此需要很长时间才能收敛。 Eta 对收敛速度至关重要。但是,如果选择较大,算法可能不会收敛。你可能会在后面的课程中了解像 Adagrad 或 Adam 这样自动调整 eta 的算法
如果您选择 eta = 0.001
eta= 0.01
eta=0.1
在 R 中,我生成了一些人工数据以使用梯度下降法执行线性回归 Y = c0 + c1 * x1 + c2 * x2 + 噪音
我也是用解析的方法计算参数theta = [c0, c1, c2]。 以下是带有变量注释的 R 代码。
我用梯度下降法来计算theta。公式取自下面的link。
slides from Stanford - Andrew Ng
但是,该方法无法收敛。我的 R 代码如下。 theta与R代码中的解析解k大不相同
rm(list = ls())
n=500
x1=rnorm(n,mean=4,sd=1.6)
x2=rnorm(n,mean=4,sd=2.5)
X=cbind(x1,x2)
A=as.matrix(cbind(rep(1,n),x1,x2))
Y=-3.9+3.8*x1-2.4*x2+rnorm(n,mean=0,sd=1.5);
k=solve(t(A)%*%A,t(A)%*%Y) # k is the parameters determined by analytical method
MSE=sum((A%*%k-Y)^2)/(n);
iterations=3000 # total number of step
epsilon = 0.0001 # set precision
eta=0.0001 # step size
t1=integer(iterations)
e1=integer(iterations)
X=as.matrix(X)# convert data table X into a matrix
N=dim(X)[1] # total number of observations
X=as.matrix(cbind(rep(1,length(N)),X))# add a column of ones to represent intercept
np=dim(X)[2] # number of parameters to be determined
theta=matrix(rnorm(n=np,mean=0,sd=1),1,np) # Initialize theta:1 x np matrix
for(i in 1:iterations){
error =theta%*%t(X)-t(Y) # error = (theta * x' -Y'). Error is a 1xN row vector;
grad=(1/N)*error%*%X # Gradient grad is 1 x np vector
theta=theta-eta*grad # updating theta
L=sqrt(sum((eta*grad)^2)) # calculating the L2 norm
e1[i]=sum((error)^2)/(2*N) # record the cost function in each step (value=2*MSE)
t1[i]=L # record the L2 norm in each step
if(L<=epsilon){ # checking whether convergence is obtained or not
break
}
}
plot(e1*2,type="l",ylab="MSE",lwd=2,col=rgb(0,0,1))
abline(h=MSE)
text(x=1000,y=MSE+1,labels = "Actual MSE",adj=1)
text(x=500,y=15,labels = "Gradient Descent",adj=0.4)
theta
k
我试过代码,问题是在 3000 次迭代后,L2 范数 L 仍然大于精度 epsilon。我可以通过 运行 获得可重现的示例数据
set.seed(5556)
第一。在 7419 次迭代后,L = 9.99938 e-5 < epsilon。
但是theta还是和预期的结果不一样,对于L2范数和正常数据当然可以计算运行lm(Y ~ X1+X2)
。
您的问题是您将 eta
设置得非常保守。因此需要很长时间才能收敛。 Eta 对收敛速度至关重要。但是,如果选择较大,算法可能不会收敛。你可能会在后面的课程中了解像 Adagrad 或 Adam 这样自动调整 eta 的算法
如果您选择 eta = 0.001
eta= 0.01
eta=0.1