How to solve the error while loading saved chain as JLD2 file or JLS file?

Hi,

I have saved the chain that generated using Turing package as below:

using JLD2, FileIO
using Turing

# Set the true probability of heads in a coin.
    p_true = 0.5

    # Iterate from having seen 0 observations to 100 observations.
    Ns = 0:100;

    # Draw data from a Bernoulli distribution, i.e. draw heads or tails.
    Random.seed!(12)
    data = rand(Bernoulli(p_true), last(Ns))
    # g_data = CuArray(data)

    # Declare our Turing model.
    @model coinflip(y) = begin
        # Our prior belief about the probability of heads in a coin.
        p ~ Beta(1, 1)

        # The number of observations.
        N = length(y)
        for n in 1:N
            # Heads or tails of a coin are drawn from a Bernoulli distribution.
            y[n] ~ Bernoulli(p)
        end
    end;

    # Settings of the Hamiltonian Monte Carlo (HMC) sampler.
    # iterations = 1000
    ϵ = 0.05
    Ď„ = 10
    iterations = 1000
    # Start sampling.
    chain = sample(coinflip(data), HMC(ϵ, τ), iterations);
    @save "data\\test.jld2" chain

After that I cleared the chain and tried to load as below:

chain = 0
@load "data\\test.jld2" chain

