[英]Incorrect number of dimensions - parallel R computation
在 R 中使用 tm 包和并行计算时遇到问题,我不确定我是在做傻事还是错误。
我创建了一个小的可重现示例:
# Load the libraries
library(tm)
library(snow)
# Create a Document Term Matrix
test_sentence = c("this is a test", "this is another test")
test_corpus = VCorpus(VectorSource(test_sentence))
test_TM = DocumentTermMatrix(test_corpus)
# Define a simple function that returns the matrix for the i-th document
test_function = function(i, TM){ TM[i, ] }
如果我使用这个例子运行一个简单的 lapply ,我会得到预期的结果,没有任何问题:
# This returns the expected list containing the rows of the Matrix
res1 = lapply(1:2, test_function, test_TM)
但是如果我并行运行它,我会收到错误:
第一个错误:维数不正确
# This should return the same thing of the lapply above but instead it stops with an error
cl = makeCluster(2)
res2 = parLapply(cl, 1:2, test_function, test_TM)
stopCluster(cl)
问题是不同的节点不会自动加载tm
包。 然而,加载包是必要的,因为它定义了相关对象类的[
方法。
下面的代码执行以下操作:
tm
包cl <- makeCluster(rep("localhost",2), type="SOCK")
clusterEvalQ(cl, library(tm))
clusterExport(cl, list=ls())
res <- parLapply(cl, as.list(1:2), test_function, test_TM)
stopCluster(cl)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.