Looking for use of Empirical class to create an empirical distribution from samples


Can someone point me to an example of creating in instance of the Empirical distribution from samples and log_weights? I wrote an ABC method for inferring a latent variable that is hard to infer using standard Pyro solvers. I want to sample my latent from that empirical distribution in a new program.

Related post: Is there any example on how to use/create an empirical distribution?


Small illustration is below, but you might find the tests in test_empirical useful. I’m not sure if you will be able to use this distribution freely inside of SVI though. In particular, it won’t work inside plates, since it lacks a .expand method and the log_prob method cannot score batched samples. Currently, this is only being used to hold the results of inference.

>>> import torch
>>> from pyro.distributions import Empirical
>>> samples = torch.randn(10, 3, 5)  
>>> weights = torch.rand(10).log()  # samples aggregated along the leftmost dim
>>> emp = Empirical(samples, weights)
>>> emp.batch_shape
>>> emp.event_shape
torch.Size([3, 5])
>>> emp.sample()  # shape([3, 5])
tensor([[ 2.5262,  0.2761,  0.7590, -1.2264,  1.7800],
        [ 0.3883,  2.0159, -0.1476, -0.0484,  0.8950],
        [ 0.6351, -1.5402, -0.2153, -1.7630, -2.9152]])
>>> emp.log_prob(emp.sample()) # shape([])