Simple Bayesian NN shape error

I’m trying to follow the guide for creating a Bayesian network as in the the following tutorial link:

http://pyro.ai/examples/bayesian_regression.html?highlight=to_event

But when I run my simple NN I get a runtime error:

RuntimeError: t() expects a 2D tensor, but self is 1D

I can’t seem to find the problem in my code but as this is my first time using Pyro I’m still getting to grips with the physical understanding of the guide and therefore maybe because of that I’ve made a simple error that I just can’t see?

Any help would be appreciated on this!

import torch
import numpy as np
from torchvision import transforms
import random
import pyro
from pyro.distributions import Normal, Uniform
from pyro.contrib.autoguide import AutoDiagonalNormal
from pyro.infer import SVI, Trace_ELBO
from pyro.optim import Adam

plt.ioff()
plt.close('all')

""" CREATING THE DATASET """

x = np.random.uniform(0,1,64)
print('made x data')
random.shuffle(x)
print('shuffled x data')
m = 3
c = 4
y = (m*x) + c 
print('made y data')

data = np.vstack([x,y]).T

transform = transforms.Compose([transforms.ToTensor()])

x_tensor = torch.tensor(x,dtype=torch.float).unsqueeze(-1)
y_tensor = torch.tensor(y,dtype=torch.float)

print('made tensors')
       
""" CREATING THE MODEL """

class NN(torch.nn.Module):
    
    def __init__(self,p):
        super().__init__()
             
        self.linear = torch.nn.Linear(p,1) 
     
    def forward(self,x):
        output = self.linear(x)

        return output

regression_model = NN(1)

print(regression_model)

""" DEFINING THE PROBABILISTIC MODEL """

def model(x_data,y_data):
    w_prior = Normal(torch.zeros(1),torch.ones(1)).to_event(1)
    priors = {'linear.weight':w_prior}
    scale = pyro.sample("sigma", Uniform(0.,10.))
    lifted_module = pyro.random_module("module",regression_model,priors)
    lifted_reg_model = lifted_module()
    with pyro.plate("map",len(x_data)):
        prediction_mean = lifted_reg_model(x_data).squeeze(-1)
        pyro.sample("obs",Normal(prediction_mean,scale),obs=y_data)
        return prediction_mean


guide = AutoDiagonalNormal(model)

optim = Adam({"lr":0.01})
svi = SVI(model,guide,optim,loss=Trace_ELBO(),num_samples = 100)

num_iterations = 40

def train():
    pyro.clear_param_store()
    for j in range(num_iterations):
        loss = svi.step(x_tensor,y_tensor)
        if j % 1 == 0:
            print("[iteration %03d] loss: %.4f" % (j+1, loss/len(x_tensor)))

train()

Edit:

To be clear, I understand that this is a shape error as it is explicitly stated in the error… I’m just unsure of how to fix the problem given my small knowledge of using Pyro.

Your weight prior is the wrong shape. nn.Linear expects 2D weights, as the error message says, but your w_prior is producing 1D weights.

With correct shapes:
w_prior = Normal(torch.zeros(1, 1),torch.ones(1, 1)).to_event(1)

1 Like