gpt4 book ai didi

r - 带有 doSMP 和 foreach 的并行随机森林大大增加了内存使用量(在 Windows 上)

转载 作者:IT王子 更新时间:2023-10-28 23:38:10 28 4
gpt4 key购买 nike

在串行执行随机森林时,它在我的系统上使用 8GB 的​​ RAM,而在并行执行时,它使用超过两倍的 RAM (18GB)。并行执行时如何将其保持在 8GB?代码如下:

install.packages('foreach')
install.packages('doSMP')
install.packages('randomForest')

library('foreach')
library('doSMP')
library('randomForest')

NbrOfCores <- 8
workers <- startWorkers(NbrOfCores) # number of cores
registerDoSMP(workers)
getDoParName() # check name of parallel backend
getDoParVersion() # check version of parallel backend
getDoParWorkers() # check number of workers


#creating data and setting options for random forests
#if your run this please adapt it so it won't crash your system! This amount of data uses up to 18GB of RAM.
x <- matrix(runif(500000), 100000)
y <- gl(2, 50000)
#options
set.seed(1)
ntree=1000
ntree2 <- ntree/NbrOfCores


gc()

#running serialized version of random forests

system.time(
rf1 <- randomForest(x, y, ntree = ntree))


gc()


#running parallel version of random forests

system.time(
rf2 <- foreach(ntree = rep(ntree2, 8), .combine = combine, .packages = "randomForest") %dopar% randomForest(x, y, ntree = ntree))

最佳答案

首先,SMP 将复制输入,以便每个进程都有自己的副本。这可以通过使用 multicore 进行转义,但还有另一个问题 - randomForest 的每次调用也会生成输入的内部副本。

您可以通过使 randomForest 删除森林模型本身(使用 keep.forest=FALSE)并在训练的同时进行测试(通过使用 xtest 和可能的 ytest 参数)。

关于r - 带有 doSMP 和 foreach 的并行随机森林大大增加了内存使用量(在 Windows 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8776816/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com