Repository: pyro-ppl/pyro

Issue:
Bayesian regression example doesn’t work with N=1
Hi,

When I run the [bayesian_regression.py](https://github.com/uber/pyro/blob/dev/examples/bayesian_regression.py) example on github, with `N = 1` on [line 44](https://github.com/uber/pyro/blob/dev/examples/bayesian_regression.py#L44), I get an error from Tensor flow. Is there an assumption that `N` must be at least 2? While values of `N < 2` may be corner cases, Pyro could improve user experience by explicitly checking `N` values and giving users a more friendly error message, rather than sending bad values to Tensor flow. Here’s the error with `N=1`:

```
$ python bayesian_regression.py
Traceback (most recent call last):
   File "bayesian_regression.py", line 140, in <module>
 	main(args)
   File "bayesian_regression.py", line 117, in main
 	epoch_loss = svi.step(data)
   File "/usr/local/lib/python2.7/dist-packages/pyro/infer/svi.py", line 98, in step
 	loss = self.loss_and_grads(self.model, self.guide, *args, **kwargs)
   File "/usr/local/lib/python2.7/dist-packages/pyro/infer/elbo.py", line 65, in loss_and_grads
 	return self.which_elbo.loss_and_grads(model, guide, *args, **kwargs)
   File "/usr/local/lib/python2.7/dist-packages/pyro/infer/trace_elbo.py", line 181, in loss_and_grads
 	torch_backward(surrogate_loss)
   File "/usr/local/lib/python2.7/dist-packages/pyro/infer/util.py", line 34, in torch_backward
 	x.backward()
   File "/usr/local/lib/python2.7/dist-packages/torch/autograd/variable.py", line 167, in backward
 	torch.autograd.backward(self, gradient, retain_graph, create_graph, retain_variables)
   File "/usr/local/lib/python2.7/dist-packages/torch/autograd/__init__.py", line 99, in backward
 	variables, grad_variables, retain_graph)
RuntimeError: matrices expected, got 3D, 2D tensors at /pytorch/torch/lib/TH/generic/THTensorMath.c:1411
"
```
Environment:

```
Python version : 3.6
Pyro version: 0.1.2
Pytorch version: 0.3.0post4
```



Discussion:
no, `N` can be any positive integer - this appears to be a bug. interestingly, this is only happening in python 3, and it seems to be happening in the calculation of the ELBO.

as a side note, you said you're using python 3.6, but your stack trace seems to be using the 2.7 library. just a heads up

thanks for the reproducible steps, i will investigate further
this is a pytorch bug related to `squeeze`. i will open an issue in pytorch. in the meantime, please change [this line](https://github.com/uber/pyro/blob/dev/examples/bayesian_regression.py#L68) to  `squeeze(1)`. ill also modify the tutorial to reflect this.

Resolving patch:
diff --git a/examples/bayesian_regression.py b/examples/bayesian_regression.py
--- a/examples/bayesian_regression.py
+++ b/examples/bayesian_regression.py
@@ -65,7 +65,7 @@ def model(data):
         x_data = data[:, :-1]
         y_data = data[:, -1]
         # run the regressor forward conditioned on inputs
-        prediction_mean = lifted_reg_model(x_data).squeeze()
+        prediction_mean = lifted_reg_model(x_data).squeeze(1)
         pyro.sample("obs",
                     Normal(prediction_mean, Variable(torch.ones(data.size(0))).type_as(data)),
                     obs=y_data.squeeze())


Test patch: