ValueError: Error while computing log_prob at site 'sigma'


SVI Part II: Conditional Independence, Subsampling, and Amortization

I am running a regression model
ValueError: Error while computing log_prob at site ‘sigma’:
The value argument must be within the support
Trace Shapes:
Param Sites:
Sample Sites:
a dist |
value |
log_prob |
bA dist |
value |
log_prob |
bR dist |
value |
log_prob |
bAR dist |
value |
log_prob |
sigma dist |
value |

you’re probably going to need to give more context if you want help but it might be that you’re missing a needed constraint:

pyro.param("sigma", constraint=torch.distributions.constraint.positive)

import logging, os, torch, pyro
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import pyro.optim as optim
import pyro.distributions as dist
from torch import nn
from torch.distributions import constraints
from functools import partial
from pyro.nn import PyroModule, PyroSample
from pyro.infer import Predictive
from pyro.infer import SVI, Trace_ELBO
from pyro.infer.autoguide import AutoDiagonalNormal
assert pyro.__version__.startswith('1.3.0')'default')
logging.basicConfig(format='%(message)s', level=logging.INFO)
rugged_data = pd.read_csv(DATA_URL, encoding="ISO-8859-1")
def model(is_cont_africa, ruggedness, log_gdp):
    a = pyro.sample("a", dist.Normal(0., 10.))
    b_a = pyro.sample("bA", dist.Normal(0., 1.))
    b_r = pyro.sample("bR", dist.Normal(0., 1.))
    b_ar = pyro.sample("bAR", dist.Normal(0., 1.))
    sigma = pyro.sample("sigma", dist.Uniform(8.0, 10.))
    mean = a + b_a * is_cont_africa + b_r * ruggedness + b_ar * is_cont_africa * ruggedness
    with pyro.plate("data", len(ruggedness)):
        pyro.sample("obs", dist.Normal(mean, sigma), obs=log_gdp)

def guide(is_cont_africa, ruggedness, log_gdp):
    a_loc = pyro.param('a_loc', torch.tensor(0.))
    a_scale = pyro.param('a_scale', torch.tensor(1.), constraint=constraints.positive)
    sigma_loc = pyro.param('sigma_loc', torch.tensor(1.), constraint=constraints.positive)
    weights_loc = pyro.param('weights_loc', torch.randn(3))
    weights_scale = pyro.param('weights_scale', torch.ones(3), constraint=constraints.positive)
    # sigma = pyro.param("sigma", constraint=torch.distributions.constraint.positive)
    a = pyro.sample("a", dist.Normal(a_loc, a_scale))
    b_a = pyro.sample("bA", dist.Normal(weights_loc[0], weights_scale[0]))
    b_r = pyro.sample("bR", dist.Normal(weights_loc[1], weights_scale[1]))
    b_ar = pyro.sample("bAR", dist.Normal(weights_loc[2], weights_scale[2]))
    sigma = pyro.sample("sigma", dist.Normal(sigma_loc, torch.tensor(0.05)))
    mean = a + b_a * is_cont_africa + b_r * ruggedness + b_ar * is_cont_africa * ruggedness

def summary(samples):
    site_stats = {}
    for site_name, values in samples.items():
        marginal_site = pd.DataFrame(values)
        describe = marginal_site.describe(percentiles=[.05, 0.25, 0.5, 0.75, 0.95]).transpose()
        site_stats[site_name] = describe[["mean", "std", "5%", "25%", "50%", "75%", "95%"]]
    return site_stats

df = rugged_data[["cont_africa", "rugged", "rgdppc_2000"]]
df = df[np.isfinite(df.rgdppc_2000)]
df["rgdppc_2000"] = np.log(df["rgdppc_2000"])
train = torch.tensor(df.values, dtype=torch.float)

svi = SVI(model, guide, optim.Adam({"lr": .05}),loss=Trace_ELBO())
is_cont_africa, ruggedness, log_gdp1 = train[:, 0], train[:, 1], train[:, 2]

num_iters =  2
for i in range(num_iters):
    elbo = svi.step(is_cont_africa, ruggedness, log_gdp1)
    if i % 500 == 0:"Elbo loss: {}".format(elbo))

Thank you very much for your reply. The program runs to elbo = svi.step(is_cont_africa, ruggedness, log_gdp1). indicates an error at def compute_log_prob(self, site_filter=lambda name, site: True)

the distribution used in the custom_guide isn’t valid since it can put probability mass on negative values. should use something like a LogNormal instead. see updated tutorial

  • Your advice is very good, I changed the ‘sigma’ in ‘def guide’ for
    sigma = pyro.sample(“sigma”, dist.LogNormal(sigma_loc, torch.tensor(0.05))), but the error remained. The intro_long.ipynb file could not be found

Thank you very much. Problem solved. Is to replace sigma = pyro.sample(“sigma”, dist.Uniform(8.0, 10.)) with sigma = pyro.sample(“sigma”, dist.LogNormal(0., 10.)) in ‘def model’, it can be compiled through.