# Get Turing's model predictions after optimization

**URL:** <https://discourse.julialang.org/t/get-turings-model-predictions-after-optimization/130275>\
**Category:** Probabilistic Programming\
**Created:** [June 27, 2025, 10:34am UTC](https://discourse.julialang.org/t/get-turings-model-predictions-after-optimization/130275 "2025-06-27T10:34:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![khoffie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/khoffie/32/215904_2.png) [@khoffie](https://discourse.julialang.org/u/khoffie)\
**Post date:** [June 27, 2025, 10:34am UTC](https://discourse.julialang.org/t/get-turings-model-predictions-after-optimization/130275/1 "2025-06-27T10:34:14Z")

</div>

Hi, since Turing 0.35 generated\_quantities() is deprecated. So how do I now get my model’s predictions? Before 0.35 I did something like this

```julia
function predict(mdl, mles)
    ## mdl is an instantiated Turing model
    params = string.(names(mles.values)...)
    preds = generated_quantities(mdl, mles.values, params)
    return preds
end

```

I could write a function like expectation(params, data) that I use inside the model to get the expectation and then after fit to get the predictions, but that is rather cumbersome I feel. Is there an easier solution?

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [June 27, 2025, 11:23am UTC](https://discourse.julialang.org/t/get-turings-model-predictions-after-optimization/130275/2 "2025-06-27T11:23:57Z")

</div>

Checkout the `returned` function described [here](https://turinglang.org/docs/usage/tracking-extra-quantities/index.html#using-returned-post-inference).

---

<div class="post-metadata">

**Author:** ![khoffie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/khoffie/32/215904_2.png) [@khoffie](https://discourse.julialang.org/u/khoffie)\
**Post date:** [June 27, 2025, 11:40am UTC](https://discourse.julialang.org/t/get-turings-model-predictions-after-optimization/130275/3 "2025-06-27T11:40:06Z")

</div>

Thanks, but I need to sample for this to work, if I understand correctly. However, I don’t sample, I do mode estimation as described here

> **[Mode Estimation – Turing.jl](https://turinglang.org/docs/usage/mode-estimation/)**

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [June 27, 2025, 6:23pm UTC](https://discourse.julialang.org/t/get-turings-model-predictions-after-optimization/130275/4 "2025-06-27T18:23:35Z")

</div>

I see the problem now. There is a [method](https://github.com/TuringLang/DynamicPPL.jl/blob/968c879730fa506f2886ea6c81d8fe4e89d50482/src/model.jl#L1201) that accepts a `NamedTuple` as an input for the parameters. One hack would be to convert your mode estimation result into a `NamedTuple`. As a long term solution, maybe a method could be created to work specifically with mode estimators.

---

<div class="post-metadata">

**Author:** ![eteppo](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@eteppo](https://discourse.julialang.org/u/eteppo)\
**Post date:** [June 27, 2025, 8:24pm UTC](https://discourse.julialang.org/t/get-turings-model-predictions-after-optimization/130275/5 "2025-06-27T20:24:06Z")

</div>

Your predict function doesn’t work if you replace `generated_quantities` with `returned`? It looks like it’s the same. `returned` uses `fix` on the model and then calls it so this would be another way to write it.

---

<div class="post-metadata">

**Author:** ![khoffie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/khoffie/32/215904_2.png) [@khoffie](https://discourse.julialang.org/u/khoffie)\
**Post date:** [June 28, 2025, 7:02am UTC](https://discourse.julialang.org/t/get-turings-model-predictions-after-optimization/130275/6 "2025-06-28T07:02:11Z")

</div>

Thanks to both of you, it works.

```julia
function predict(mdl, mles)
    ## mdl is an instantiated Turing model
    vs = Tuple(mles.values)
    ks = Tuple(names(mles.values)[1])
    preds = returned(mdl, NamedTuple{ks}(vs))
    return preds
end

```

---

<div class="post-metadata">

**Author:** ![penelopeysm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/penelopeysm/32/213172_2.png) [@penelopeysm](https://discourse.julialang.org/u/penelopeysm)\
**Post date:** [June 29, 2025, 7:01pm UTC](https://discourse.julialang.org/t/get-turings-model-predictions-after-optimization/130275/7 "2025-06-29T19:01:03Z")

</div>

The API of the mode estimation bit in Turing is a bit undeveloped compared to the MCMC side I think. Will put this on the list 🙂 [create `returned()` method for results from mode estimation · Issue #2607 · TuringLang/Turing.jl · GitHub](https://github.com/TuringLang/Turing.jl/issues/2607)
