0

I am trying to do a descent gradient algorithm with a Himmelblau function in R. This is my code:

`library(numDeriv)

gradient_descent <- function(f, start_value, max_iter) {
  gamma <- 1/max_iter #how much the function will move to the negative direction
  #of the gradient function in order to have a minimum (the learning rate)
  current_value <- start_value
  
  for (i in 1:max_iter) {
    grad_f <- grad(f, current_value)
    step_size <- gamma * grad_f
    current_value <- current_value - gamma * grad_f #minus for the negative 
    #direction (to get the minimum)
    
    if (step_size <0.001){ #if the step size is smaller than 0.001
      break
    }
    
  }
  
  return(current_value)
}`

but I get this error: Error in if (step_size < 0.001) { : the condition has length > 1

Any idea what is happening?

Thanks!

  • 1
    It means that your object `step_size` has more than one coefficient (which makes sense since it is defined as proportional to a gradient, that would probabily have more than one coefficient). I would advise you to think again about the stopping criterion of your algorithm. – Vincent Guillemot Feb 27 '23 at 17:26
  • the stopping criteria should use either the function value or the norm of the gradient/score vector – Onyambu Feb 27 '23 at 17:36

0 Answers0