0

I've code gradient descent algorithm in R and now I'm trying to "draw" the path of the vectors.

I've got draw points in my contour plot, but it's not correct because nobody knows what happened first.

In my algorith always I have an previous state P=(Xi,Yi) and a later state L=(Xi+1,Yi+1), so, How can I draw the vector PL in a contour or a persp plot?

I only got this with contour, where the red point is the convergence:

contour

The same for persp:

persp

Thanks all!

EDIT:

Graphics can be obtanined respectively:

f<-function(u,v){
  u*u*exp(2*v)+4*v*v*exp(-2*u)-4*u*v*exp(v-u)
}

x = seq(-2, 2, by = 0.5)
y = seq(-2, 2, by = 0.5)
z <- outer(x,y,f)
#Contour plot
contour(x,y,z)
#Persp plot
persp(x, y, z, phi = 25, theta = 55, xlim=c(-2,2), ylim=c(-2,2),
      xlab = "U", ylab = "V",
      main = "F(u,v)", col="yellow", ticktype = "detailed"
) -> res
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Carlos
  • 889
  • 3
  • 12
  • 34

2 Answers2

0

There's a trick to plotting points using persp, as mentioned in ?persp. By employing the power of trans3d, you can successfully put points and lines on a perspective plot.

f<-function(u,v){
  u*u*exp(2*v)+4*v*v*exp(-2*u)-4*u*v*exp(v-u)
}

x = seq(-2, 2, by = 0.5)
y = seq(-2, 2, by = 0.5)
z <- scale(outer(x,y,f))

view <- persp(x, y, z, phi = 30, theta = 30, xlim=c(-2,2), ylim=c(-2,2),
      xlab = "X", ylab = "Y", zlab = "Z", scale = FALSE,
      main = "F(u,v)", col="yellow", ticktype = "detailed")

set.seed(2)
pts <- data.frame(x = sample(x, 3),
                  y = sample(y, 3),
                  z = sample(z, 3))

points(trans3d(x = pts$x, y = pts$y, z = pts$z, pmat = view), pch = 16)
lines(trans3d(x = pts$x, y = pts$y, z = pts$z, pmat = view))

enter image description here

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
0

Taking Himmelblau's function as a test example:

f <- function(x, y) { (x^2+y-11)^2 + (x+y^2-7)^2 }

Its partial derivatives:

dx <- function(x,y) {4*x**3-4*x*y-42*x+4*x*y-14}
dy <- function(x,y) {4*y**3+2*x**2-26*y+4*x*y-22}

Running the gradient descent:

# gradient descent parameters
num_iter <- 100
learning_rate <- 0.001
x_val <- 6
y_val <- 6

updates_x <- vector("numeric", length = num_iter)
updates_y <- vector("numeric", length = num_iter)
updates_z <- vector("numeric", length = num_iter)

# parameter updates
for (i in 1:num_iter) {

  dx_val = dx(x_val,y_val)
  dy_val = dy(x_val,y_val)

  x_val <- x_val-learning_rate*dx_val
  y_val <- y_val-learning_rate*dx_val
  z_val <- f(x_val, y_val)

  updates_x[i] <- x_val
  updates_y[i] <- y_val
  updates_z[i] <- z_val
}

Plotting:

x <- seq(-6, 6, length = 100)
y <- x
z <- outer(x, y, f)

plt <- persp(x, y, z,
               theta = -50-log(i), phi = 20+log(i),
               expand = 0.5,
               col = "lightblue", border = 'lightblue',
               axes = FALSE, box = FALSE,
               ltheta = 60, shade = 0.90
  )

points(trans3d(updates_x[1:i], updates_y[1:i], updates_z[1:i],pmat = plt),
       col = c(rep('white', num_iter-1), 'blue'),
       pch = 16,
       cex = c(rep(0.5, num_iter-1), 1))

enter image description here

Imran Kocabiyik
  • 419
  • 5
  • 15