-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Potential bug using scan to create random variable in PyMC #173
Comments
Sounds like the problem is simply not passing |
Thanks @ricardoV94! Besides passing |
Only important for forward sampling I think. If you use |
Ah, the gradient (but not the logp) raises an error if you have a scan without the explicit updates... strange. sigma_rv = at.random.halfnormal(0, 5.0, name="sigma")
mu_rv = at.random.normal(0, 1.0, name="mu")
def step(x_tm1, sigma_rv):
x = at.random.normal(x_tm1, sigma_rv)
return x #, {x.owner.inputs[0]: x.owner.outputs[0]}
scan_rv, updates = aesara.scan(
fn=step,
outputs_info=[{"initial": mu_rv}],
n_steps=num_timesteps,
non_sequences=[sigma_rv],
)
scan_rv.name = "scan"
sigma_vv = sigma_rv.clone()
mu_vv = mu_rv.clone()
scan_vv = scan_rv.clone()
logp_dict = aeppl.factorized_joint_logprob({
sigma_rv: sigma_vv,
mu_rv: mu_vv,
scan_rv: scan_vv,
})
# The next line raises
at.grad(logp_dict[scan_vv].sum(), wrt=sigma_vv) TypeError: Tensor type field must be a TensorType; found <class 'aesara.tensor.random.type.RandomGeneratorType'>. |
@junpenglao Does this still fail? If it works by modifying the original example would you mind sharing the modified version? |
Yes, this is the working implementation: with pm.Model(coords={"timestep": np.arange(num_timesteps)}) as m:
sigma = pm.HalfNormal("sigma", 5.0)
mu = pm.Normal("mu", 0.0, 1.0)
def step(x_tm1, sigma):
x = pm.Normal.dist(x_tm1, sigma)
# x = at.random.normal(x_tm1, sigma)
# Return the new variable and the RNG update expression
return x, {x.owner.inputs[0]: x.owner.outputs[0]}
X_rv, updates = aesara.scan(
fn=step, outputs_info=[mu], non_sequences=[sigma], n_steps=num_timesteps
)
m.register_rv(X_rv, name="X_rv", data=data)
# X_rv = pm.GaussianRandomWalk("X_rv", mu, sigma, observed=data)
idata = pm.sample() |
Currently, a simple random walk example fails for me:
with:
Inspecting with aeppl seems to indicate it does not recognize the RV result from scan:
cc @ricardoV94
The text was updated successfully, but these errors were encountered: