2016-06-11 49 views
1

當我嘗試這段代碼時。我在平行並行時找不到變量

# include library 
require(stats) 
library(GMD) 
library(parallel) 
# include function 
source('~/Workspaces/Projects/RProject/MovielensCluster/readData.R'); # contain readtext.convert() function 
### 
elbow.k <- function(mydata){ 
    ## determine a "good" k using elbow 
    dist.obj <- dist(mydata); 
    hclust.obj <- hclust(dist.obj); 
    css.obj <- css.hclust(dist.obj,hclust.obj); 
    elbow.obj <- elbow.batch(css.obj); 
    # print(elbow.obj) 
    k <- elbow.obj$k 
    return(k) 
} 

# include file 
filePath <- "dataset/u.user"; 
data.convert <- readtext.convert(filePath); 
data.clustering <- data.convert[,c(-1,-4)]; 
# find k value 
no_cores <- detectCores(); 
cl<-makeCluster(no_cores); 
clusterExport(cl, list("data.clustering", "data.original", "elbow.k", "clustering.kmeans")); 
start.time <- Sys.time(); 
k.clusters <- parSapply(cl, 1, function(x) elbow.k(data.clustering)); 
end.time <- Sys.time(); 
cat('Time to find k using Elbow method is',(end.time - start.time),'seconds with k value:', k.clusters); 

的問題,我有一個錯誤的通知:

Error in get(name, envir = envir) : object 'data.original' not found 
Error in checkForRemoteErrors(val) : 
    one node produced an error: could not find function "elbow.k" 

誰能幫我解決這個問題?非常感謝。

回答

3

我認爲你的問題與「可變範圍」有關。在Mac/Linux上,您可以選擇使用自動包含所有環境變量的makeCluster(no_core,type =「FORK」)。在Windows上,您必須使用並行套接字羣集(PSOCK),該套接字僅在加載基礎軟件包時開始。因此,您總是確切地指定包含並行函數的變量和庫。 clusterExport()clusterEvalQ()是必要的,以便函數分別查看所需的變量和包。請注意,對clusterExport後的變量所做的任何更改都將被忽略。回到你的問題。這麼多

# include library 
require(stats) 
library(GMD) 
library(parallel) 
# include function 
source('~/Workspaces/Projects/RProject/MovielensCluster/readData.R'); # contain readtext.convert() function 
### 
elbow.k <- function(mydata){ 
    ## determine a "good" k using elbow 
    dist.obj <- dist(mydata); 
    hclust.obj <- hclust(dist.obj); 
    css.obj <- css.hclust(dist.obj,hclust.obj); 
    elbow.obj <- elbow.batch(css.obj); 
    # print(elbow.obj) 
    k <- elbow.obj$k 
    return(k) 
} 

# include file 
filePath <- "dataset/u.user"; 
data.convert <- readtext.convert(filePath); 
data.clustering <- data.convert[,c(-1,-4)]; 
# find k value 
no_cores <- detectCores(); 
cl<-makeCluster(no_cores); 
clusterEvalQ(cl, library(GMD)); 
clusterExport(cl, list("data.clustering", "data.original", "elbow.k", "clustering.kmeans")); 
start.time <- Sys.time(); 
k.clusters <- parSapply(cl, 1, function(x) elbow.k(data.clustering)); 
end.time <- Sys.time(); 
cat('Time to find k using Elbow method is',(end.time - start.time),'seconds with k value:', k.clusters); 
+0

感謝:必須以下列用途:

clusterEvalQ(cl, library(GMD)); 

和你的全部代碼。它運作良好 – Khongbich