我尝试从PYMC3 and Stan comparison复制模型。但是它似乎运行缓慢,当我查看@code_warntype
时,我认为有些东西– K
和N
–编译器似乎将其称为Any
。>
我尝试添加类型-尽管无法将类型添加到turing_model
的参数中,并且turing_model
中的事情很复杂,因为它使用的是autodiff变量,而不是通常的变量。我将所有代码放入函数do_it
中以避免使用全局变量,因为它们说全局变量会使速度变慢。 (不过,实际上似乎较慢。)
有关导致问题的原因的任何建议? turing_model
代码正在迭代,因此应该发挥最大作用。
using Turing, StatsPlots, Random
sigmoid(x) = 1.0 / (1.0 + exp(-x))
function scale(w0::Float64, w1::Array{Float64,1})
scale = √(w0^2 + sum(w1 .^ 2))
return w0 / scale, w1 ./ scale
end
function do_it(iterations::Int64)::Chains
K = 10 # predictor dimension
N = 1000 # number of data samples
X = rand(N, K) # predictors (1000, 10)
w1 = rand(K) # weights (10,)
w0 = -median(X * w1) # 50% of elements for each class (number)
w0, w1 = scale(w0, w1) # unit length (euclidean)
w_true = [w0, w1...]
y = (w0 .+ (X * w1)) .> 0.0 # labels
y = [Float64(x) for x in y]
σ = 5.0
σm = [x == y ? σ : 0.0 for x in 1:K, y in 1:K]
@model turing_model(X, y, σ, σm) = begin
w0_pred ~ Normal(0.0, σ)
w1_pred ~ MvNormal(σm)
p = sigmoid.(w0_pred .+ (X * w1_pred))
@inbounds for n in 1:length(y)
y[n] ~ Bernoulli(p[n])
end
end
@time chain = sample(turing_model(X, y, σ, σm), NUTS(iterations, 200, 0.65));
# ϵ = 0.5
# τ = 10
# @time chain = sample(turing_model(X, y, σ), HMC(iterations, ϵ, τ));
return (w_true=w_true, chains=chain::Chains)
end
chain = do_it(1000)