UserWarning: Encountered NaN with Trace_ELBO

I’m new to pyro and I’m following the Bayesian Regression - Inference Algorithm (part 2) Tutorial. I tried to build my own model and guide, and I just want to find the value of the weight, bias and noise given some linear data.

I’ll leave here part of my code. I think it’s pretty similar to the one in the tutorial but I get UserWarning: Encountered NaN: loss when I do svi.step. Can you help me understand why and what I should change? Thanks.

The data (x=height, y=mass) is normalised.

    def model(height, mass):

        # unit normal priors over the parameters b, w, noise

        mean = b + w * height
        # condition on the observed data
        with pyro.plate("data", len(height)):
            pyro.sample("obs", dist.Normal(mean, noise), obs=mass)

    def guide(height, mass):
        # register variational parameters b_loc, b_scale, w_loc, w_scale, noise_loc

        # sample from normal prior

your noise distributions needs to have support on the positive real line not the full real line. try using e.g. a LogNormal distribution

1 Like

Thank you! I changed also the learning rate and now it works perfectly