But I got error as below:

ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex at .\array.jl:744 [inlined]
 [2] #86 at .\none:0 [inlined]
 [3] iterate at .\generator.jl:47 [inlined]
 [4] collect(::Base.Generator{UnitRange{Int64},MCMCChains.var"#86#87"{Array{ChainDataFrame,N} where N,Array{ChainDataFrame,1}}}) at .\array.jl:622
 [5] convert at C:\Users\z5168736\.julia\packages\MCMCChains\YabhG\src\summarize.jl:92 [inlined]
 [6] construct_array(::JLD2.MmapIO, ::Type{ChainDataFrame}, ::Int64) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:291
 [7] read_array(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.ReadDataspace, ::JLD2.ReadRepresentation{ChainDataFrame,JLD2.RelOffset}, ::Int64, ::UInt16, ::JLD2.RelOffset, ::Array{JLD2.ReadAttribute,1}) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:316
 [8] read_data(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.ReadRepresentation{Any,JLD2.RelOffset}, ::Array{JLD2.ReadAttribute,1}) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:198
 [9] macro expansion at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:156 [inlined]
 [10] macro expansion at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datatypes.jl:76 [inlined]
 [11] read_data(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.ReadDataspace, ::UInt8, ::Int64, ::Int64, ::Int64, ::UInt16, ::JLD2.RelOffset, ::Array{JLD2.ReadAttribute,1}) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:152
 [12] load_dataset(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.RelOffset) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:92
 [13] jlconvert at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:674 [inlined]
 [14] macro expansion at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:1322 [inlined]
 [15] jlconvert(::JLD2.ReadRepresentation{Tuple{UInt64,Array{ChainDataFrame,1}},JLD2.OnDiskRepresentation{(0, 8),Tuple{UInt64,Array{ChainDataFrame,1}},Tuple{UInt64,JLD2.RelOffset}}()}, ::JLD2.JLDFile{JLD2.MmapIO}, ::Ptr{Nothing}, ::JLD2.RelOffset) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:1269
 [16] read_data(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.ReadRepresentation{Tuple{UInt64,Array{ChainDataFrame,1}},JLD2.OnDiskRepresentation{(0, 8),Tuple{UInt64,Array{ChainDataFrame,1}},Tuple{UInt64,JLD2.RelOffset}}()}, ::Array{JLD2.ReadAttribute,1}) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\dataio.jl:37
 [17] read_data(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.ReadDataspace, ::UInt8, ::Int64, ::Int64, ::Int64, ::UInt16, ::JLD2.RelOffset, ::Array{JLD2.ReadAttribute,1}) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:149
 [18] load_dataset(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.RelOffset) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:92
 [19] jlconvert at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:674 [inlined]
 [20] macro expansion at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:1322 [inlined]
 [21] jlconvert(::JLD2.ReadRepresentation{Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}},JLD2.OnDiskRepresentation{(0,),Tuple{Tuple{UInt64,Array{ChainDataFrame,1}}},Tuple{JLD2.RelOffset}}()}, ::JLD2.JLDFile{JLD2.MmapIO}, ::Ptr{Nothing}, ::JLD2.RelOffset) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:1269
 [22] read_data(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.ReadRepresentation{Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}},JLD2.OnDiskRepresentation{(0,),Tuple{Tuple{UInt64,Array{ChainDataFrame,1}}},Tuple{JLD2.RelOffset}}()}, ::Array{JLD2.ReadAttribute,1}) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\dataio.jl:37
 [23] read_data(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.ReadDataspace, ::UInt8, ::Int64, ::Int64, ::Int64, ::UInt16, ::JLD2.RelOffset, ::Array{JLD2.ReadAttribute,1}) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:149
 [24] load_dataset(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.RelOffset) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:92
 [25] jlconvert at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:674 [inlined]
 [26] macro expansion at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:1322 [inlined]
 [27] jlconvert(::JLD2.ReadRepresentation{NamedTuple{(:range, :model, :spl, :vi, :hashedsummary),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},DynamicPPL.Sampler{HMC{Turing.Core.ZygoteAD,(),AdvancedHMC.Adaptation.UnitEuclideanMetric},Turing.Inference.HMCState{DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},AdvancedHMC.StaticTrajectory{AdvancedHMC.EndPointTS,AdvancedHMC.Leapfrog{Float64}},AdvancedHMC.Adaptation.NoAdaptation,AdvancedHMC.PhasePoint{Array{Float64,1},AdvancedHMC.DualValue{Float64,Array{Float64,1}}}}},DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}}}},JLD2.OnDiskRepresentation{(0, 0, 8, 16, 96),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},Any,DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Any},Tuple{nothing,JLD2.OnDiskRepresentation{(0, 0, 8, 8),Tuple{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},Tuple{nothing,JLD2.OnDiskRepresentation{(0,),Tuple{Array{Int64,1}},Tuple{JLD2.RelOffset}}(),nothing,nothing}}(),JLD2.RelOffset,JLD2.OnDiskRepresentation{(0, 64, 72),Tuple{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Any,Any},Tuple{JLD2.OnDiskRepresentation{(0,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}},Tuple{JLD2.OnDiskRepresentation{(0, 8, 16, 24, 32, 40, 48, 56),Tuple{Dict{DynamicPPL.VarName{:p},Int64},Any,Any,Any,Any,Any,Any,Dict{String,BitArray{1}}},Tuple{JLD2.CustomSerialization{Array,JLD2.RelOffset},JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.CustomSerialization{Array,JLD2.RelOffset}}}()}}(),JLD2.RelOffset,JLD2.RelOffset}}(),JLD2.RelOffset}}()}, ::JLD2.JLDFile{JLD2.MmapIO}, ::Ptr{Nothing}, ::JLD2.RelOffset) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:1269
 [28] jlconvert(::JLD2.ReadRepresentation{Chains{Real,Missing,NamedTuple{(:internals, :parameters),Tuple{Array{String,1},Array{String,1}}},NamedTuple{(:range, :model, :spl, :vi, :hashedsummary),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},DynamicPPL.Sampler{HMC{Turing.Core.ZygoteAD,(),AdvancedHMC.Adaptation.UnitEuclideanMetric},Turing.Inference.HMCState{DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},AdvancedHMC.StaticTrajectory{AdvancedHMC.EndPointTS,AdvancedHMC.Leapfrog{Float64}},AdvancedHMC.Adaptation.NoAdaptation,AdvancedHMC.PhasePoint{Array{Float64,1},AdvancedHMC.DualValue{Float64,Array{Float64,1}}}}},DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}}}}},JLD2.OnDiskRepresentation{(0, 8, 8, 24),Tuple{Any,Missing,NamedTuple{(:internals, :parameters),Tuple{Array{String,1},Array{String,1}}},NamedTuple{(:range, :model, :spl, :vi, :hashedsummary),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},DynamicPPL.Sampler{HMC{Turing.Core.ZygoteAD,(),AdvancedHMC.Adaptation.UnitEuclideanMetric},Turing.Inference.HMCState{DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},AdvancedHMC.StaticTrajectory{AdvancedHMC.EndPointTS,AdvancedHMC.Leapfrog{Float64}},AdvancedHMC.Adaptation.NoAdaptation,AdvancedHMC.PhasePoint{Array{Float64,1},AdvancedHMC.DualValue{Float64,Array{Float64,1}}}}},DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}}}}},Tuple{JLD2.RelOffset,nothing,JLD2.OnDiskRepresentation{(0, 8),Tuple{Array{String,1},Array{String,1}},Tuple{JLD2.RelOffset,JLD2.RelOffset}}(),JLD2.OnDiskRepresentation{(0, 0, 8, 16, 96),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},Any,DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Any},Tuple{nothing,JLD2.OnDiskRepresentation{(0, 0, 8, 8),Tuple{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},Tuple{nothing,JLD2.OnDiskRepresentation{(0,),Tuple{Array{Int64,1}},Tuple{JLD2.RelOffset}}(),nothing,nothing}}(),JLD2.RelOffset,JLD2.OnDiskRepresentation{(0, 64, 72),Tuple{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Any,Any},Tuple{JLD2.OnDiskRepresentation{(0,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}},Tuple{JLD2.OnDiskRepresentation{(0, 8, 16, 24, 32, 40, 48, 56),Tuple{Dict{DynamicPPL.VarName{:p},Int64},Any,Any,Any,Any,Any,Any,Dict{String,BitArray{1}}},Tuple{JLD2.CustomSerialization{Array,JLD2.RelOffset},JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.CustomSerialization{Array,JLD2.RelOffset}}}()}}(),JLD2.RelOffset,JLD2.RelOffset}}(),JLD2.RelOffset}}()}}()}, ::JLD2.JLDFile{JLD2.MmapIO}, ::Ptr{Nothing}, ::JLD2.RelOffset) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\data.jl:1322
 [29] read_data(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.ReadRepresentation{Chains{Real,Missing,NamedTuple{(:internals, :parameters),Tuple{Array{String,1},Array{String,1}}},NamedTuple{(:range, :model, :spl, :vi, :hashedsummary),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},DynamicPPL.Sampler{HMC{Turing.Core.ZygoteAD,(),AdvancedHMC.Adaptation.UnitEuclideanMetric},Turing.Inference.HMCState{DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},AdvancedHMC.StaticTrajectory{AdvancedHMC.EndPointTS,AdvancedHMC.Leapfrog{Float64}},AdvancedHMC.Adaptation.NoAdaptation,AdvancedHMC.PhasePoint{Array{Float64,1},AdvancedHMC.DualValue{Float64,Array{Float64,1}}}}},DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}}}}},JLD2.OnDiskRepresentation{(0, 8, 8, 24),Tuple{Any,Missing,NamedTuple{(:internals, :parameters),Tuple{Array{String,1},Array{String,1}}},NamedTuple{(:range, :model, :spl, :vi, :hashedsummary),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},DynamicPPL.Sampler{HMC{Turing.Core.ZygoteAD,(),AdvancedHMC.Adaptation.UnitEuclideanMetric},Turing.Inference.HMCState{DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},AdvancedHMC.StaticTrajectory{AdvancedHMC.EndPointTS,AdvancedHMC.Leapfrog{Float64}},AdvancedHMC.Adaptation.NoAdaptation,AdvancedHMC.PhasePoint{Array{Float64,1},AdvancedHMC.DualValue{Float64,Array{Float64,1}}}}},DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}}}}},Tuple{JLD2.RelOffset,nothing,JLD2.OnDiskRepresentation{(0, 8),Tuple{Array{String,1},Array{String,1}},Tuple{JLD2.RelOffset,JLD2.RelOffset}}(),JLD2.OnDiskRepresentation{(0, 0, 8, 16, 96),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},Any,DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Any},Tuple{nothing,JLD2.OnDiskRepresentation{(0, 0, 8, 8),Tuple{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},Tuple{nothing,JLD2.OnDiskRepresentation{(0,),Tuple{Array{Int64,1}},Tuple{JLD2.RelOffset}}(),nothing,nothing}}(),JLD2.RelOffset,JLD2.OnDiskRepresentation{(0, 64, 72),Tuple{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Any,Any},Tuple{JLD2.OnDiskRepresentation{(0,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}},Tuple{JLD2.OnDiskRepresentation{(0, 8, 16, 24, 32, 40, 48, 56),Tuple{Dict{DynamicPPL.VarName{:p},Int64},Any,Any,Any,Any,Any,Any,Dict{String,BitArray{1}}},Tuple{JLD2.CustomSerialization{Array,JLD2.RelOffset},JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.RelOffset,JLD2.CustomSerialization{Array,JLD2.RelOffset}}}()}}(),JLD2.RelOffset,JLD2.RelOffset}}(),JLD2.RelOffset}}()}}()}, ::Array{JLD2.ReadAttribute,1}) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\dataio.jl:37
 [30] read_data(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.ReadDataspace, ::UInt8, ::Int64, ::Int64, ::Int64, ::UInt16, ::JLD2.RelOffset, ::Array{JLD2.ReadAttribute,1}) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:149
 [31] load_dataset(::JLD2.JLDFile{JLD2.MmapIO}, ::JLD2.RelOffset) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\datasets.jl:92
 [32] getindex(::JLD2.Group{JLD2.JLDFile{JLD2.MmapIO}}, ::String) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\groups.jl:108
 [33] read(::JLD2.JLDFile{JLD2.MmapIO}, ::String) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\JLD2.jl:326
 [34] #27 at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\loadsave.jl:112 [inlined]
 [35] #jldopen#31(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(jldopen), ::var"#27#28", ::String) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\loadsave.jl:4
 [36] jldopen(::Function, ::String) at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\loadsave.jl:2
 [37] top-level scope at C:\Users\z5168736\.julia\packages\JLD2\hB4ya\src\loadsave.jl:111

