## ============================================================ ## Gradient descent using R - on quadratic function (no graph) ## Copyright © 2021 ANN-Sense Pte. Ltd. - All Rights Reserved ## By using this material, you are agreeing to abide by our terms of service ## as listed here - https://ann-sense.com/terms-of-service ## ============================================================ ## Assume that we have the following quadratic function ## quadratic function: x^2 - 3*x +1 ## 1. Let's create our gradient of our quadratic function ## as we need it for gradient descent ## -------------------------------------------------- gradient_quadratic_fn <- function(x){ 2*x - 3 } ## 2. Create a gradient descent function that can trace every steps ## ---------------------------------------------------------------- ## Note: This gradient descent is only designed to work on our specified ## quadratic function. gradient_descent<- function(x, learn_rate, conv_threshold, max_iter){ ## 1. Initialise key parameters ## ---------------------------- y <- quadratic_func(x) converged = F iterations = 0 learn_rate0 = learn_rate ## 2. Create the gradient descent loop ## -------------------------------------- while(converged == F) { ## (a) core gradient descent algorithm ## ----------------------------------- gradient <- gradient_quadratic_fn(x) x_new <- x - learn_rate * gradient y <- quadratic_func(x_new) ## (b) checking for convergence ## ---------------------------- if( abs(x_new - x) <= conv_threshold) { converged = T } ## (c) checking if max iteration is reached ## ------------------------------------ if(iterations >= max_iter) { break } x <- x_new } return(c(x_new, y)) } ## 3. Using our gradient descent function ## --------------------------------------------- gradient_descent(x = 0.2, ## initial x_value learn_rate = 0.2, ## learning rate conv_threshold = 0.00001, ## convergence threshold max_iter = 100 ## maximum iteration )