Get posterior distribution of a Pyro Module

Hello everyone,

I generated a pyro model by exploiting the pyro.module class and a neural network created with Pytorch:

class NN(nn.Module):

    def __init__(self, input_dim, output_dim, neurons):
        super().__init__()
        self.input_layer = nn.Linear(input_dim, neurons)
        self.hidden_layer = nn.Linear(neurons, neurons)
        self.output_layer = nn.Linear(neurons, output_dim)
        self.tanh = nn.Tanh()

    def forward(self, x):
        h1 = self.tanh(self.input_layer(x))
        h2 = self.tanh(self.hidden_layer(h1))
        h3 = self.tanh(self.hidden_layer(h2))
        h4 = self.tanh(self.hidden_layer(h3))
        return self.output_layer(h4)


class MyModel(nn.Module):

    def __init__(self, input_dim=1, out_dim=1, neurons=20):
        super().__init__()
        self.nn = NN(input_dim, out_dim, neurons)
        self.guide = AutoDiagonalNormal(self.model)
        self.history = list()

    def model(self, x_data, y_data):
        pyro.module("nn", self.nn)
        ...

However, I am not able to compute the posterior distribution of the network parameters.
In particular, when I do:

predictive = Predictive(my_model.model, guide=my_model.guide, num_samples=1000)
data = predictive(some_input, None)

there is not site corresponding to the network parameters.
Has anyone have any idea?
Thank you in advance!

The network parameters are deterministic and found through optimization. They don’t have a prior or posterior because of that. If you want to turn them into rvs, use poutine.lift or turn the network into a PyroModule and explicitly set network weight and bias priors.

1 Like