Why all of my MCMC samples are identical?

I am inferring some parameters of a neural network via MCMC. I noticed all of the samples returned by MCMC are identical. Note that I am not fixing the random seed for Pyro. Am I doing something wrong?

Here’s how I define my neural network model and use MCMC to get a posterior over v_q:

class model(nn.Module):
    def __init__(self):
        super(model, self).__init__()
        . # define model layers

    def infer_v_q(self, x_q, sigma):
        with pyro.plate("data", x.shape[0]):
                # v_q.shape = (batch_dim, 7), batch_dim=36
                v_q = pyro.sample('v_q', pyro.distributions.Uniform(v_q_min, v_q_max).to_event(1))
                u = self.generation_network(v_q)
                return pyro.sample("x", pyro.distributions.Normal(u, sigma).to_event(3), obs=x_q)

# Here's how I run MCMC inference
nuts_kernel = pyro.infer.NUTS(model.infer_v_q, adapt_step_size=True, step_size=1e-7, target_accept_prob=0.7, max_tree_depth=2)
mcmc = pyro.infer.MCMC(nuts_kernel, num_samples=200, warmup_steps=100, num_chains=1)
# After inference, estimated step_size is around 1e-8 and acceptance probability is around 70%

# get samples
v_q_pred = mcmc.get_samples()["v_q"]

cond = True
for num_sample1 in range(200):
    for num_sample2 in range(1, 200):
        cond = cond and (torch.isclose(v_q_pred[num_sample1], v_q_pred[num_sample2], atol=1e-5).min().item())

print(cond) # prints True

I guess this is one reason. You only move around within 3 leapfrog steps, with a pretty small step_size.

1 Like

The problem is if I set adapt_step_size=False and set step_size=1e-3 acceptance probability goes down to 0 quickly (nearly 40% of the samples are still identical) regardless of what I set for max_tree_depth. What should I do about that? Is there a way to use adaptive step size but set a minimum for step_size?

I also have a side question: what is a good range of value for target acceptance probability? I know that in general an acceptance probability of 0.2-0.35 should be good but not higher or lower than that. However, it is a bit counter-intuitive that setting target_accept_prob to a higher value causes inference to take longer. So I’m wondering if target_accept_prob has a different interpretation in Pyro?

I think a good range of target accept prob is 0.6 -> 0.9. Currently, there is no way to set a minimum step_size under adaptive_step_size=True. It seems that your model is highly unstable (e.g. there are so many parameters) so NUTS/HMC rarely accepts new proposals. You can try to use other init_strategy (e.g. init_to_uniform(radius=0.1)). I guess it is better to use SVI to find the optimal points of the parameters before trying MCMC.

1 Like

Yes unfortunately the number of parameters is pretty high in my model and this is related to a thread that I opened earlier. I looks like it is not possible to enforce Pyro to estimate a posterior only for a subset of the variables in the sample site and I have to use MCMC methods unfortunately … .

This belongs to the modeling phase. You can just simply replace all x = pyro.sample(...) by x = torch.tensor(1.) or using condition.

1 Like