带有两次归约的 Julia 并行 for 循环

标签 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

最佳答案

就简单性和清晰度而言,使用类型可能是最好的,例如

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/

相关文章:

function - 在语言层面, `ccall` 到底是什么?

oop - Julia 中的享元/OOP 设计模式

julia - 如何在 Julia 中删除数组数组中的数组

julia - 如何用 Julia 语言播放任何波形的音频?

plot - 如何使用 Makie 的流图为时变函数制作动画?

julia inf by inf 不同的结果

arrays - Julia 语言: sub vs. slice函数

julia - Julia Sets 和 Dicts 的顺序是否保证跨 session 、平台和版本稳定?

random - 有没有办法获取随机数生成器的状态?

arrays - 在 Julia 中构建遍历多个索引的数组