How do I combine AutoGuideList with a custom guide?

I’m trying to create an autoguide list over a model.

Let’s say my model looks like this:

def model(X, number_of_deltas, y=None):

    # compute some coefficients
    means = torch.zeros(X.size(1))
    standard_deviations = torch.full((X.size(1),), 10.0)

    normal_distribution = dist.Normal(means, standard_deviations).to_event(1)

    betas = pyro.sample(f"betas", seasonality_coefficient_normal_distribution)


    # compute some changes in those coefficients
    means_L = torch.zeros(number_of_deltas)
    scales_L = torch.full((number_of_deltas), 10.0)

    laplace_distribution = dist.Laplace(means_L, scales_L).to_event(1)

    deltas = pyro.sample("delta", laplace_distribution)

    # do something with the betas and deltas and observe y...

In reality I have many more different coefficients I sample, but only one sampling statement for “delta”.

What I’d like to do is use the AutoNormal for the betas, and a Laplace distribution for the “deltas”.

I have a guide like this:

def guide_Laplace_deltas(X, number_of_deltas, y=None):
        
    means_L = pyro.param("means", torch.zeros(number_of_deltas))
    scales_L = pyro.param("scales", torch.full((number_of_deltas), 10.0), constraint=constraints.positive)

    laplace_distribution = dist.Laplace(means_L, scales_L).to_event(1)

    deltas = pyro.sample("delta", laplace_distribution)

And I want to use both that and the AutoNormal as part of an AutoGuideList, and only expose the “delta” sample to my custom guide. How would I do that? I’ve seen examples of combining multiple AutoGuides into the AutoGuideList but is that possible to do with the regular guide as well?

OK nvm, looks like this is pretty simple. The AutoCallable interface automatically kicks in.

I just had to do

my_guide = AutoGuideList(model)

columns_for_laplace_distribution = ["delta"]
my_huide.append(AutoNormal(pyro.poutine.block(model, hide=columns_for_laplace_distribution ), init_scale=0.1))
my_guide.append(guide_Laplace_deltas)

That’s right @bshabash, you should generally be able to simply simply append your custom guide function::

def my_custom_guide(...):
   ...

my_autoguide_list = AutoGuideList(my_model)
my_autoguide_list.append(my_custom_guide)
...
1 Like

@fritzo
I really think the Pyro documentation can benefit from more examples for each class/function.

2 Likes