How to access the loss for each proposal and how to tell the accepted and rejected samples?

Hi Pyro community,

I’m new to the Pyro package, and I want to extend my thanks to the developers for this powerful tool. I’m currently working on PDE-constrained parameter optimization and have set up Pyro to run HMC sampling for parameters in a PDE to fit observed data. I have a few questions and would appreciate any guidance:

  1. How can I access the loss for each of the proposed parameter samples?
  2. I understand that I can get the samples using mcmc.get_samples(), but I believe these include both accepted and rejected samples. How can I obtain only the accepted samples?

Thank you in advance for your help.

By loss, do you mean the potential energy?

For “rejected” info, you can compare two contiguous samples, something like norm(samples[:-1] - samples[1:]) < 1e-6. Note that “reject” is just a part of Metropolis Hasting proposal - you should include those “repeated” samples when computing posterior diagnostics/predictions. We do not return “rejected” samples in the mcmc.get_samples() method.

Certainly! Here’s a refined version of your text:

Thank you for your response. I now understand question #2.

By “loss,” I am referring to the potential energy, which is calculated as log(-misfit). I realize that I can use the predictive function to make predictions. However, running PDE simulations in my case is time-consuming, and I prefer not to repeat them for the proposed samples. Therefore, I would like to know if there is a way to access the potential energy after running MCMC?


If you use NumPyro, you can collect it via extra_fields of We currently don’t have such api in Pyro.

1 Like

I see. Thank you!