We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `examples/bayesian_regression.py`
Content:
```
1 import numpy as np
2 import argparse
3 import torch
4 import torch.nn as nn
5 from torch.nn.functional import normalize  # noqa: F401
6 
7 from torch.autograd import Variable
8 
9 import pyro
10 from pyro.distributions import Normal, Bernoulli  # noqa: F401
11 from pyro.infer import SVI
12 from pyro.optim import Adam
13 
14 """
15 Bayesian Regression
16 Learning a function of the form:
17     y = wx + b
18 """
19 
20 
21 # generate toy dataset
22 def build_linear_dataset(N, p, noise_std=0.01):
23     X = np.random.rand(N, p)
24     # use random integer weights from [0, 7]
25     w = np.random.randint(8, size=p)
26     # set b = 1
27     y = np.matmul(X, w) + np.repeat(1, N) + np.random.normal(0, noise_std, size=N)
28     y = y.reshape(N, 1)
29     X, y = Variable(torch.Tensor(X)), Variable(torch.Tensor(y))
30     return torch.cat((X, y), 1)
31 
32 
33 # NN with one linear layer
34 class RegressionModel(nn.Module):
35     def __init__(self, p):
36         super(RegressionModel, self).__init__()
37         self.linear = nn.Linear(p, 1)
38 
39     def forward(self, x):
40         # x * w + b
41         return self.linear(x)
42 
43 
44 N = 100  # size of toy data
45 p = 1  # number of features
46 
47 softplus = nn.Softplus()
48 regression_model = RegressionModel(p)
49 
50 
51 def model(data):
52     # Create unit normal priors over the parameters
53     mu = Variable(torch.zeros(1, p)).type_as(data)
54     sigma = Variable(torch.ones(1, p)).type_as(data)
55     bias_mu = Variable(torch.zeros(1)).type_as(data)
56     bias_sigma = Variable(torch.ones(1)).type_as(data)
57     w_prior, b_prior = Normal(mu, sigma), Normal(bias_mu, bias_sigma)
58     priors = {'linear.weight': w_prior, 'linear.bias': b_prior}
59     # lift module parameters to random variables sampled from the priors
60     lifted_module = pyro.random_module("module", regression_model, priors)
61     # sample a regressor (which also samples w and b)
62     lifted_reg_model = lifted_module()
63 
64     with pyro.iarange("map", N, subsample=data):
65         x_data = data[:, :-1]
66         y_data = data[:, -1]
67         # run the regressor forward conditioned on inputs
68         prediction_mean = lifted_reg_model(x_data).squeeze()
69         pyro.sample("obs",
70                     Normal(prediction_mean, Variable(torch.ones(data.size(0))).type_as(data)),
71                     obs=y_data.squeeze())
72 
73 
74 def guide(data):
75     w_mu = Variable(torch.randn(1, p).type_as(data.data), requires_grad=True)
76     w_log_sig = Variable((-3.0 * torch.ones(1, p) + 0.05 * torch.randn(1, p)).type_as(data.data), requires_grad=True)
77     b_mu = Variable(torch.randn(1).type_as(data.data), requires_grad=True)
78     b_log_sig = Variable((-3.0 * torch.ones(1) + 0.05 * torch.randn(1)).type_as(data.data), requires_grad=True)
79     # register learnable params in the param store
80     mw_param = pyro.param("guide_mean_weight", w_mu)
81     sw_param = softplus(pyro.param("guide_log_sigma_weight", w_log_sig))
82     mb_param = pyro.param("guide_mean_bias", b_mu)
83     sb_param = softplus(pyro.param("guide_log_sigma_bias", b_log_sig))
84     # gaussian guide distributions for w and b
85     w_dist = Normal(mw_param, sw_param)
86     b_dist = Normal(mb_param, sb_param)
87     dists = {'linear.weight': w_dist, 'linear.bias': b_dist}
88     # overloading the parameters in the module with random samples from the guide distributions
89     lifted_module = pyro.random_module("module", regression_model, dists)
90     # sample a regressor
91     return lifted_module()
92 
93 
94 # instantiate optim and inference objects
95 optim = Adam({"lr": 0.001})
96 svi = SVI(model, guide, optim, loss="ELBO")
97 
98 
99 # get array of batch indices
100 def get_batch_indices(N, batch_size):
101     all_batches = np.arange(0, N, batch_size)
102     if all_batches[-1] != N:
103         all_batches = list(all_batches) + [N]
104     return all_batches
105 
106 
107 def main(args):
108     data = build_linear_dataset(N, p)
109     if args.cuda:
110         # make tensors and modules CUDA
111         data = data.cuda()
112         softplus.cuda()
113         regression_model.cuda()
114     for j in range(args.num_epochs):
115         if args.batch_size == N:
116             # use the entire data set
117             epoch_loss = svi.step(data)
118         else:
119             # mini batch
120             epoch_loss = 0.0
121             perm = torch.randperm(N) if not args.cuda else torch.randperm(N).cuda()
122             # shuffle data
123             data = data[perm]
124             # get indices of each batch
125             all_batches = get_batch_indices(N, args.batch_size)
126             for ix, batch_start in enumerate(all_batches[:-1]):
127                 batch_end = all_batches[ix + 1]
128                 batch_data = data[batch_start: batch_end]
129                 epoch_loss += svi.step(batch_data)
130         if j % 100 == 0:
131             print("epoch avg loss {}".format(epoch_loss/float(N)))
132 
133 
134 if __name__ == '__main__':
135     parser = argparse.ArgumentParser(description="parse args")
136     parser.add_argument('-n', '--num-epochs', default=1000, type=int)
137     parser.add_argument('-b', '--batch-size', default=N, type=int)
138     parser.add_argument('--cuda', action='store_true')
139     args = parser.parse_args()
140     main(args)
141
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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 @@
         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())

{"golden_diff": "diff --git a/examples/bayesian_regression.py b/examples/bayesian_regression.py\n--- a/examples/bayesian_regression.py\n+++ b/examples/bayesian_regression.py\n@@ -65,7 +65,7 @@\n         x_data = data[:, :-1]\n         y_data = data[:, -1]\n         # run the regressor forward conditioned on inputs\n-        prediction_mean = lifted_reg_model(x_data).squeeze()\n+        prediction_mean = lifted_reg_model(x_data).squeeze(1)\n         pyro.sample(\"obs\",\n                     Normal(prediction_mean, Variable(torch.ones(data.size(0))).type_as(data)),\n                     obs=y_data.squeeze())\n", "issue": "Bayesian regression example doesn\u2019t work with N=1\nHi,\r\n\r\nWhen 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\u2019s the error with `N=1`:\r\n\r\n```\r\n$ python bayesian_regression.py\r\nTraceback (most recent call last):\r\n   File \"bayesian_regression.py\", line 140, in <module>\r\n \tmain(args)\r\n   File \"bayesian_regression.py\", line 117, in main\r\n \tepoch_loss = svi.step(data)\r\n   File \"/usr/local/lib/python2.7/dist-packages/pyro/infer/svi.py\", line 98, in step\r\n \tloss = self.loss_and_grads(self.model, self.guide, *args, **kwargs)\r\n   File \"/usr/local/lib/python2.7/dist-packages/pyro/infer/elbo.py\", line 65, in loss_and_grads\r\n \treturn self.which_elbo.loss_and_grads(model, guide, *args, **kwargs)\r\n   File \"/usr/local/lib/python2.7/dist-packages/pyro/infer/trace_elbo.py\", line 181, in loss_and_grads\r\n \ttorch_backward(surrogate_loss)\r\n   File \"/usr/local/lib/python2.7/dist-packages/pyro/infer/util.py\", line 34, in torch_backward\r\n \tx.backward()\r\n   File \"/usr/local/lib/python2.7/dist-packages/torch/autograd/variable.py\", line 167, in backward\r\n \ttorch.autograd.backward(self, gradient, retain_graph, create_graph, retain_variables)\r\n   File \"/usr/local/lib/python2.7/dist-packages/torch/autograd/__init__.py\", line 99, in backward\r\n \tvariables, grad_variables, retain_graph)\r\nRuntimeError: matrices expected, got 3D, 2D tensors at /pytorch/torch/lib/TH/generic/THTensorMath.c:1411\r\n\"\r\n```\r\nEnvironment:\r\n\r\n```\r\nPython version : 3.6\r\nPyro version: 0.1.2\r\nPytorch version: 0.3.0post4\r\n```\r\n\n", "before_files": [{"content": "import numpy as np\nimport argparse\nimport torch\nimport torch.nn as nn\nfrom torch.nn.functional import normalize  # noqa: F401\n\nfrom torch.autograd import Variable\n\nimport pyro\nfrom pyro.distributions import Normal, Bernoulli  # noqa: F401\nfrom pyro.infer import SVI\nfrom pyro.optim import Adam\n\n\"\"\"\nBayesian Regression\nLearning a function of the form:\n    y = wx + b\n\"\"\"\n\n\n# generate toy dataset\ndef build_linear_dataset(N, p, noise_std=0.01):\n    X = np.random.rand(N, p)\n    # use random integer weights from [0, 7]\n    w = np.random.randint(8, size=p)\n    # set b = 1\n    y = np.matmul(X, w) + np.repeat(1, N) + np.random.normal(0, noise_std, size=N)\n    y = y.reshape(N, 1)\n    X, y = Variable(torch.Tensor(X)), Variable(torch.Tensor(y))\n    return torch.cat((X, y), 1)\n\n\n# NN with one linear layer\nclass RegressionModel(nn.Module):\n    def __init__(self, p):\n        super(RegressionModel, self).__init__()\n        self.linear = nn.Linear(p, 1)\n\n    def forward(self, x):\n        # x * w + b\n        return self.linear(x)\n\n\nN = 100  # size of toy data\np = 1  # number of features\n\nsoftplus = nn.Softplus()\nregression_model = RegressionModel(p)\n\n\ndef model(data):\n    # Create unit normal priors over the parameters\n    mu = Variable(torch.zeros(1, p)).type_as(data)\n    sigma = Variable(torch.ones(1, p)).type_as(data)\n    bias_mu = Variable(torch.zeros(1)).type_as(data)\n    bias_sigma = Variable(torch.ones(1)).type_as(data)\n    w_prior, b_prior = Normal(mu, sigma), Normal(bias_mu, bias_sigma)\n    priors = {'linear.weight': w_prior, 'linear.bias': b_prior}\n    # lift module parameters to random variables sampled from the priors\n    lifted_module = pyro.random_module(\"module\", regression_model, priors)\n    # sample a regressor (which also samples w and b)\n    lifted_reg_model = lifted_module()\n\n    with pyro.iarange(\"map\", N, subsample=data):\n        x_data = data[:, :-1]\n        y_data = data[:, -1]\n        # run the regressor forward conditioned on inputs\n        prediction_mean = lifted_reg_model(x_data).squeeze()\n        pyro.sample(\"obs\",\n                    Normal(prediction_mean, Variable(torch.ones(data.size(0))).type_as(data)),\n                    obs=y_data.squeeze())\n\n\ndef guide(data):\n    w_mu = Variable(torch.randn(1, p).type_as(data.data), requires_grad=True)\n    w_log_sig = Variable((-3.0 * torch.ones(1, p) + 0.05 * torch.randn(1, p)).type_as(data.data), requires_grad=True)\n    b_mu = Variable(torch.randn(1).type_as(data.data), requires_grad=True)\n    b_log_sig = Variable((-3.0 * torch.ones(1) + 0.05 * torch.randn(1)).type_as(data.data), requires_grad=True)\n    # register learnable params in the param store\n    mw_param = pyro.param(\"guide_mean_weight\", w_mu)\n    sw_param = softplus(pyro.param(\"guide_log_sigma_weight\", w_log_sig))\n    mb_param = pyro.param(\"guide_mean_bias\", b_mu)\n    sb_param = softplus(pyro.param(\"guide_log_sigma_bias\", b_log_sig))\n    # gaussian guide distributions for w and b\n    w_dist = Normal(mw_param, sw_param)\n    b_dist = Normal(mb_param, sb_param)\n    dists = {'linear.weight': w_dist, 'linear.bias': b_dist}\n    # overloading the parameters in the module with random samples from the guide distributions\n    lifted_module = pyro.random_module(\"module\", regression_model, dists)\n    # sample a regressor\n    return lifted_module()\n\n\n# instantiate optim and inference objects\noptim = Adam({\"lr\": 0.001})\nsvi = SVI(model, guide, optim, loss=\"ELBO\")\n\n\n# get array of batch indices\ndef get_batch_indices(N, batch_size):\n    all_batches = np.arange(0, N, batch_size)\n    if all_batches[-1] != N:\n        all_batches = list(all_batches) + [N]\n    return all_batches\n\n\ndef main(args):\n    data = build_linear_dataset(N, p)\n    if args.cuda:\n        # make tensors and modules CUDA\n        data = data.cuda()\n        softplus.cuda()\n        regression_model.cuda()\n    for j in range(args.num_epochs):\n        if args.batch_size == N:\n            # use the entire data set\n            epoch_loss = svi.step(data)\n        else:\n            # mini batch\n            epoch_loss = 0.0\n            perm = torch.randperm(N) if not args.cuda else torch.randperm(N).cuda()\n            # shuffle data\n            data = data[perm]\n            # get indices of each batch\n            all_batches = get_batch_indices(N, args.batch_size)\n            for ix, batch_start in enumerate(all_batches[:-1]):\n                batch_end = all_batches[ix + 1]\n                batch_data = data[batch_start: batch_end]\n                epoch_loss += svi.step(batch_data)\n        if j % 100 == 0:\n            print(\"epoch avg loss {}\".format(epoch_loss/float(N)))\n\n\nif __name__ == '__main__':\n    parser = argparse.ArgumentParser(description=\"parse args\")\n    parser.add_argument('-n', '--num-epochs', default=1000, type=int)\n    parser.add_argument('-b', '--batch-size', default=N, type=int)\n    parser.add_argument('--cuda', action='store_true')\n    args = parser.parse_args()\n    main(args)\n", "path": "examples/bayesian_regression.py"}], "after_files": [{"content": "import numpy as np\nimport argparse\nimport torch\nimport torch.nn as nn\nfrom torch.nn.functional import normalize  # noqa: F401\n\nfrom torch.autograd import Variable\n\nimport pyro\nfrom pyro.distributions import Normal, Bernoulli  # noqa: F401\nfrom pyro.infer import SVI\nfrom pyro.optim import Adam\n\n\"\"\"\nBayesian Regression\nLearning a function of the form:\n    y = wx + b\n\"\"\"\n\n\n# generate toy dataset\ndef build_linear_dataset(N, p, noise_std=0.01):\n    X = np.random.rand(N, p)\n    # use random integer weights from [0, 7]\n    w = np.random.randint(8, size=p)\n    # set b = 1\n    y = np.matmul(X, w) + np.repeat(1, N) + np.random.normal(0, noise_std, size=N)\n    y = y.reshape(N, 1)\n    X, y = Variable(torch.Tensor(X)), Variable(torch.Tensor(y))\n    return torch.cat((X, y), 1)\n\n\n# NN with one linear layer\nclass RegressionModel(nn.Module):\n    def __init__(self, p):\n        super(RegressionModel, self).__init__()\n        self.linear = nn.Linear(p, 1)\n\n    def forward(self, x):\n        # x * w + b\n        return self.linear(x)\n\n\nN = 100  # size of toy data\np = 1  # number of features\n\nsoftplus = nn.Softplus()\nregression_model = RegressionModel(p)\n\n\ndef model(data):\n    # Create unit normal priors over the parameters\n    mu = Variable(torch.zeros(1, p)).type_as(data)\n    sigma = Variable(torch.ones(1, p)).type_as(data)\n    bias_mu = Variable(torch.zeros(1)).type_as(data)\n    bias_sigma = Variable(torch.ones(1)).type_as(data)\n    w_prior, b_prior = Normal(mu, sigma), Normal(bias_mu, bias_sigma)\n    priors = {'linear.weight': w_prior, 'linear.bias': b_prior}\n    # lift module parameters to random variables sampled from the priors\n    lifted_module = pyro.random_module(\"module\", regression_model, priors)\n    # sample a regressor (which also samples w and b)\n    lifted_reg_model = lifted_module()\n\n    with pyro.iarange(\"map\", N, subsample=data):\n        x_data = data[:, :-1]\n        y_data = data[:, -1]\n        # run the regressor forward conditioned on inputs\n        prediction_mean = lifted_reg_model(x_data).squeeze(1)\n        pyro.sample(\"obs\",\n                    Normal(prediction_mean, Variable(torch.ones(data.size(0))).type_as(data)),\n                    obs=y_data.squeeze())\n\n\ndef guide(data):\n    w_mu = Variable(torch.randn(1, p).type_as(data.data), requires_grad=True)\n    w_log_sig = Variable((-3.0 * torch.ones(1, p) + 0.05 * torch.randn(1, p)).type_as(data.data), requires_grad=True)\n    b_mu = Variable(torch.randn(1).type_as(data.data), requires_grad=True)\n    b_log_sig = Variable((-3.0 * torch.ones(1) + 0.05 * torch.randn(1)).type_as(data.data), requires_grad=True)\n    # register learnable params in the param store\n    mw_param = pyro.param(\"guide_mean_weight\", w_mu)\n    sw_param = softplus(pyro.param(\"guide_log_sigma_weight\", w_log_sig))\n    mb_param = pyro.param(\"guide_mean_bias\", b_mu)\n    sb_param = softplus(pyro.param(\"guide_log_sigma_bias\", b_log_sig))\n    # gaussian guide distributions for w and b\n    w_dist = Normal(mw_param, sw_param)\n    b_dist = Normal(mb_param, sb_param)\n    dists = {'linear.weight': w_dist, 'linear.bias': b_dist}\n    # overloading the parameters in the module with random samples from the guide distributions\n    lifted_module = pyro.random_module(\"module\", regression_model, dists)\n    # sample a regressor\n    return lifted_module()\n\n\n# instantiate optim and inference objects\noptim = Adam({\"lr\": 0.001})\nsvi = SVI(model, guide, optim, loss=\"ELBO\")\n\n\n# get array of batch indices\ndef get_batch_indices(N, batch_size):\n    all_batches = np.arange(0, N, batch_size)\n    if all_batches[-1] != N:\n        all_batches = list(all_batches) + [N]\n    return all_batches\n\n\ndef main(args):\n    data = build_linear_dataset(N, p)\n    if args.cuda:\n        # make tensors and modules CUDA\n        data = data.cuda()\n        softplus.cuda()\n        regression_model.cuda()\n    for j in range(args.num_epochs):\n        if args.batch_size == N:\n            # use the entire data set\n            epoch_loss = svi.step(data)\n        else:\n            # mini batch\n            epoch_loss = 0.0\n            perm = torch.randperm(N) if not args.cuda else torch.randperm(N).cuda()\n            # shuffle data\n            data = data[perm]\n            # get indices of each batch\n            all_batches = get_batch_indices(N, args.batch_size)\n            for ix, batch_start in enumerate(all_batches[:-1]):\n                batch_end = all_batches[ix + 1]\n                batch_data = data[batch_start: batch_end]\n                epoch_loss += svi.step(batch_data)\n        if j % 100 == 0:\n            print(\"epoch avg loss {}\".format(epoch_loss/float(N)))\n\n\nif __name__ == '__main__':\n    parser = argparse.ArgumentParser(description=\"parse args\")\n    parser.add_argument('-n', '--num-epochs', default=1000, type=int)\n    parser.add_argument('-b', '--batch-size', default=N, type=int)\n    parser.add_argument('--cuda', action='store_true')\n    args = parser.parse_args()\n    main(args)\n", "path": "examples/bayesian_regression.py"}]}