Hi, all. I am confusing if I use eval function like
eval(Meta.parse("sigma ~ Normal(0,1)"))
does it will affect the speed of model?
Hi, all. I am confusing if I use eval function like
eval(Meta.parse("sigma ~ Normal(0,1)"))
does it will affect the speed of model?
If I understand your question correctly, then the answer is no. The resulting model does not know how it was constructed.
However, before construction you have additional overhead of Meta.parse
and eval
of course. Additionally, the latter means that Julia cannot infer the type so further use of the resulting model is type unstable and incurs dynamic dispatches (which you could restrict using a function barrier).
But more likely than not you don’t need eval(Meta.parse(..))
. Would you mind explaining why you think you need this?
It doesn’t work anyway:
julia> using Turing
julia> @model function f()
eval(Meta.parse("sigma ~ Normal()"))
end
f (generic function with 2 methods)
julia> f()()
ERROR: UndefVarError: `sigma` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
The model macro acts on the contents before eval
and parse
can get to it.
I am a bit confused how your post relates to OP and I am no expert in Turing but that’s not how I interpret the error message. You see eval
evaluates the expression in global scope and there is no variable sigma
in global scope. I am fairly sure that the macro @model
does not reach inside the eval
and does not transform the expression and that’s why you are seeing the error.
If these two posts belong to the same underlying problem then you should try to explain that in order to get suitable help.
OP is asking whether using eval(Meta.parse(...))
in a Turing model makes it slower. My point is that they can’t do that, it will error, so the question of whether it’s faster or not is not well-founded.
Yes, you’re saying the same thing as me (although I was admittedly much more terse and less helpful). @model
does not transform the string "sigma ~ ..."
but that’s precisely because the contents of @model ...
are expanded, before eval(parse(...))
which is only executed at runtime. Consequently attempting to run sigma ~ ...
at runtime then gives the error as shown.