<div dir="ltr">Hello Team,<div>                  Greetings. I am implementing the gradient descent function on the cxx function and have not been able to find the correct function argument without errors. Could you please suggest the <i><b>correct function argument</b></i> for implementing it. The complete R code for the gradient descent algorithm and my code is provided below :  </div><div><br></div><div>Gradient descent algorithm : </div><div><br></div><div>--------------------------------------------------------------<br><b><br>library(Rcpp)<br>library(inline)<br>library(rbenchmark)<br>#UNIVARIATE GRADIENT DESCENT ALGORITHM<br>#OBJECTIVE: MINIMIZE THE FUNCTION <br><br></b></div><div><b>#FIRST, DEFINE THE FUNCTION f(theta)<br>objfun <- function(theta){<br>  return(1 + 3*(theta + 3)^2 )<br>}<br><br>#DEFINE A FUNCTION THAT CALCULATES THE DERIVATIVE f'(theta) <br># AT INPUT VALUE theta<br>deriv <- function(theta)<br>{<br>  return( 6*(theta + 3 ) )<br>}<br><br><br><br><br><br>#PLOT THE FUNCTION WE ARE SEEKING TO MINIMIZE OVER THE RANGE [-15, 15]<br>theta_seq <- seq(-20,15, len = 1000)<br>plot(theta_seq, objfun(theta_seq), type = "l", <br>     ylab = "f(theta)", xlab = "theta")<br><br>points(10, objfun(10), col = "red")<br><br><br>tol <- 0.0000001 #CONVERGENCE THRESHOLD<br>alpha <- 0.01  #LEARNING RATE<br>theta0 <- 10  #SELECT INITIAL GUESS FOR THETA<br>newval <- objfun( theta0 ) #inital value of f(theta)<br>points(theta0, newval, col = "red") #add current theta to plot<br>rel_ch <- 1 #(arbitrary) init. val for relative change in objective function<br>j <- 1 #iteration counter<br>theta <- c() #vector to store theta at each iteration<br>theta[j] <- theta0 #theta[j] stores current value of theta<br><br>#update theta while relative change in f(theta) is greater than tol<br>while( rel_ch > tol ) <br>{<br>  j <- j+1; #increment j<br>  #update theta<br>  #set theta_new =  theta_previous - ( learning rate )* f'(theta_previous)<br>  theta[j] <- theta[j-1] - alpha * deriv(theta[ j-1 ])<br>  <br>  #test relative absolute change in target function<br>  oldval <- newval #store f(theta_previous) <br>  newval <- objfun(theta[j]) #calculate f(theta_new)<br>  points(theta[j], newval, col = "red") #add new theta to plot<br>  Sys.sleep(0.1) #pause algorithm to give you time to see dot appear on plot<br>  #calculate relative change f(theta) from previous iteration to current iteration<br>  rel_ch <- abs(  ( newval - oldval ) / oldval ) #use to test convergence<br>}<br>#R version EXECUTED WITHOUT A PLOT (NON SLOW MOTION VERSION)<br><br><br>gdes <- function( theta0  , tol = 0.00000001, alpha = 0.01 )<br>{<br>  <br>  newval <- objfun( theta0 ) #inital value of target function<br>  rel_ch <- 1 #to store relative change in objective function<br>  j <- 1 #iteration counter<br>  theta <- c(theta0) #vector to store parameter<br>  <br>  while( rel_ch > tol )<br>  {<br>    j <- j+1; #increment counter<br>    theta[j] <- theta[j-1] - alpha * deriv(theta[ j-1 ])<br>    <br>    #test relative absolute change in target function<br>    oldval <- newval<br>    newval <- objfun(theta[j])<br>    rel_ch <- abs(  ( newval - oldval ) / oldval )<br>  } #end of while<br>  <br>  return( list( theta = theta[j], thetavec = theta, min_f = newval, niter = j ) )<br>} #end of gdes function <br><br><br>opt <- gdes(10) <br>opt$theta<br>opt$min_f<br></b></div><div><br></div><div>--------------------------------------------------------------------------</div><div><br></div><div>MY  TRY : </div><div><b><br></b></div><div><b>library(Rcpp)<br>library(inline)<br><br><br><br># and define our version in C++<br>src <- 'int theta0 = as<int>(ns);<br>int j = as<int>(ns1);<br>int rel_ch = as<int>(ns2);<br>int min_f = as<int>(ns22);<br>int oldval = as<int>(rup);<br>int dif = as<int>(ank);<br>int newval = as<int>(saha);<br>int niter = as<int>(nit);<br>NumericVector theta = as<NumericVector>(theta1);<br>NumericVector thetavec = as<NumericVector>(thetavector);<br>double tol = as<double>(xs);<br>double alpha = as<double>(rt);<br>while(rel_ch>tol) j=j+1; theta[j] = theta[j-1] - alpha * 6*(theta[j-1] + 3 ); oldval = (1 + 3*pow(theta0 + 3),2); newval = (1 + 3*pow(theta[j] + 3),2); dif = (newval - oldval);rel_ch = abs(dif) / oldval );              <br>theta = theta[j];<br>thetavec = theta;<br>min_f = newval;<br>niter = j; <br>return wrap(theta);<br>return wrap(thetavec);<br>return wrap(min_f);<br>return wrap(niter);<br>'<br><br>al <- cxxfunction(signature(ns="integer", nit="integer",ns22 = "integer",ns1 = "integer", ns2 = "integer", ank = "integer", rup = "integer", saha = "integer",theta1="NumericVector",thetavector="NumericVector",<br>                            xs="numeric",rt="numeric"),<br>                  body=src, plugin="Rcpp")<br><br></b>----------------------------------------------------------------------------------<br><br></div><div>Sincerely,</div><div>Ankit Chakraborty<br></div><div>Machine Learning Engineer, EEGRAB</div></div>