Moreover, I tried to write and read the file using commands mentioned in MCMCChains.j documentation as below:
write("chain_i.jls", chain)
When I tried to read using below commands,
chn2 = read("chain_i.jls", chain), I got error like below,

ERROR: MethodError: no method matching read(::IOStream, ::Chains{Real,Missing,NamedTuple{(:internals, :parameters),Tuple{Array{String,1},Array{String,1}}},NamedTuple{(:range, :model, :spl, :vi, :hashedsummary),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},DynamicPPL.Sampler{HMC{Turing.Core.ZygoteAD,(),AdvancedHMC.Adaptation.UnitEuclideanMetric},Turing.Inference.HMCState{DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},AdvancedHMC.StaticTrajectory{AdvancedHMC.EndPointTS,AdvancedHMC.Leapfrog{Float64}},AdvancedHMC.Adaptation.NoAdaptation,AdvancedHMC.PhasePoint{Array{Float64,1},AdvancedHMC.DualValue{Float64,Array{Float64,1}}}}},DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}}}}})
Closest candidates are:
  read(::IOStream) at iostream.jl:453
  read(::IOStream, ::Type{UInt8}) at iostream.jl:348
  read(::IOStream, ::Union{Type{Int16}, Type{Int32}, Type{Int64}, Type{UInt16}, Type{UInt32}, Type{UInt64}}) at iostream.jl:357
  ...
