Julia并行循环,两次减少

时间:2015-03-02 06:25:11

标签: julia

我想在Julia的并行for循环中执行两次缩减。我正在尝试计算并行for循环内随机林中的错误,因为每个树都是构建的。有什么想法吗?

电流:

forest = @parallel (vcat) for i in 1:ntrees
    inds = rand(1:Nlabels, Nsamples)
    build_tree(labels[inds], features[inds,:], nsubfeatures)
end

我想要的是,直观地说是在这个for循环中做一个补充,以避免出现包错误。这就是我希望它的工作方式:

forest, ooberror = @parallel (vcat, +) for i in 1:ntrees
    inds = rand(1:Nlabels, Nsamples)
    tree = build_tree(labels[inds], features[inds,:], nsubfeatures)
    error = geterror(ids, features, tree)
    (tree, error)
end

1 个答案:

答案 0 :(得分:5)

在简单性和清晰度方面使用类型可能是最好的,例如

type Forest
  trees :: Vector
  error
end
join(a::Forest, b::Forest) = Forest(vcat(a.trees,b.trees), a.error+b.error)

#...

forest = @parallel (join) for i in 1:ntrees
    inds  = rand(1:Nlabels, Nsamples)
    tree  = build_tree(labels[inds], features[inds,:], nsubfeatures)
    error = geterror(ids, features, tree)
    Forest(tree, error)
end