【发布时间】:2019-06-24 16:38:04
【问题描述】:
我正在阅读 Goodfellow 等人的深度学习。并尝试实现梯度下降,如第 4.5 节示例:线性最小二乘法所示。这是该书硬拷贝中的第 92 页。
可以在https://www.deeplearningbook.org/contents/numerical.html查看算法的详细信息,第 94 页的线性最小二乘的 R 实现。
我尝试在 R 中实现,并且实现的算法收敛于一个向量,但这个向量似乎并没有根据需要最小化最小二乘函数。将 epsilon 添加到有问题的向量中经常会产生小于我的程序输出的最小值的“最小值”。
options(digits = 15)
dim_square = 2 ### set dimension of square matrix
# Generate random vector, random matrix, and
set.seed(1234)
A = matrix(nrow = dim_square, ncol = dim_square, byrow = T, rlnorm(dim_square ^ 2)/10)
b = rep(rnorm(1), dim_square)
# having fixed A & B, select X randomly
x = rnorm(dim_square) # vector length of dim_square--supposed to be arbitrary
f = function(x, A, b){
total_vector = A %*% x + b # this is the function that we want to minimize
total = 0.5 * sum(abs(total_vector) ^ 2) # L2 norm squared
return(total)
}
f(x,A,b)
# how close do we want to get?
epsilon = 0.1
delta = 0.01
value = (t(A) %*% A) %*% x - t(A) %*% b
L2_norm = (sum(abs(value) ^ 2)) ^ 0.5
steps = vector()
while(L2_norm > delta){
x = x - epsilon * value
value = (t(A) %*% A) %*% x - t(A) %*% b
L2_norm = (sum(abs(value) ^ 2)) ^ 0.5
print(L2_norm)
}
minimum = f(x, A, b)
minimum
minimum_minus = f(x - 0.5*epsilon, A, b)
minimum_minus # less than the minimum found by gradient descent! Why?
在https://www.deeplearningbook.org/contents/numerical.html出现的pdf的第94页上
我试图找到向量 x 的值,以使 f(x) 最小化。但是,正如我的代码中的最小值和 minimum_minus 所示,最小值 不是 实际最小值,因为它超过了最小值负值。
知道可能是什么问题吗?
【问题讨论】:
-
您可以编辑您的问题以更改链接
标签: r algorithm least-squares