Problem running bayesian_regression.py

I added the following code to the main module:

for name in pyro.get_param_store().get_all_param_names():
    print("[%s]: %.3f" % (name, pyro.param(name).data.numpy()))

X = np.linspace(6, 7, num=20)
y = 3 * X + 1
X, y = X.reshape((20, 1)), y.reshape((20, 1))
x_data, y_data = Variable(torch.Tensor(X)), Variable(torch.Tensor(y))
loss = nn.MSELoss()
y_preds = Variable(torch.zeros(20, 1))
for i in range(20):
    # guide does not require the data
    sampled_reg_model = guide(None)
    # run the regression model and add prediction to total
    y_preds = y_preds + sampled_reg_model(x_data)
# take the average of the predictions
y_preds = y_preds / 20
print ("Loss: ", loss(y_preds, y_data).data[0])

and I go the error:

epoch avg loss 5.362271118164062
epoch avg loss 5.106288757324219
epoch avg loss 4.880701293945313
epoch avg loss 4.297378845214844
epoch avg loss 4.150056457519531
epoch avg loss 3.7338333129882812
epoch avg loss 3.8261447143554688
epoch avg loss 3.32685546875
epoch avg loss 2.7866165161132814
epoch avg loss 2.593047790527344
[guide_mean_weight]: 0.553
[guide_log_sigma_weight]: -2.794
[guide_mean_bias]: 0.590
[guide_log_sigma_bias]: -2.909
Traceback (most recent call last):
  File "C:\sm\BottleRockets\Python\Pyro\bayesian_regression.py", line 157, in <module>
    main(args)
  File "C:\sm\BottleRockets\Python\Pyro\bayesian_regression.py", line 144, in main
    sampled_reg_model = guide(None)
  File "C:\sm\BottleRockets\Python\Pyro\bayesian_regression.py", line 75, in guide
    w_mu = Variable(torch.randn(1, p).type_as(data.data), requires_grad=True)
AttributeError: 'NoneType' object has no attribute 'data'
Press any key to continue . . .

Any suggestions?

Charles

Hm instead of None, try passing in the data in the guide:

sampled_reg_model = guide(x_data)

That line in the example handles consistency across tensors if you’re using CUDA, which is simplified out in the tutorial.

Well… I don’t get an error, but the results are nothing like the web page:

Bayesian Regression - Introduction (Part 1) — Pyro Tutorials 1.8.6 documentation

Somebody needs to edit the web page.

I now get:

epoch avg loss 7.6388177490234375
epoch avg loss 6.879603881835937
epoch avg loss 6.507572021484375
epoch avg loss 5.942550048828125
epoch avg loss 5.306795654296875
epoch avg loss 4.970294799804687
epoch avg loss 4.815382995605469
epoch avg loss 4.590582275390625
epoch avg loss 3.8029959106445315
epoch avg loss 3.8445388793945314
[guide_mean_weight]: 0.845
[guide_log_sigma_weight]: -2.854
[guide_mean_bias]: -0.564
[guide_log_sigma_bias]: -3.030
Loss: 247.13351440429688

Not very encouraging.

Charles

the examples are slightly fancier than the tutorials, so you cant always expect to drop in code and have everything necessarily work right out of the box. for one, this example has been updated to use a random w. the model also uses a narrower variance and a smaller learning rate. you can change those to what they are in the tutorial to replicate results (if youre looking to just reproduce the results in the tutorial, you can just run the jupyter notebook directly). thanks for your comments; ill add a note in our tutorials to watch out for this.