Re: [筆記] R 的設定-平行運算 (CPU)

看板R_Language作者 (攸藍)時間11年前 (2013/06/26 12:12), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/2 (看更多)
分享另一個平行運算的package: snowfall 以下是一個例子 for cross validation ### Example: Cross validation for choosing ### the tuning parameter of ridge regression ## Data data(longley) X <- as.matrix(longley[,1:5]) Y <- as.matrix(longley[,7]) ## Normalization norm <- function(a) (a-mean(a))/sd(a) Y.N <- apply(Y, 2, norm) X.N <- apply(X, 2, norm) D <- cbind(Y.N, X.N) n <- nrow(X.N) p <- ncol(X.N) ## Ridge regression, given lambda beta.R <- function(Y, X, nc, lambda) solve(t(X)%*%X + lambda*diag(nc),t(X)%*%Y) ## cross validation, single CPU version m <- 1001 lambda <- seq(0, 1, length=m) CV <- sapply(1:m, function(j){ out <- sapply(1:n, function(i) beta.hat <- beta.R(Y.N[-i,], X.N[-i,], p, lambda[j])) out <- mean((Y.N - diag(X.N%*%out))^2) }) plot(lambda, CV, type='l', xlab=expression(lambda), ylab='CV') lambda.opt <- lambda[which(CV == min(CV))] beta.opt <- beta.R(Y.N, X.N, p, lambda.opt) ## cross validation, parallel version ## in this case, the sample size is small but the number of lambda is large ## so we replace the outer loop by parallel computing ## prepare for parallel computing library(snowfall) # parallel computing sfInit(parallel = TRUE, cpus = 8, type="SOCK") # built four nodes ## Tell all nodes that following objects are going to be used sfExport('Y.N', 'X.N','m','n','p','beta.R', 'lambda') inner.loop <- function(j){ A <- apply(matrix(1:m,m,1), 1, function(i) beta.hat <- beta.R(Y.N[-i], X.N[-i,], p, lambda[j])) out <- mean((Y.N - diag(X.N%*%A))^2) return(out) } CV <- sfSapply(seq(along=Y.N), inner.loop) ## evaluate the performance wallTime <- function(expr) system.time(expr)[3] wallTime(sapply(1:m, function(j){ out <- sapply(1:n, function(i) beta.hat <- beta.R(Y.N[-i,], X.N[-i,], p, lambda[j])) out <- mean((Y.N - diag(X.N%*%out))^2) })) wallTime(sfSapply(seq(along=Y.N), inner.loop)) sfStop() -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.164.73.129

06/26 19:38, , 1F
謝謝分享,有空來看看
06/26 19:38, 1F

06/26 22:07, , 2F
06/26 22:07, 2F
文章代碼(AID): #1Hocf0cY (R_Language)
文章代碼(AID): #1Hocf0cY (R_Language)