1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| library(kernlab) Rcpp::sourceCpp("kernel_matrix_arma.cpp") Rcpp::sourceCpp("kernel_matrix_eigen1.cpp") Rcpp::sourceCpp("kernel_matrix_eigen2.cpp") Rcpp::sourceCpp("kernel_matrix_arma_para.cpp") Rcpp::sourceCpp("kernel_matrix_eigen_para1.cpp") Rcpp::sourceCpp("kernel_matrix_eigen_para2.cpp")
Sys.setenv("PKG_LIBS" = "$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)") Rcpp::sourceCpp("kernel_matrix_eigen3.cpp") Rcpp::sourceCpp("kernel_matrix_eigen_para3.cpp") N <- 5000L p <- 1000L b <- 500L X <- matrix(rnorm(N*p), ncol = p) center <- X[sample(N, b),] sigma <- 2.5
kernelMatrix_R <- function(X, center, sigma){ exp(sweep(sweep(X %*% t(center), 1, rowSums(X**2)/2), 2, rowSums(center**2)/2) / (sigma**2)) }
res_kernlab <- kernelMatrix(rbfdot(sigma=1/(2*sigma^2)), X, center)@.Data
all.equal(kernelMatrix_R(X, center, sigma), res_kernlab) all.equal(kernelMatrix_arma_cpp(X, center, sigma), res_kernlab) all.equal(kernelMatrix_eigen_cpp(X, center, sigma), res_kernlab) all.equal(kernelMatrix_eigen_omp_cpp(X, center, sigma), res_kernlab) all.equal(kernelMatrix_eigen_cpp2(X, center, sigma), res_kernlab) all.equal(kernelMatrix_arma_para_cpp(X, center, sigma), res_kernlab) all.equal(kernelMatrix_eigen_para_cpp(X, center, sigma), res_kernlab) all.equal(kernelMatrix_eigen_para_omp_cpp(X, center, sigma), res_kernlab) all.equal(kernelMatrix_eigen_para_cpp2(X, center, sigma), res_kernlab)
library(microbenchmark) microbenchmark( Rfun = kernelMatrix_R(X, center, sigma), kernlab = kernelMatrix(rbfdot(sigma=1/(2*sigma^2)), X, center), RcppArmadillo = kernelMatrix_arma_cpp(X, center, sigma), RcppEigen = kernelMatrix_eigen_cpp(X, center, sigma), RcppEigen_Openmp = kernelMatrix_eigen_omp_cpp(X, center, sigma), RcppEigen2 = kernelMatrix_eigen_cpp2(X, center, sigma), RcppArmadillo_RcppParallel = kernelMatrix_arma_cpp(X, center, sigma), RcppEigen_RcppParallel = kernelMatrix_eigen_para_cpp(X, center, sigma), RcppEigen_RcppParallel_Openmp = kernelMatrix_eigen_para_omp_cpp(X, center, sigma), RcppEigen_RcppParallel2 = kernelMatrix_eigen_para_cpp2(X, center, sigma), times = 30L )
|