Stacktrace:
 [1] (::Base.var"#278#279"{Tuple{Chains{Real,Missing,NamedTuple{(:internals, :parameters),Tuple{Array{String,1},Array{String,1}}},NamedTuple{(:range, :model, :spl, :vi, :hashedsummary),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},DynamicPPL.Sampler{HMC{Turing.Core.ZygoteAD,(),AdvancedHMC.Adaptation.UnitEuclideanMetric},Turing.Inference.HMCState{DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},AdvancedHMC.StaticTrajectory{AdvancedHMC.EndPointTS,AdvancedHMC.Leapfrog{Float64}},AdvancedHMC.Adaptation.NoAdaptation,AdvancedHMC.PhasePoint{Array{Float64,1},AdvancedHMC.DualValue{Float64,Array{Float64,1}}}}},DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}}}}}}})(::IOStream) at .\io.jl:379
 [2] #open#271(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(open), ::Base.var"#278#279"{Tuple{Chains{Real,Missing,NamedTuple{(:internals, :parameters),Tuple{Array{String,1},Array{String,1}}},NamedTuple{(:range, :model, :spl, :vi, :hashedsummary),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},DynamicPPL.Sampler{HMC{Turing.Core.ZygoteAD,(),AdvancedHMC.Adaptation.UnitEuclideanMetric},Turing.Inference.HMCState{DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},AdvancedHMC.StaticTrajectory{AdvancedHMC.EndPointTS,AdvancedHMC.Leapfrog{Float64}},AdvancedHMC.Adaptation.NoAdaptation,AdvancedHMC.PhasePoint{Array{Float64,1},AdvancedHMC.DualValue{Float64,Array{Float64,1}}}}},DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}}}}}}}, ::String) at .\io.jl:298
 [3] open(::Function, ::String) at .\io.jl:296
 [4] read(::String, ::Chains{Real,Missing,NamedTuple{(:internals, :parameters),Tuple{Array{String,1},Array{String,1}}},NamedTuple{(:range, :model, :spl, :vi, :hashedsummary),Tuple{Random._GLOBAL_RNG,DynamicPPL.Model{var"##inner_function#608#24",NamedTuple{(:y,),Tuple{Array{Int64,1}}},DynamicPPL.ModelGen{(:y,),var"###coinflip#622",NamedTuple{(),Tuple{}}},Val{()}},DynamicPPL.Sampler{HMC{Turing.Core.ZygoteAD,(),AdvancedHMC.Adaptation.UnitEuclideanMetric},Turing.Inference.HMCState{DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},AdvancedHMC.StaticTrajectory{AdvancedHMC.EndPointTS,AdvancedHMC.Leapfrog{Float64}},AdvancedHMC.Adaptation.NoAdaptation,AdvancedHMC.PhasePoint{Array{Float64,1},AdvancedHMC.DualValue{Float64,Array{Float64,1}}}}},DynamicPPL.VarInfo{NamedTuple{(:p,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:p},Int64},Array{Beta{Float64},1},Array{DynamicPPL.VarName{:p},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},Base.RefValue{Tuple{UInt64,Array{ChainDataFrame,1}}}}}}) at .\io.jl:379
 [5] top-level scope at REPL[146]:1

