[英]Problem to reproduce results from parallelSVM in R
我無法設置種子值以從parallelSVM()
獲得可重現的結果。
library(e1071)
library(parallelSVM)
data(iris)
x <- subset(iris, select = -Species)
y <- iris$Species
set.seed(1)
model <- parallelSVM(x, y)
parallelPredictions <- predict(model, x)
set.seed(1)
model2 <- parallelSVM(x, y)
parallelPredictions2 <- predict(model2, x)
all.equal(parallelPredictions,parallelPredictions2)
我知道這不是為多核操作設置種子值的正確方法,但我不知道該怎么做。
我知道在使用mclapply
時有一個選項,但這對我的情況沒有幫助。
編輯:
我找到了一個解決方案,方法是通過trace
更改parallelSVM
中的 function trainSample()
並使用foreach
循環更改種子的doRNG
package。
有人知道更好的解決方案嗎?
簡而言之, parallelSVM
中沒有實現的方法來處理這個問題。 然而 package 使用foreach
和doParallel
包來處理它的並行操作。 並且在stackoverflow上足夠努力地挖掘解決方案是可能的!
歸功於這個答案,關於doRNG
package 的使用,這個答案讓我對更簡單的封閉解決方案有了一個想法。
在parallelSVM
package 中,並行化通過parallelSVM::registerCores
函數發生。 這個 function 只是簡單地使用核心數量調用doParallel::registerDoParallel
,而不是進一步的 arguments。 我的想法是簡單地更改parallelSVM::registerCores
function,以便在創建新集群后自動將種子設置為。
在執行需要並行種子的並行計算時,需要確保兩件事
幸運的是doRNG
package 處理第一個並使用在 2 上沒問題的種子。使用unlockNamespace
和assign
的組合,我們可以覆蓋parallelSVM::registerCores
,這樣它就可以使用適當的種子調用doRNG::registerDoRNG
(答案末尾的函數)。 這樣做我們實際上可以獲得適當的再現性,如下所示:
library(parallelSVM)
library(e1071)
data(magicData)
set.seed.parallelSWM(1) #<=== set seed as we would normally.
#Example from help(parallelSVM)
system.time(parallelSvm1 <- parallelSVM(V11 ~ ., data = trainData[,-1],
numberCores = 4, samplingSize = 0.2,
probability = TRUE, gamma=0.1, cost = 10))
system.time(parallelSvm2 <- parallelSVM(V11 ~ ., data = trainData[,-1],
numberCores = 4, samplingSize = 0.2,
probability = TRUE, gamma=0.1, cost = 10))
pred1 <- predict(parallelSvm1)
pred2 <- predict(parallelSvm2)
all.equal(pred1, pred2)
[1] TRUE
identical(parallelSvm1, parallelSvm2)
[1] FALSE
請注意, identical
沒有能力通過parallel::parallelSvm
正確評估對象 output,因此預測更好地檢查模型是否相同。
為了安全起見,讓我們檢查問題中的可重復示例是否也是這種情況
x <- subset(iris, select = -Species)
y <- iris$Species
set.seed.parallelSWM(1) #<=== set seed as we would normally (not necessary if above example has been run).
model <- parallelSVM(x, y)
model2 <- parallelSVM(x, y)
parallelPredicitions <- predict(model, x)
parallelPredicitions2 <- predict(model2, x)
all.equal(parallelPredicitions, parallelPredicitions2)
[1] TRUE
呸..
最后,如果我們完成了,或者如果我們再次想要隨機種子,我們可以通過執行重置種子
set.seed.parallelSWM() #<=== set seed to random each execution (standard).
#check:
model <- parallelSVM(x, y)
model2 <- parallelSVM(x, y)
parallelPredicitions <- predict(model, x)
parallelPredicitions2 <- predict(model2, x)
all.equal(parallelPredicitions, parallelPredicitions2)
[1] "3 string mismatches"
(output 會有所不同,因為未設置 RNNG 種子)
歸功於這個答案。 請注意,我們可能不必加倍分配,但在這里我只是簡單地復制了答案,而不檢查代碼是否可以進一步減少。
set.seed.parallelSWM <- function(seed, once = TRUE){
if(missing(seed) || is.character(seed)){
out <- function (numberCores)
{
cluster <- parallel::makeCluster(numberCores)
doParallel::registerDoParallel(cluster)
}
}else{
require("doRNG", quietly = TRUE, character.only = TRUE)
out <- function(numberCores){
cluster <- parallel::makeCluster(numberCores)
doParallel::registerDoParallel(cluster)
doRNG::registerDoRNG(seed = seed, once = once)
}
}
unlockBinding("registerCores", as.environment("package:parallelSVM"))
assign("registerCores", out, "package:parallelSVM")
lockBinding("registerCores", as.environment("package:parallelSVM"))
unlockBinding("registerCores", getNamespace("parallelSVM"))
assign("registerCores", out, getNamespace("parallelSVM"))
lockBinding("registerCores", getNamespace("parallelSVM"))
#unlockBinding("registerCores", as.environment("package:parallelSVM"))
invisible()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.