RuntimeWarning: trying to observe a value outside of inference

Hi,

Can someone please explain me what the following error implies:

/opt/anaconda3/lib/python3.8/site-packages/pyro/primitives.py:137: RuntimeWarning: trying to observe a value outside of inference at l2_regularizer

How can I fix this?

Thank you,
Atharva

It sounds like you are trying to run a model in data-generating mode (i.e. outside of inference), but that model has a pyro.sample(..., obs=...) statement somewhere. This mode of operation doesn’t quite make sense, because by the time you’ve drawn earlier latent variables from the prior, they no longer correspond to the conditional obs=...) in your model. That is, you’ll need an inference algorithm to handle the observe statement.

If you simply want to generate data then you can remove the obs= part. I often write models with optional data as in

def model(data=None):
    x = pyro.sample("x", ...)
    y = pyro.sample("y", ..., obs=data)
    return y

that way when I want to generate fake data I can call data = model(), and when I want to run inference I can pass that data to SVI as in svi.step(data).

Let me know if I’ve misunderstood; I can only guess at your use case.

1 Like