【问题标题】:Need to reduce threads used by neuralnet package in R需要减少 R 中神经网络包使用的线程
【发布时间】:2022-01-28 21:42:27
【问题描述】:

我有一个运行 rstudio(R 版本 4.0.5)的大型 linux 服务器,我与其他 6 人共享。它有 56 个内核。按照设计,任何程序员都不应该在需要很长时间的工作上使用超过 8 个内核。我正在使用神经网络,它完全消耗了所有可用的内核。我需要能够将其锁定为 8 个核心。

像 xgboost 这样的包具有限制线程的能力。我无法为包神经网络找到任何类似的东西。是否有另一种线程限制方式,或者包神经网络是否有一种我不知道的限制线程的方式?

这是你可以使用的脚本,这个脚本不是我写的,我在这篇文章中找到了它: Error in plot.nn: weights were not calculated 我已经更改了脚本,以便它会收敛


install.packages("tidyverse", dependencies = TRUE)
install.packages("neuralnet", dependencies = TRUE)
install.packages("plyr", dependencies = TRUE)

library(tidyverse)
library(neuralnet)
library(plyr)


CreditCardnn <- read.csv("https://raw.githubusercontent.com/621-Group2/Final-Project/master/UCI_Credit_Card.csv")


#Normalize dataset
maxValue <- apply(CreditCardnn, 2, max)
minValue <- apply(CreditCardnn, 2, min)

CreditCardnn <- as.data.frame(scale(CreditCardnn, center = minValue, scale = maxValue - minValue))

#Rename to target variable
colnames(CreditCardnn)[25] <- "target"


smp <- floor(0.70 * nrow(CreditCardnn))
set.seed(4784)

CreditCardnn$ID <- NULL
train_index <- sample(seq_len(nrow(CreditCardnn)), size = smp, replace = FALSE)

train_nn <- CreditCardnn[train_index, ]
test_nn <- CreditCardnn[-train_index, ]

allVars <- colnames(CreditCardnn)
predictorVars <- allVars[!allVars%in%'target']
predictorVars <- paste(predictorVars, collapse = "+")
f <- as.formula(paste("target~", predictorVars, collapse = "+"))

nueralModel <- neuralnet(formula = f, 
                         data = train_nn, 
                         hidden = c(4,2), 
                         linear.output = T, 
                         lifesign = 'full',
                         stepmax = 1e6)

plot(nueralModel)

更新 1:1.27.2022

这是我的会话信息。我确实似乎 BLAS 可能正在这样做 我没有使用数据表

R version 4.0.5 (2021-03-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux 8.3 (Ootpa)

Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblasp-r0.3.3.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] plyr_1.8.6       neuralnet_1.44.6 forcats_0.5.1    stringr_1.4.0    dplyr_1.0.7      purrr_0.3.4      readr_2.0.0      tidyr_1.1.4     
 [9] tibble_3.1.3     ggplot2_3.3.5    tidyverse_1.3.1 

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7       cellranger_1.1.0 pillar_1.6.2     compiler_4.0.5   dbplyr_2.1.1     tools_4.0.5      jsonlite_1.7.2   lubridate_1.7.10
 [9] lifecycle_1.0.0  gtable_0.3.0     pkgconfig_2.0.3  rlang_0.4.11     reprex_2.0.1     cli_3.0.1        rstudioapi_0.13  DBI_1.1.1       
[17] haven_2.4.3      xml2_1.3.2       withr_2.4.2      httr_1.4.2       fs_1.5.0         generics_0.1.0   vctrs_0.3.8      hms_1.1.0       
[25] grid_4.0.5       tidyselect_1.1.1 glue_1.4.2       R6_2.5.0         fansi_0.5.0      readxl_1.3.1     tzdb_0.1.2       modelr_0.1.8    
[33] magrittr_2.0.1   backports_1.2.1  scales_1.1.1     ellipsis_0.3.2   rvest_1.0.1      assertthat_0.2.1 colorspace_2.0-2 utf8_1.2.2      
[41] stringi_1.6.2    munsell_0.5.0    broom_0.7.9      crayon_1.4.1    

【问题讨论】:

  • 在我的 Windows 10 机器上,它仅在单线程上运行。也许另一个包正在使用所有内核? data.table 例如?
  • 我在neuralnet cran page 上没有看到任何建议包级并行化的内容。这可能发生在较低级别,例如在通过 openblas 或 intel mkl 之类的矩阵计算中。 sessionInfo() 是否提供有关正在使用的矩阵库的任何信息?

标签: r multithreading neural-network rstudio-server


【解决方案1】:

并行化是由于使用了矩阵库openblas。我将线程数设置为 8 并解决了问题。

我在下面有一个更新的脚本,供以后的观众查看做了什么:

install.packages("tidyverse", dependencies = TRUE)
install.packages("neuralnet", dependencies = TRUE)
install.packages("plyr", dependencies = TRUE)
install.packages("RhpcBLASctl", dependencies = TRUE)

library(tidyverse)
library(neuralnet)
library(plyr)
library(RhpcBLASctl)

CreditCardnn <- read.csv("https://raw.githubusercontent.com/621-Group2/Final-Project/master/UCI_Credit_Card.csv")


#Normalize dataset
maxValue <- apply(CreditCardnn, 2, max)
minValue <- apply(CreditCardnn, 2, min)

CreditCardnn <- as.data.frame(scale(CreditCardnn, center = minValue, scale = maxValue - minValue))

#Rename to target variable
colnames(CreditCardnn)[25] <- "target"


smp <- floor(0.70 * nrow(CreditCardnn))
set.seed(4784)

CreditCardnn$ID <- NULL
train_index <- sample(seq_len(nrow(CreditCardnn)), size = smp, replace = FALSE)

train_nn <- CreditCardnn[train_index, ]
test_nn <- CreditCardnn[-train_index, ]

allVars <- colnames(CreditCardnn)
predictorVars <- allVars[!allVars%in%'target']
predictorVars <- paste(predictorVars, collapse = "+")
f <- as.formula(paste("target~", predictorVars, collapse = "+"))

设置 blas 使用和运行的线程数:

threads <- 8
blas_set_num_threads(threads)
omp_set_num_threads(threads)

nueralModel <- neuralnet(formula = f, 
                         data = train_nn, 
                         hidden = c(4,2), 
                         linear.output = T, 
                         lifesign = 'full',
                         stepmax = 1e6)

plot(nueralModel)

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2018-11-01
  • 2017-08-27
  • 1970-01-01
  • 2017-11-27
  • 2015-09-10
  • 2012-08-05
  • 2018-05-30
  • 1970-01-01
  • 2021-01-10
相关资源
最近更新 更多