You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I'm trying to do some stuff in Lua and send it back to Terra (maybe sounds familiar from my past stuff). Long story short, I modified ad.t to get it to work, I'm just checking to see if I'm thinking on the right lines.
Here's my model, with the cast of my lua function right before it:
function num.metamethods.__cast(from, to, exp)
if from:isfloat() and to == num then
return `num { DualNumBase.new(exp, nil) }
elseif from == num and to:isfloat() then
return `[to](exp:val())
else
error(string.format("ad.t: Cannot cast '%s' to '%s'", tostring(from), tostring(to)))
end
end
Is this close to what needs to happen?
The text was updated successfully, but these errors were encountered:
chrisranderson
changed the title
Casting from dualnum for use in Lua functions
Casting from dualnum to float for use in Lua functions
Sep 19, 2016
This looks like it will work (as in not crash), but there are a couple issues I can see with it:
It's been a while since I've dug into Terra details, so I'm not sure how often / where it does implicit conversions using the __cast metamethod. With the change you've made to num:__cast, it might be possible to run into a situation where you're calling a function on a num, and that function has an overload for num, but it instead converts to float and invokes the float overload. I know I avoided adding the num to float cast for this reason. Maybe this isn't actually a problem; closer reading of the Terra documentation / experience with more code might be illuminating.
More importantly: by casting down to float, you're breaking the flow of derivatives between the random variables and the likelihood, which will dramatically drop HMC's performance (I assume you're using HMC). Basically, it'll behave as a random walk sampler with respect to any factor statements you have. You'd be better off implementing your likelihood functions (e.g. terra_get_image_diff) as autodifferentiable Terra functions if you want to get benefits from HMC.
So I'm trying to do some stuff in Lua and send it back to Terra (maybe sounds familiar from my past stuff). Long story short, I modified
ad.t
to get it to work, I'm just checking to see if I'm thinking on the right lines.Here's my model, with the cast of my lua function right before it:
(full file here)
I would run it and get this error:
I fixed it like this, at this line in ad.t:
Is this close to what needs to happen?
The text was updated successfully, but these errors were encountered: