ValueError: Must provide valid initial parameters to begin sampling when using `potential_fn` in HMC/NUTS kernel

Hi All,

I’m a new user to using pyro and wanted to use pyro for sampling from a model I have written within PyTorch. The model itself is a R^N to R^2 function where it takes a vector and outputs the signed-log determinant of a matrix. I can use this model to create a probability distribution by taking the log-abs value and multiple it by 2, this is equivalent to taking the function and squaring it, then taking its log!
I.e. my model returns

sign, logabsdet = net(X)
pdf = logabsdet.mul(2) 
#this is equal to
det = sign*torch.exp(logabsdet)
pdf = torch.log(det**2)

I asked a question a few months ago (Can the MCMC modules in Pyro be used sample inputs of a PyTorch model?) about how to sample inputs from a model directly. I.e., how to generate the input X in the example above via some MCMC method! I’ve followed the answers within this other topic posted above but I get the following error,

"ValueError: Must provide valid initial parameters to begin sampling when using potential_fn in HMC/NUTS kernel."

net = mynetwork(args...)

hmc_kernel = pyro.infer.mcmc.HMC(potential_fn=net, step_size=1, target_accept_prob=0.5)
mcmc = pyro.infer.MCMC(hmc_kernel, num_samples=1, warmup_steps=10, num_chains=4096) #crashes here
samples = mcmc.get_samples()

The full error message is here,

Traceback (most recent call last):
  File "", line 53, in <module>
    mcmc = pyro.infer.MCMC(hmc_kernel, num_samples=1, warmup_steps=10, num_chains=4096)
  File "~/anaconda3/lib/python3.8/site-packages/pyro/infer/mcmc/", line 476, in __init__
  File "~/anaconda3/lib/python3.8/site-packages/pyro/infer/mcmc/", line 389, in _validate_kernel
    raise ValueError(
ValueError: Must provide valid initial parameters to begin sampling when using `potential_fn` in HMC/NUTS kernel.

I’ve tried adding a normal dist. to the initial params but it doesn’t work. So I feel like I’m doing a novice mistake here that is quite easily diagnosable!

mcmc = pyro.infer.MCMC(hmc_kernel, num_samples=1, warmup_steps=10, num_chains=4096, initial_params=pyro.distributions.Normal(loc=0, scale=1)) #fails too

Any help would be appreciated! Thank you in advance! :slight_smile:

Hi @AlphaBetaGamma96, you need to provide the init values for your potential function. If potential function is your neural network, you are using NUTS to draw samples for your net input - so you need to provide an initial value of your net input. I guess it is a tensor in your case. If you want to draw samples of your net parameters, don’t use potential_fn because your net is not a potential function of its parameter. You will need to construct a Pyro model with your net, set priors for your net’s parameters, and provide model (rather than potential_fn) to your mcmc kernel. You can find references at pyro.module, pyro.random_module,…

1 Like

Hi @fehiepsi! Thanks for the quick response!

Yes, I’d like to sample inputs to my network, rather than the parameters of my network! So, if I wanted the initial values of my input to distributed from say a Gaussian. I could just do torch.randn(num_chains, num_input) ? (or something like this)

From the docs, it seems that the initial_params argument takes a dictionary so I’m not exactly sure what the corresponding key should be for this? So, if my initial values is something like torch.randn(num_chains, num_inputs) how is this fed to pyro.infer.MCMC? Something like initial_params={'X':torch.randn(num_chains, num_inputs)}?

Thank you!

You are right. The docs said that the input of potential_fn is a dictionary. So you would provide the initial value as a dictionary: e.g. {‘X’: torch.zeros(())} if your net input is a scalar. If your net’s input is a tensor, you can just simple wrap it by using potential_fn=lambda inputs: net(inputs[‘X’]).