Rilja - 1 year ago 106
R Question

# Gradient-descent code error

I wrote the code below for gradient descent algorithm. I get an error, can anyone tell me why and how I can fix it?

``````gradient <- function(h, start, alpha = 0.01, tolerance = 0.0001, debug = FALSE) {
MAXITER <- 1000
x_old <- start
iter <- 0

cat("gradient descent minimization =\n")
if (debug == TRUE) cat("iter=", iter,", value=", x_old, "\n")

repeat {
iter <- iter + 1
x_new <- x_old - alpha*h(x_old)
if (debug == TRUE) cat("iter=", iter,", value=", x_new, "\n")
if (abs(x_old - x_new) < tolerance) break
if (iter > MAXITER) break
x_old <- x_new
}
cat("total number of interations =", iter, "\n")
cat("last diference =", abs(x_old - x_new, "\n"))
cat("final value =", x_new, "\n")
cat("final function value =", h(x_new), "\n")
}

h <- function(x){ x^4 - 8*x^2 + 2*x }

gradient(h, -5, tolerance = 0.0001)

...

gradient descent minimization =

Error in if (abs(x_old - x_new) < tolerance) break :
missing value where TRUE/FALSE needed
``````

Answer Source

I just run your code. There is a numeric instability here. Add a condition `if(is.nan(x_new)) break` after your `x_new <- x_old - alpha*h(x_old)` statement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download