我想在 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
最佳答案
就简单性和清晰度而言,使用类型可能是最好的,例如
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
关于带有两次归约的 Julia 并行 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28803782/