Get Turing's model predictions after optimization

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

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?

1 Like

Checkout the returned function described here.

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

I see the problem now. There is a method 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.

1 Like

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.

Thanks to both of you, it works.

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
1 Like

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 :slight_smile: create `returned()` method for results from mode estimation · Issue #2607 · TuringLang/Turing.jl · GitHub