How can I solve this error?

Thanks in advance
Manu

CC: @cpfiffer @devmotion

JLD doesn’t appear to work out of the box, but I’m not sure why. Try using chain serialization (fixed below) instead, though we should probably get JLD working at some point because it’s a much more stable storage format that serialization.

You need to specify the type you want to receive in read. Currently you are calling read with a Chains object you already have. You need to do this instead:

chn2 = read("chain_i.jls", Chains)

Here’s a full copy-pasteable working example:

using Random
using Turing

# Set the true probability of heads in a coin.
p_true = 0.5

# Iterate from having seen 0 observations to 100 observations.
Ns = 0:100;

# Draw data from a Bernoulli distribution, i.e. draw heads or tails.
Random.seed!(12)
data = rand(Bernoulli(p_true), last(Ns))
# g_data = CuArray(data)

# Declare our Turing model.
@model coinflip(y) = begin
    # Our prior belief about the probability of heads in a coin.
    p ~ Beta(1, 1)

    # The number of observations.
    N = length(y)
    for n in 1:N
        # Heads or tails of a coin are drawn from a Bernoulli distribution.
        y[n] ~ Bernoulli(p)
    end
end;

# Settings of the Hamiltonian Monte Carlo (HMC) sampler.
# iterations = 1000
ϵ = 0.05
Ď„ = 10
iterations = 1000
# Start sampling.
chain = sample(coinflip(data), HMC(ϵ, τ), iterations);

write("chain.jls", chain)

chn2 = read("chain.jls", Chains)
1 Like

@cpfiffer: Sorry to disturb you. I misunderstood Chains in chn2 = read("chain.jls", Chains) as the chain name… But now it is clear that Chains is the object type.
Thanks you
Manu

Hello, I have the same error when saving the MCMC strings. Could someone help me with this problem. Thank you so much.

MethodError: no method matching write(::IOStream, ::Chains{Float64, AxisArrays.AxisArray{Float64, 3, Array{Float64, 3}, Tuple{AxisArrays.Axis{:iter, StepRange{Int64, Int64}}, AxisArrays.Axis{:var, Vector{Symbol}}, AxisArrays.Axis{:chain, UnitRange{Int64}}}}, Missing, NamedTuple{(:parameters, :internals), Tuple{Vector{Symbol}, Vector{Symbol}}}, NamedTuple{(:start_time, :stop_time), Tuple{Vector{Float64}, Vector{Float64}}}})
Closest candidates are:
  write(::IO, ::Any) at io.jl:672
  write(::IO, ::Any, ::Any...) at io.jl:673
  write(::FilePathsBase.AbstractPath, ::Any) at C:\Users\alian\.julia\packages\FilePathsBase\9kSEl\src\path.jl:771

Hello ALIAN, I have found an alternative to save chains in Turing.
We have the chains from the package example.

chain = sample(model, NUTS(0.65), MCMCSerial(), 1000, 3; progress=false

We use the JLD2 package:

using Pkg
Pkg.add(“JLD2”)

file = “C:/Users/Pc/Documents/CODE/Bayesian Statistics/2. EDO/Lotka-Volterra/ChainS.jld2” # Specify the path and name of the file
data = Dict(“chain” => chain) # Create a dictionary to store the chains
save(file, data) # Save the chains to the file

Then to read the chains:

data = load(file)
chainguarded = data[“chain”]

Muchas Gracias Rafa, a ver si lo pruebo entonces. Saludos.