Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
foreach缺少输出_R_Foreach_Parallel Processing - Fatal编程技术网

foreach缺少输出

foreach缺少输出,r,foreach,parallel-processing,R,Foreach,Parallel Processing,如果我创建了一个简单的模型,foreach将以我认为应该的方式返回每个结果: m=function(i,j){data.frame(i=i,j=j)} > foreach(i=1:2, .combine='rbind') %:% foreach(j=1:2, .combine='rbind') %dopar%{ + m(i,j) + } i j 1 1 1 2 1 2 3 2 1 4 2 2 但使用更复杂的函数会错过第一个循环: # Loop through the predicti

如果我创建了一个简单的模型,foreach将以我认为应该的方式返回每个结果:

m=function(i,j){data.frame(i=i,j=j)}
> foreach(i=1:2, .combine='rbind') %:% foreach(j=1:2, .combine='rbind') %dopar%{
+ m(i,j)
+ }
  i j
1 1 1
2 1 2
3 2 1
4 2 2
但使用更复杂的函数会错过第一个循环:

# Loop through the prediction model (in parallel) with different parameters
results = foreach(i=1:2, .combine='rbind') %:% foreach(j=1:2, .combine='rbind') %dopar%{
    model(i,j)
}
> results
  i j tpr       fpr       rj                      day
1 1 2   0 0.2127812 1.022387 Wed Oct 29 11:53:45 2014
2 2 1   0 0.2161888 1.023102 Wed Oct 29 11:54:41 2014
3 2 2   0 0.2127812 1.022387 Wed Oct 29 11:53:45 2014
当i=1,j=1时,您可能会假设函数正在生成错误,但在foreach循环之外运行函数会得到很好的结果:

> model(1,1)
  i j tpr       fpr       rj                      day
1 1 1   0 0.2161888 1.023102 Wed Oct 29 12:30:31 2014
所以我假设我把foreach迭代器设置错了。希望这个问题对你来说比我更明显

编辑: 如果将%dopar%替换为%do%,则该选项也有效。当然,此解决方案不符合使用foreach的目的

我假设“model”在由worker执行时返回不同的结果,可能是因为worker没有正确初始化

为了测试该理论,我将添加一些额外的测试代码:

results <-
  foreach(i=1:2, .combine='rbind') %:%
    foreach(j=1:2, .combine='rbind') %dopar% {
      x <- model(i,j)
      stopifnot(! is.null(x))
      stopifnot(nrow(x) == 1)
      x
    }

results一个最小的可复制示例将很好地帮助您进行调试。没有它,也不知道模型(i,j)
是如何定义的,很难说问题是什么。我无法用通用数据重现问题。事实上,重新连接到服务器似乎已经解决了这个问题。(尽管将来可能会再次出现。)我怀疑有内存不足的错误,尽管从详细的输出来看这并不明显。我无法在没有模型的情况下再现您的示例,但您可能希望编写
results=foreach(I=1:2,.combine='rbind')%:%foreach(j=1:2'))
即没有第二个联合收割机参数?谢谢您的建议。这一次,两个i=1结果都被忽略。关于如何解释这一点,我还需要多读一些书。