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