juliaturing-machines

Unable to export Chains class from Julia's Turing library


I am using Julia's Turing library to do the analysis using the MCMC method, and I am able to get the output of type Chains and store it in the variable chain. However, I am unable to write it out to a .jis file. I get the following error.Please let me know how to solve it.

code

write(".jis", chain)

error log

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{Float64, Float64}}})
Closest candidates are:
  write(::IO, ::Any) at io.jl:672
  write(::IO, ::Any, !Matched::Any...) at io.jl:673
  write(!Matched::FilePathsBase.AbstractPath, ::Any) at ~/.julia/packages/FilePathsBase/9kSEl/src/path.jl:771
  ...

Solution

  • The question is not entirely clear. But a possible answer is to use serialization/deserialization support of Julia. This can be done in the following way:

    using Serialization
    using Turing
    
    # calculate chain here
    #
    # chain = ...
    #
    
    f = open("thefile.jls", "w")
    serialize(f, chain)
    close(f)
    

    and then to read the chain in a new session:

    using Serialization
    using Turing
    
    f = open("thefile.jls", "r")
    chain = deserialize(f)
    close(f)