# Turing autodiff error

**URL:** <https://discourse.julialang.org/t/turing-autodiff-error/98603>\
**Category:** New to Julia\
**Tags:** question, turing\
**Created:** [May 10, 2023, 9:25am UTC](https://discourse.julialang.org/t/turing-autodiff-error/98603 "2023-05-10T09:25:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 10, 2023, 9:25am UTC](https://discourse.julialang.org/t/turing-autodiff-error/98603/1 "2023-05-10T09:25:25Z")

</div>

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 [./](https://vscode-remote+ssh-002dremote-002bl-002d01.vscode-resource.vscode-cdn.net/home/sahil.khan/julia_programs/code/)number.jl:7 [2] setindex!(A::Vector{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 4}, i1::Int64) @ Base [./](https://vscode-remote+ssh-002dremote-002bl-002d01.vscode-resource.vscode-cdn.net/home/sahil.khan/julia_programs/code/)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 [./](https://vscode-remote+ssh-002dremote-002bl-002d01.vscode-resource.vscode-cdn.net/home/sahil.khan/julia_programs/code/)In[20]:22 [4] macro expansion @ [~/.julia/packages/DynamicPPL/zPOYL/src/](https://vscode-remote+ssh-002dremote-002bl-002d01.vscode-resource.vscode-cdn.net/home/sahil.khan/julia_programs/code/~/.julia/packages/DynamicPPL/zPOYL/src/)model.jl:593 [inlined] [5] \_evaluate!! @ [~/.julia/packages/DynamicPPL/zPOYL/src/](https://vscode-remote+ssh-002dremote-002bl-002d01.vscode-resource.vscode-cdn.net/home/sahil.khan/julia_programs/code/~/.julia/packages/DynamicPPL/zPOYL/src/)model.jl:576 [inlined] [6] evaluate\_threadsafe!! @ [~/.julia/packages/DynamicPPL/zPOYL/src/](https://vscode-remote+ssh-002dremote-002bl-002d01.vscode-resource.vscode-cdn.net/home/sahil.khan/julia_programs/code/~/.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/](https://vscode-remote+ssh-002dremote-002bl-002d01.vscode-resource.vscode-cdn.net/home/sahil.khan/julia_programs/code/~/.julia/packages/DynamicPPL/zPOYL/src/)model.jl:502

…

@ [~/.julia/packages/Turing/szPqN/src/inference/Inference.jl:205](https://vscode-remote+ssh-002dremote-002bl-002d01.vscode-resource.vscode-cdn.net/home/sahil.khan/julia_programs/code/~/.julia/packages/Turing/szPqN/src/inference/Inference.jl:205) [inlined] [13] #sample#5 @ [~/.julia/packages/Turing/szPqN/src/inference/Inference.jl:192](https://vscode-remote+ssh-002dremote-002bl-002d01.vscode-resource.vscode-cdn.net/home/sahil.khan/julia_programs/code/~/.julia/packages/Turing/szPqN/src/inference/Inference.jl:192) [inlined] [14] top-level scope @ In[21]:1

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 10, 2023, 9:51am UTC](https://discourse.julialang.org/t/turing-autodiff-error/98603/2 "2023-05-10T09:51:19Z")

</div>

Hello!

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

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

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 10, 2023, 10:36am UTC](https://discourse.julialang.org/t/turing-autodiff-error/98603/3 "2023-05-10T10:36:58Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 10, 2023, 11:27am UTC](https://discourse.julialang.org/t/turing-autodiff-error/98603/4 "2023-05-10T11:27:39Z")

</div>

My turing model look like this:

```julia
@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

```

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 11, 2023, 12:15pm UTC](https://discourse.julialang.org/t/turing-autodiff-error/98603/5 "2023-05-11T12:15:00Z")

</div>

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