来自 pycall 的 Julia 多线程

发布于 2025-01-17 05:23:27 字数 897 浏览 0 评论 0原文

假设我有一个 jupyter 笔记本:

%%julia

using Pkg
Pkg.add("DecisionTree")
using DecisionTree

X = Vector([1.1,2.2,3.3])
Y = Vector([1.1,2.2,3.3])
X = reshape(X, size(X))

X = Float32.(X)
Y = Float32.(Y)
print(typeof(X))
print(typeof(Y))
model = DecisionTree.build_forest(Y, X')

据我所知 DecisionTree.jl 使用多线程,而 pycall 不支持多线程,这会导致错误:

RuntimeError: <PyCall.jlwrap (in a Julia function called from Python)
JULIA: TaskFailedException
Stacktrace:
  [1] wait
    @ .\task.jl:334 [inlined]
  [2] threading_run(func::Function)
    @ Base.Threads .\threadingconstructs.jl:38
  [3] macro expansion
    @ .\threadingconstructs.jl:97 [inlined]
  [4] build_forest(labels::Vector{Float32}, features::LinearAlgebra.Adjoint{Float32, Vector{Float32}}, n_subfeatures::Int64, n_trees::Int64, partial_sampling::Float64, max_depth::Int64, 

我的问题是 - 有什么办法让它工作吗?

Say I have a jupyter notebook:

%%julia

using Pkg
Pkg.add("DecisionTree")
using DecisionTree

X = Vector([1.1,2.2,3.3])
Y = Vector([1.1,2.2,3.3])
X = reshape(X, size(X))

X = Float32.(X)
Y = Float32.(Y)
print(typeof(X))
print(typeof(Y))
model = DecisionTree.build_forest(Y, X')

From what I know DecisionTree.jl uses multithreading, which pycall does not support, which results in the error:

RuntimeError: <PyCall.jlwrap (in a Julia function called from Python)
JULIA: TaskFailedException
Stacktrace:
  [1] wait
    @ .\task.jl:334 [inlined]
  [2] threading_run(func::Function)
    @ Base.Threads .\threadingconstructs.jl:38
  [3] macro expansion
    @ .\threadingconstructs.jl:97 [inlined]
  [4] build_forest(labels::Vector{Float32}, features::LinearAlgebra.Adjoint{Float32, Vector{Float32}}, n_subfeatures::Int64, n_trees::Int64, partial_sampling::Float64, max_depth::Int64, 

My question is - is there any way to make it work after all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2025-01-24 05:23:28

这个问题与从 Python 调用它无关,而是因为您正在尝试创建一个模型,其中特征是具有 3 维的单个记录,标签是 3(记录)向量。
DecisionTrees 确实期望输入是标签维度为 nRecords 的列向量,以及特征的 nRecods × nDimensions 矩阵。

例如:

julia> X = [1.1,2.2,3.3]
3-element Vector{Float64}:
 1.1
 2.2
 3.3

julia> Y = [1.1,2.2,3.3]
3-element Vector{Float64}:
 1.1
 2.2
 3.3

julia> X = reshape(X,3,1) # reshape to a single column **matrix**
3×1 Matrix{Float64}:
 1.1
 2.2
 3.3

julia> model = DecisionTree.build_forest(Y, X)
Ensemble of Decision Trees
Trees:      10
Avg Leaves: 1.0
Avg Depth:  0.0

此外,要创建矢量,您不需要指定“Vector”。
我建议你看看我的 Julia 教程 或在我的用 Julia 进行科学编程和机器学习课程中(我只用了几天就完成了)之前我还需要“清理”一下才公布)

The problem has nothing to do with calling it from Python, but from the fact that you are trying to make a model where the features is a single record with 3 dimensions and the label is a 3 (records) vector.
DecisionTrees expects indeed the input to be a column vector of dimension nRecords for the label and a nRecods by nDimensions matrix for the features.

For example:

julia> X = [1.1,2.2,3.3]
3-element Vector{Float64}:
 1.1
 2.2
 3.3

julia> Y = [1.1,2.2,3.3]
3-element Vector{Float64}:
 1.1
 2.2
 3.3

julia> X = reshape(X,3,1) # reshape to a single column **matrix**
3×1 Matrix{Float64}:
 1.1
 2.2
 3.3

julia> model = DecisionTree.build_forest(Y, X)
Ensemble of Decision Trees
Trees:      10
Avg Leaves: 1.0
Avg Depth:  0.0

Also, to make a vector you don't need to specify "Vector".
I suggest you to have a look on my tutorial on Julia or on my course on Scientific Programming and Machine Learning with Julia (I completed it just a couple of days ago, I still need to "clean" it before announcing it)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文