How to Convert Constrained Samples to Unconstrained Samples for SVI Guide?`

Hello,

Building from my previous post #5950, I am trying to calculate the log probability of samples from an SVI autoguide using the log_density utility function. However, in my current use case, I only have samples from the constrained space. With an autoguide, log_density requires both the unconstrained_samples and constrained_samples to compute the log probability. Therefore, I need to convert constrained_samplesunconstrained_samples if I want to perform the calculation.

How can I make the conversion constrained_samplesunconstrained_samples? I.e., I’m trying to construct the inverse of guide._unpack_and_constrain.

Update: I’ve made the conversions manually for each latent variable using the approach suggested in #2857.2. This is done by using the biject_to function and the supports of the constrained latent distributions.

However, I am finding small differences between the _auto_latent produced using Predictive(guide, ...) and the unconstrained_samples obtained by applying the bijection on constrained_samples, also obtained with Predictive(guide, ...). Though these differences are small, log_density assumes a particular bijection, so the small differences cause us to exit the support of log_density and obtain -Inf.

Is there a better way to either calculate the log_density with only constrained samples or a way to get the unconstrained samples in a way that matches Predictive(guide, ...)?

I think log_density works for constrained samples. I’m not sure why you require unconstrained samples in your pipeline.

Hi @fehiepsi. I tried computing log_density without specifying values for _auto_latent. I still get -Inf, probably because _auto_latent values are generated when seeding the guide and those seeded values are used in the computation if no _auto_latent samples are inputted manually. Note, the log_density calculation works when I input the _auto_latent variables produced with Predictive(guide, ...).

I don’t require the unconstrained samples. All I want is the log_density of the constrained samples z from the guide q(z). The reason why I need the unconstrained samples is because I haven’t been able to compute the log_density without their values.

Would it help if I provide an example?

Thanks!

I see. How about using _ravel_dict? First, you convert constrained to unconstrained samples, then, using _ravel_dict to obtain _auto_latent samples. Finally, you can using log_density with that _auto_latent.

@fehiepsi, I found an alternative to this approach. I was considering a toy example where my latent variables are constrained. However, for my actual problem, the subset of latent variables that I am interested in are actually unconstrained. Since my guide is an AutoMultivariateNormal, I don’t have to worry about the transformation and can compute the log probability using the MultivariateNormal distribution. This calculation was successful.

Regarding the -Inf that I was getting for the log_density, this was actually due to the fact that I defined my guide and samples using float32 numbers, but switched to float64 later in the pipeline. After making this fix, I am still getting -Inf for the constrained samples that I am using to calculate the log probability. I also tried _ravel_dict and found no improvement.

Thanks for the help!