daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(Deriv)
  2. library(tidyverse)
  3. library(stringr)
  4. library(latex2exp)
  5. library(glue)
  6.  
  7. # test function
  8. f <- function(x) ((x^2-4*x+4)*(x^2+4*x+2))
  9.  
  10. # plotting the test function
  11. library(ggplot2)
  12. p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
  13. p + stat_function(fun = f) +
  14.   xlim(-4,4)
  15.  
  16. # derivative of the test function
  17. f_prime <- Deriv(f)
  18.  
  19. # run a gradient descent
  20. x = 3.5
  21. x_initial = x
  22. x_updated_values <- numeric(0)
  23. learning_rate = 0.005
  24.  
  25. vdx = 0
  26. beta = 0.9
  27. for (i in 1:240) {
  28.   if (i==1) {x_updated_values[i] = x}
  29.   if (i==1) {next}
  30.  
  31.   dx = f_prime(x)
  32.   vdx = beta*vdx+dx
  33.   x = x-learning_rate*vdx
  34.   x_updated_values[i] = x
  35. }
  36.  
  37. df <- data_frame(x = x_updated_values,
  38.                  y = f(x_updated_values))
  39.  
  40. # plotting the parameter updates
  41. x_on_iteration_i <- df$x[i]
  42. y_on_iteration_i <- df$y[i]
  43. p + stat_function(fun = f) +
  44.   xlim(-4,4)+
  45.   geom_point(data = df, aes(x, y))+
  46.   geom_point(x = x_on_iteration_i, y = y_on_iteration_i, color = 'blue', size = 4, alpha = 0.5)+
  47.   theme(legend.position="none")+
  48.   ylab("f(x)")+
  49.   ggtitle(TeX('$f(x) = (x^2-4x+4)(x^2+4x+2)$'),
  50.           subtitle = glue("Gradient Descent with Learning Rate: {learning_rate} and beta: {beta}"))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top