Mixture model with multidimensional weights prior

Hello,

I am trying to implement a mixture model in pyro with categorical distribution weights that have more than one dimension. This is because I have some prior knowledge about the categories of some (but not all) of my observations.

In this example my data has 80 observations across two dimensions with size 10 and 2, so:

x_data.shape = (80,10,2)

Furthermore, there are 4 categories. So in the simple case:

self.weights.shape = torch.Size([1, 1, 1, 4])

and in the more complex case with separate weights for each observation:

self.weights.shape = torch.Size([80, 1, 1, 4])

My likelihood function is:

with obs_plate:
    assignment = pyro.sample('assignment', dist.Categorical(probs = self.weights))
    rate = alpha / mu[torch.arange(self.n_obs),:,:,assignment[:,0,0,0]]
    pyro.sample("data_target", 
                dist.GammaPoisson(concentration= alpha, rate= rate), obs=x_data)

If I do not have separate weight priors for each observation, all variables seem to have the correct dimensions when I train the model and print out the shape of each variable:

self.weights.shape = torch.Size([1, 1, 1, 4])
assignment.shape = torch.Size([1, 1, 1, 4])
alpha.shape = torch.Size([10, 1])
rate.shape = torch.Size([80, 10, 2])
x_data.shape = torch.Size([80, 10, 2])

However, when I do have separate weights priors for each observations, I think the dimensions of the assignment variable are wrong:

self.weights.shape = torch.Size([80, 1, 1, 4])
assignment.shape = torch.Size([80, 1, 1, 4])
alpha.shape = torch.Size([10, 1])
rate.shape = torch.Size([80, 10, 2])
x_data.shape = torch.Size([80, 10, 2])

The assignment variable is inferred via enumeration during training, so from what I understand it should still just have shape (1,1,1,4) and not (80,1,1,4).
Memory requirements also increase drastically in this case, so that I cannot run this example on a large dataset. So I am stuck at this point and it would be really great if you can help me to get this complex model to work.

Best wishes,

Alexander

Hello again,
I am still stuck at this problem.
So I have made a minimum reproducible example using just a 1D Gaussian Mixture Model. I am hoping this could help to answer my question. Please see the notebook here: Minimum-Example/MultiDimensionalWeightPrior_MinimumExample.ipynb at main · AlexanderAivazidis/Minimum-Example · GitHub
Thanks a lot!
Alexander

Hi @AlexanderA . If you are using enumeration then assignment should have a shape of assignment.shape = torch.Size([4, 1, 1, 1]) (enumerated dim should be on the left). Can you paste your entire code or simpler version of it if it is too big?

Edit: oh sorry, I see the link to the code.

@AlexanderA can you restate your question or provide a full example that matches your question. I don’t see how you can get assignment.shape = torch.Size([1, 1, 1, 4]) from the Jupyter notebook code that you provided.

Thanks for your reply! Changing the shape of assignment actually already helped in my case it seems. I will try to post an example of this later so other people can use this if they get stuck at the same problem.