人們經常說,人們應該更喜歡lapply
而不是for
循環。 Hadley Wickham在他的Advance R書中指出了一些例外。 ()(修改原地,遞歸等)。 以下是這種情況之一。lapply vs for loop - Performance R
爲了學習的緣故,我嘗試用函數形式重寫感知器算法,以便基準測試 的相對性能。 來源(https://rpubs.com/FaiHas/197581)。
這是代碼。
# prepare input
data(iris)
irissubdf <- iris[1:100, c(1, 3, 5)]
names(irissubdf) <- c("sepal", "petal", "species")
head(irissubdf)
irissubdf$y <- 1
irissubdf[irissubdf[, 3] == "setosa", 4] <- -1
x <- irissubdf[, c(1, 2)]
y <- irissubdf[, 4]
# perceptron function with for
perceptron <- function(x, y, eta, niter) {
# initialize weight vector
weight <- rep(0, dim(x)[2] + 1)
errors <- rep(0, niter)
# loop over number of epochs niter
for (jj in 1:niter) {
# loop through training data set
for (ii in 1:length(y)) {
# Predict binary label using Heaviside activation
# function
z <- sum(weight[2:length(weight)] * as.numeric(x[ii,
])) + weight[1]
if (z < 0) {
ypred <- -1
} else {
ypred <- 1
}
# Change weight - the formula doesn't do anything
# if the predicted value is correct
weightdiff <- eta * (y[ii] - ypred) * c(1,
as.numeric(x[ii, ]))
weight <- weight + weightdiff
# Update error function
if ((y[ii] - ypred) != 0) {
errors[jj] <- errors[jj] + 1
}
}
}
# weight to decide between the two species
return(errors)
}
err <- perceptron(x, y, 1, 10)
### my rewriting in functional form auxiliary
### function
faux <- function(x, weight, y, eta) {
err <- 0
z <- sum(weight[2:length(weight)] * as.numeric(x)) +
weight[1]
if (z < 0) {
ypred <- -1
} else {
ypred <- 1
}
# Change weight - the formula doesn't do anything
# if the predicted value is correct
weightdiff <- eta * (y - ypred) * c(1, as.numeric(x))
weight <<- weight + weightdiff
# Update error function
if ((y - ypred) != 0) {
err <- 1
}
err
}
weight <- rep(0, 3)
weightdiff <- rep(0, 3)
f <- function() {
t <- replicate(10, sum(unlist(lapply(seq_along(irissubdf$y),
function(i) {
faux(irissubdf[i, 1:2], weight, irissubdf$y[i],
1)
}))))
weight <<- rep(0, 3)
t
}
我沒有任何預期持續改善由於上述 問題。但是,當我看到使用lapply
和replicate
的劇烈惡化 時,我真的很驚訝。
我獲得使用microbenchmark
功能從microbenchmark
庫
這個結果怎麼可能是什麼原因? 難道是一些內存泄漏?
expr min lq mean median uq
f() 48670.878 50600.7200 52767.6871 51746.2530 53541.2440
perceptron(as.matrix(irissubdf[1:2]), irissubdf$y, 1, 10) 4184.131 4437.2990 4686.7506 4532.6655 4751.4795
perceptronC(as.matrix(irissubdf[1:2]), irissubdf$y, 1, 10) 95.793 104.2045 123.7735 116.6065 140.5545
max neval
109715.673 100
6513.684 100
264.858 100
第一個功能是所述lapply
/replicate
功能
第二與for
環
第三是在C++
相同功能的使用Rcpp
這裏根據羅蘭功能功能分析。 我不確定我能否以正確的方式解讀它。 它看起來像我的大部分時間都花在子集化 Function profiling
請確定。我沒有看到在函數f中調用'apply'。 – Roland
我建議你學習如何剖析函數:http://adv-r.had.co.nz/Profiling.html – Roland
你的代碼有幾個錯誤;首先,'irissubdf [,4] < - 1'應該是'irissubdf $ y < - 1',所以您可以稍後使用該名稱,其次,'weight'在您在'f'中使用之前未定義。我也不清楚'' - ''在'lapply'和'replicate'命令中做了正確的事情,但是我不清楚它應該做什麼。這也可能是兩者之間的主要區別; '<< - '必須處理環境,而另一個則不處理,雖然我不確切知道可能會有什麼影響,但它不再是蘋果來比較蘋果。 – Aaron