UserWarning: RNG Variable RandomGeneratorSharedVariable has multiple clients

Hi all,

I ran the following code and got a UserWarning.
Is there a solution to avoid this User Warning?

import numpy as np
import pymc as pm
import pytensor
from pymc.pytensorf import collect_default_updates

with pm.Model() as model:
    t = pm.Beta("t", alpha=2, beta=1, shape=3)
    
    def loop(symbol, t):
        symbol_new = pm.Categorical.dist(t)
        return (symbol_new), collect_default_updates([symbol_new])
    
    symbol = np.array(0, dtype=int)
    results, updates = pytensor.scan(
        fn=loop,
        outputs_info=[symbol],
        non_sequences=[t],
        n_steps=10,
        strict=True
        )
    
    pm.Deterministic("final_symbol", results[-1])
    
    idata = pm.sample_prior_predictive(10)
UserWarning: RNG Variable RandomGeneratorSharedVariable(<Generator(PCG64) at 0x14F69B300>) has multiple clients. This is likely an inconsistent random graph.
  warnings.warn(
Sampling: [t]

Hi Yamaguchi, could you say a little bit about why you want a scan function here? It doesn’t look like you are doing any recursive computation. The result of each loop doesn’t depend on the previous symbol. In that case, you can just specify you want 10 draws from a categorical distribution with something like:

with pm.Model():
    t = pm.Beta('t',alpha=2, beta=1, shape=3)
    symbol = pm.Categorical('symbol',t,shape=10)
    pm.Deterministic("final_symbol", symbol[-1])
    idata = pm.sample_prior_predictive(10)

If I was a bit more clear on your intentions, I think it would be easier to clean up the scan function.

3 Likes