2015-03-02 35 views
4

我想在朱莉娅平行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 

回答

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