gradient descent pour une fonction à variable multiple
Bonjour ;
J'ai implémenter l'algorithme du gradient descent sur R :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| # Having the number of iterations, step size, and start value be parameters the
# user can alter (with sane default values) I think is a better approach than
# hard coding them in the body of the function
grad<-function(iter = 30, alpha = 0.1, x_init = 1){
# define the objective function f(x) = sqrt(2+x)+sqrt(1+x)+sqrt(3+x)
objFun = function(x) return(sqrt(2+x)+sqrt(1+x)+sqrt(3+x))
# define the gradient of f(x) = sqrt(2+x)+sqrt(1+x)+sqrt(3+x)
# Note we don't split up the gradient here
gradient <- function(x) {
result <- 1 / (2 * sqrt(2 + x))
result <- result + 1 / (2 * sqrt(1 + x))
result <- result + 1 / (2 * sqrt(3 + x))
return(result)
}
x <- x_init
# create a vector to contain all xs for all steps
x.All = numeric(iter)
# gradient descent method to find the minimum
for(i in seq_len(iter)){
# Guard against NaNs
tmp <- x - alpha * gradient(x)
if ( !is.nan(suppressWarnings(objFun(tmp))) ) {
x <- tmp
}
x.All[i] = x
print(x)
}
# print result and plot all xs for every iteration
print(paste("The minimum of f(x) is ", objFun(x), " at position x = ", x, sep = ""))
plot(x.All, type = "l")
} |
Je veux faire la meme chose pour une fonction à plusieurs variables , J'ai essayé mais ça ne marche pas :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
grad<-function(iter = 30, alpha = 0.1){
# define the objective function f(x)
objFun = function (x,y) return((x^2+y-11)^2+(x+y^2-7)^2)
# define the gradient of f(x)
# Note we don't split up the gradient here
gradient <- function(x , y) {
result <- c(4*x*(x^2+y-11)+2*(x+y^2-7),2*(x^2 + y - 11 )+4*y*(x+y^2-7))
return(result)
}
result <- c(5,5)
# create a vector to contain all xs for all steps
result.All = c(iter)
# gradient descent method to find the minimum
for(i in seq_len(iter)){
# Guard against NaNs
tmp <- result - alpha * gradient(x,y)
if ( !is.nan(suppressWarnings(objFun(tmp))) ) {
result <- tmp
}
result.All[i] = result
print(result)
}
# print result and plot all xs for every iteration
print(paste("The minimum of f(x) is ", objFun(x , y), " at position x = ", result, sep = ""))
plot(result.All, type = "l")
} |