# How to save to disk my results so far?

**URL:** <https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942>\
**Category:** New to Julia\
**Tags:** question, turing\
**Created:** [May 7, 2020, 9:47am UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942 "2020-05-07T09:47:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manoj\_Gopalkrishnan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj_gopalkrishnan/32/14587_2.png) [@Manoj\_Gopalkrishnan](https://discourse.julialang.org/u/Manoj_Gopalkrishnan)\
**Post date:** [May 7, 2020, 9:47am UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/1 "2020-05-07T09:47:59Z")

</div>

I am using Turing from IJulia in Jupyter notebook. I am running some MCMC simulations which are taking some time to converge. How do I save the chains to disk so that I can reload them next time I start the kernel instead of having to run the computation again? I’m not sure if this is a question specific to Turing and Julia, or a more general Jupyter Notebook question.

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [May 7, 2020, 10:00am UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/2 "2020-05-07T10:00:21Z")

</div>

cc: @cpfiffer

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [May 7, 2020, 1:42pm UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/3 "2020-05-07T13:42:51Z")

</div>

You should be able to load and save a chain with this:

```julia
# Save a chain.
write("chain-file.jls", chn)

# Read a chain.
chn2 = read("chain-file.jls", Chains)

```

Let me know if this doesn’t work for you. I have long wanted to improve the saving functionality, so if this doesn’t work I might get around to moving us over to JLD storage for good.

---

<div class="post-metadata">

**Author:** ![Manoj\_Gopalkrishnan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj_gopalkrishnan/32/14587_2.png) [@Manoj\_Gopalkrishnan](https://discourse.julialang.org/u/Manoj_Gopalkrishnan)\
**Post date:** [May 7, 2020, 2:37pm UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/4 "2020-05-07T14:37:19Z")

</div>

> [@cpfiffer](#):
>
> hn2 = read(“chain-file.jls”, Chains)

Yes this worked fine! I am able to write to disk, recover and describe(chn2) also returns all the stats.

I have a follow-up question. I tried

```julia
resume(chn2,40)

```

and got the error message

```julia
AssertionError: [Turing] cannot resume from a chain without state info

```

I tried chn2.info and I get

```julia
NamedTuple()

```

So where is this going wrong?

---

<div class="post-metadata">

**Author:** ![Manoj\_Gopalkrishnan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj_gopalkrishnan/32/14587_2.png) [@Manoj\_Gopalkrishnan](https://discourse.julialang.org/u/Manoj_Gopalkrishnan)\
**Post date:** [May 7, 2020, 2:40pm UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/5 "2020-05-07T14:40:38Z")

</div>

To be clear, even the original chain refuses to resume, so this is not an issue with the write and read. Probably just me not able to figure out how to resume.

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [May 7, 2020, 3:19pm UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/6 "2020-05-07T15:19:22Z")

</div>

Ah, I see. We made a change a little while ago that requires you to run sampling with the keyword `save_state=true`, i.e.

```julia
chain = sample(model, sampler, n_samples; save_state=true)

```

After which, `resume` should work. But one issue with this is that `write`/`read` don’t work very well with the save state, and so you might also get an error here.

I think the biggest issue is that we serialize the model to disk, which is the biggest pain point. The signature for `resume` should probably be `resume(model, chain, n_samples)` since users already have their model definitions and saving it Turing-side is difficult.

If using `save_state=true` doesn’t work for you, could you open an issue at [MCMCChains.jl](https://github.com/TuringLang/MCMCChains.jl) to remind me?

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [May 7, 2020, 3:26pm UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/7 "2020-05-07T15:26:21Z")

</div>

Feels like we should really add some tests into Turing for this.

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [May 7, 2020, 3:34pm UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/8 "2020-05-07T15:34:45Z")

</div>

I mean, the problem is not even testing (though it would help). A lot of the chain I/O code is _very_ old (basically still the original Mamba code), and has not been seriously re-evaluated since. I have long wanted to modernize it but haven’t yet gotten to it.

---

<div class="post-metadata">

**Author:** ![Manoj\_Gopalkrishnan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj_gopalkrishnan/32/14587_2.png) [@Manoj\_Gopalkrishnan](https://discourse.julialang.org/u/Manoj_Gopalkrishnan)\
**Post date:** [May 8, 2020, 1:00pm UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/9 "2020-05-08T13:00:10Z")

</div>

So I managed to add the save\_state=true flag, and this means I can now run my chain for a short number of steps, plot some graphs, and then resume if needed. This is great!

Writing chain to disk and read also succeeded, so I can turn my kernel off and on again, without having to run all this sampling once more! So it’s working, at least for now…

By the way, I have been getting a very weird error. It only happens when I type HMC into a Jupyter cell. When I start typing parameters HMC(.05,10) into HMC, when I reach the point where I type

```julia
HMC(0.)

```

the cell freezes. Any key I press, delete, doesn’t respond, or it copies the previous line of the notebook many times, with the same line number many times. This is really weird, and I have been able to reproduce the same thing many times.

I know this is not a minimal example, but I am working to a deadline, and got around this by not using HMC for now. NUTS works great, so I’m just using that for now. But thought I should let folks known in case this is some known bug.

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [May 8, 2020, 1:43pm UTC](https://discourse.julialang.org/t/how-to-save-to-disk-my-results-so-far/38942/10 "2020-05-08T13:43:33Z")

</div>

That sounds very strange. Not sure this is Turing related though. You could try a different IDE and see if the problem still occurs.

To list a few:

- [GitHub - julia-vscode/julia-vscode: Julia extension for Visual Studio Code](https://github.com/julia-vscode/julia-vscode)
- [https://junolab.org](https://junolab.org)
- [GitHub - JuliaEditorSupport/julia-vim: Vim support for Julia.](https://github.com/JuliaEditorSupport/julia-vim)
