Turing autodiff error

I have a two small model which are connected to each other. Both have common parameters.

nested task error: TaskFailedException nested task error: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}) Closest candidates are: (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat at rounding.jl:200 (::Type{T})(::T) where T<:Number at boot.jl:772 (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50 … Stacktrace: [1] convert(#unused#::Type{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}) @ Base ./number.jl:7 [2] setindex!(A::Vector{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}, i1::Int64) @ Base ./array.jl:966 [3] fit_affinity_stability(model::DynamicPPL.Model{typeof(fit_affinity_stability), (:data, :prob), (), (), Tuple{Vector{DataFrame}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Int64}, ODEFunction{true, SciMLBase.AutoSpecialize, typeof(Affinity_stability), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}}, Tuple{}, DynamicPPL.DefaultContext}, varinfo::DynamicPPL.ThreadSafeVarInfo{DynamicPPL.TypedVarInfo{NamedTuple{(:kon, :koff, :X, :σ), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:kon, Setfield.IdentityLens}, Int64}, Vector{Uniform{Float64}}, Vector{AbstractPPL.VarName{:kon, Setfield.IdentityLens}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, Vector{Set{DynamicPPL.Selector}}}, DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:koff, Setfield.IdentityLens}, Int64}, Vector{Uniform{Float64}}, Vector{AbstractPPL.VarName{:koff, Setfield.IdentityLens}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, Vector{Set{DynamicPPL.Selector}}}, DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:X, Setfield.IdentityLens}, Int64}, Vector{Uniform{Float64}}, Vector{AbstractPPL.VarName{:X, Setfield.IdentityLens}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, Vector{Set{DynamicPPL.Selector}}}, DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:σ, Setfield.IdentityLens}, Int64}, Vector{Uniform{Float64}}, Vector{AbstractPPL.VarName{:σ, Setfield.IdentityLens}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, Vector{Set{DynamicPPL.Selector}}}}}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, Vector{Base.RefValue{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}}}, context::DynamicPPL.SamplingContext{DynamicPPL.Sampler{NUTS{Turing.Essential.ForwardDiffAD{0}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext, Random._GLOBAL_RNG}, data::Vector{DataFrame}, prob::ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Int64}, ODEFunction{true, SciMLBase.AutoSpecialize, typeof(Affinity_stability), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}) @ Main ./In[20]:22 [4] macro expansion @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:593 [inlined] [5] _evaluate!! @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:576 [inlined] [6] evaluate_threadsafe!! @ ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:567 [inlined] [7] evaluate!!(model::DynamicPPL.Model{typeof(fit_affinity_stability), (:data, :prob), (), (), Tuple{Vector{DataFrame}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Int64}, ODEFunction{true, SciMLBase.AutoSpecialize, typeof(Affinity_stability), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}}, Tuple{}, DynamicPPL.DefaultContext}, varinfo::DynamicPPL.TypedVarInfo{NamedTuple{(:kon, :koff, :X, :σ), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:kon, Setfield.IdentityLens}, Int64}, Vector{Uniform{Float64}}, Vector{AbstractPPL.VarName{:kon, Setfield.IdentityLens}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, Vector{Set{DynamicPPL.Selector}}}, DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:koff, Setfield.IdentityLens}, Int64}, Vector{Uniform{Float64}}, Vector{AbstractPPL.VarName{:koff, Setfield.IdentityLens}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, Vector{Set{DynamicPPL.Selector}}}, DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:X, Setfield.IdentityLens}, Int64}, Vector{Uniform{Float64}}, Vector{AbstractPPL.VarName{:X, Setfield.IdentityLens}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, Vector{Set{DynamicPPL.Selector}}}, DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:σ, Setfield.IdentityLens}, Int64}, Vector{Uniform{Float64}}, Vector{AbstractPPL.VarName{:σ, Setfield.IdentityLens}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, Vector{Set{DynamicPPL.Selector}}}}}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}}, context::DynamicPPL.SamplingContext{DynamicPPL.Sampler{NUTS{Turing.Essential.ForwardDiffAD{0}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext, Random._GLOBAL_RNG}) @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/model.jl:502

@ ~/.julia/packages/Turing/szPqN/src/inference/Inference.jl:205 [inlined] [13] #sample#5 @ ~/.julia/packages/Turing/szPqN/src/inference/Inference.jl:192 [inlined] [14] top-level scope @ In[21]:1

Hello!

If you provide more information, it’s more likely you’ll get some helpful answers.

1 Like

You are trying to autodiff a model which has a hardcoded conversion to Float64 somewhere and therefore doesn’t work with dual numbers:

no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4})

Often this comes from using something like zeros(n) instead of zeros(eltype(x), n) where x is some parameter in your model.

2 Likes

My turing model look like this:

@model function fit(data, prob)

    # Prior distributions.
    k1  ~ Uniform(0, 0.1)
    k2 ~ Uniform(0, 0.1)
    X    ~ Uniform(0, 10^-5)
    σ    ~ Uniform(0, 2000)
    

    p = [ k1, k2, X]
    

    pp =  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

  for i in 1:6

    u0 = [a[i], b, c]
    prob  = remake(prob, u0 = u0, p=p)
    sol = solve(prob, Rosenbrock23(autodiff = false), saveat=saveat)
    sol =  sol'[end,3]
    pp[i] = sol
  
  end
    
  predicted_1 =  pp
  u0 = [ 0, b - pp[6] , pp[6]]
  
  prob  = remake(prob, u0 = u0)
  
  predicted_2 = solve(prob, Rosenbrock23(autodiff = false), saveat=[0,1,2,4,6,8])'[:,3] 
   
  predicted = [predicted_1, predicted_2]
    
   for i in 1:2
       for j in 1:size(data[i])[2] - 1
             
        data[i][:,j+1] ~ MvNormal(predicted[i] /X ,  σ*(data[i][:,j+1] .+10^-8))
      
       end 
    end
   
    return nothing

Thank you for your suggestion.I tried but its not woking.