prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  pytorch broadcasting
  in various places in the codebase we have tensor ops like `expand_as()`. given the extended broadcasting functionality in the latest version of pytorch, some of these may be unnecessary and/or clunky. more generally, we should investigate and deal with any pytorch warnings that popped once once we switched pytorch versions. for example: 
  
  _UserWarning: other is not broadcastable to self, but they have the same number of elements.  Falling back to deprecated pointwise behavior._
  
  --- 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/categorical_bmm.py`
  Content:
  ```
  1 import argparse
  2 
  3 import numpy as np
  4 import torch
  5 import torchvision.datasets as dset
  6 import torchvision.transforms as transforms
  7 import visdom
  8 from torch.autograd import Variable
  9 from torch.nn import Softmax
  10 
  11 import pyro
  12 from pyro.distributions import Bernoulli, Categorical
  13 from pyro.infer.kl_qp import KL_QP
  14 
  15 mnist = dset.MNIST(
  16     root='./data',
  17     train=True,
  18     transform=None,
  19     target_transform=None,
  20     download=True)
  21 print('dataset loaded')
  22 
  23 softmax = Softmax()
  24 
  25 train_loader = torch.utils.data.DataLoader(
  26     dset.MNIST('../data', train=True, download=True,
  27                transform=transforms.Compose([
  28                    transforms.ToTensor(),
  29                    transforms.Normalize((0.1307,), (0.3081,))
  30                ])),
  31     batch_size=128, shuffle=True)
  32 test_loader = torch.utils.data.DataLoader(
  33     dset.MNIST('../data', train=False, transform=transforms.Compose([
  34         transforms.ToTensor(),
  35         transforms.Normalize((0.1307,), (0.3081,))
  36     ])),
  37     batch_size=128, shuffle=True)
  38 
  39 
  40 def local_model(i, datum):
  41     beta = Variable(torch.ones(1, 10)) * 0.1
  42     cll = pyro.sample("class_of_datum_" + str(i), Categorical(beta))
  43     mean_param = Variable(torch.zeros(1, 784), requires_grad=True)
  44     # do MLE for class means
  45     mu = pyro.param("mean_of_class_" + str(cll[0]), mean_param)
  46     mu_param = softmax(mu)
  47     pyro.observe("obs_" + str(i), Bernoulli(mu_param), datum)
  48     return cll
  49 
  50 
  51 def local_guide(i, datum):
  52     alpha = torch.ones(1, 10) * 0.1
  53     beta_q = Variable(alpha, requires_grad=True)
  54     beta_param = pyro.param("class_posterior_", beta_q)
  55     guide_params = softmax(beta_param)
  56     cll = pyro.sample("class_of_datum_" + str(i), Categorical(guide_params))
  57     return cll
  58 
  59 
  60 def inspect_posterior_samples(i):
  61     cll = local_guide(i, None)
  62     mean_param = Variable(torch.zeros(1, 784), requires_grad=True)
  63     # do MLE for class means
  64     mu = pyro.param("mean_of_class_" + str(cll[0]), mean_param)
  65     dat = pyro.sample("obs_" + str(i), Bernoulli(mu))
  66     return dat
  67 
  68 
  69 optim_fct = pyro.optim(torch.optim.Adam, {'lr': .0001})
  70 
  71 inference = KL_QP(local_model, local_guide, optim_fct)
  72 
  73 vis = visdom.Visdom()
  74 
  75 nr_epochs = 50
  76 # apply it to minibatches of data by hand:
  77 
  78 mnist_data = Variable(train_loader.dataset.train_data.float() / 255.)
  79 mnist_labels = Variable(train_loader.dataset.train_labels)
  80 mnist_size = mnist_data.size(0)
  81 batch_size = 1  # 64
  82 
  83 all_batches = np.arange(0, mnist_size, batch_size)
  84 
  85 if all_batches[-1] != mnist_size:
  86     all_batches = list(all_batches) + [mnist_size]
  87 
  88 
  89 def main():
  90     parser = argparse.ArgumentParser(description="parse args")
  91     parser.add_argument('-n', '--num-epochs', nargs='?', default=1000, type=int)
  92     args = parser.parse_args()
  93     for i in range(args.num_epochs):
  94         epoch_loss = 0.
  95         for ix, batch_start in enumerate(all_batches[:-1]):
  96             batch_end = all_batches[ix + 1]
  97             batch_data = mnist_data[batch_start:batch_end]
  98             bs_size = batch_data.size(0)
  99             batch_class_raw = mnist_labels[batch_start:batch_end]
  100             batch_class = torch.zeros(bs_size, 10)  # maybe it needs a FloatTensor
  101             batch_class.scatter_(1, batch_class_raw.data.view(-1, 1), 1)
  102             batch_class = Variable(batch_class)
  103             epoch_loss += inference.step(ix, batch_data)
  104 
  105             # optional  visualization!
  106             #      vis.image(batch_data[0].view(28, 28).data.numpy())
  107             #      vis.image(sample[0].view(28, 28).data.numpy())
  108             #      vis.image(sample_mu[0].view(28, 28).data.numpy())
  109         print("epoch avg loss {}".format(epoch_loss / float(mnist_size)))
  110 
  111 
  112 if __name__ == '__main__':
  113     main()
  114
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/examples/categorical_bmm.py b/examples/categorical_bmm.py
  --- a/examples/categorical_bmm.py
  +++ b/examples/categorical_bmm.py
  @@ -12,6 +12,7 @@
   from pyro.distributions import Bernoulli, Categorical
   from pyro.infer.kl_qp import KL_QP
   
  +
   mnist = dset.MNIST(
       root='./data',
       train=True,
  @@ -44,7 +45,7 @@
       # do MLE for class means
       mu = pyro.param("mean_of_class_" + str(cll[0]), mean_param)
       mu_param = softmax(mu)
  -    pyro.observe("obs_" + str(i), Bernoulli(mu_param), datum)
  +    pyro.observe("obs_" + str(i), Bernoulli(mu_param), datum.view(1, -1))
       return cll
  
verification_info: {"golden_diff": "diff --git a/examples/categorical_bmm.py b/examples/categorical_bmm.py\n--- a/examples/categorical_bmm.py\n+++ b/examples/categorical_bmm.py\n@@ -12,6 +12,7 @@\n from pyro.distributions import Bernoulli, Categorical\n from pyro.infer.kl_qp import KL_QP\n \n+\n mnist = dset.MNIST(\n     root='./data',\n     train=True,\n@@ -44,7 +45,7 @@\n     # do MLE for class means\n     mu = pyro.param(\"mean_of_class_\" + str(cll[0]), mean_param)\n     mu_param = softmax(mu)\n-    pyro.observe(\"obs_\" + str(i), Bernoulli(mu_param), datum)\n+    pyro.observe(\"obs_\" + str(i), Bernoulli(mu_param), datum.view(1, -1))\n     return cll\n", "issue": "pytorch broadcasting\nin various places in the codebase we have tensor ops like `expand_as()`. given the extended broadcasting functionality in the latest version of pytorch, some of these may be unnecessary and/or clunky. more generally, we should investigate and deal with any pytorch warnings that popped once once we switched pytorch versions. for example: \r\n\r\n_UserWarning: other is not broadcastable to self, but they have the same number of elements.  Falling back to deprecated pointwise behavior._\n", "before_files": [{"content": "import argparse\n\nimport numpy as np\nimport torch\nimport torchvision.datasets as dset\nimport torchvision.transforms as transforms\nimport visdom\nfrom torch.autograd import Variable\nfrom torch.nn import Softmax\n\nimport pyro\nfrom pyro.distributions import Bernoulli, Categorical\nfrom pyro.infer.kl_qp import KL_QP\n\nmnist = dset.MNIST(\n    root='./data',\n    train=True,\n    transform=None,\n    target_transform=None,\n    download=True)\nprint('dataset loaded')\n\nsoftmax = Softmax()\n\ntrain_loader = torch.utils.data.DataLoader(\n    dset.MNIST('../data', train=True, download=True,\n               transform=transforms.Compose([\n                   transforms.ToTensor(),\n                   transforms.Normalize((0.1307,), (0.3081,))\n               ])),\n    batch_size=128, shuffle=True)\ntest_loader = torch.utils.data.DataLoader(\n    dset.MNIST('../data', train=False, transform=transforms.Compose([\n        transforms.ToTensor(),\n        transforms.Normalize((0.1307,), (0.3081,))\n    ])),\n    batch_size=128, shuffle=True)\n\n\ndef local_model(i, datum):\n    beta = Variable(torch.ones(1, 10)) * 0.1\n    cll = pyro.sample(\"class_of_datum_\" + str(i), Categorical(beta))\n    mean_param = Variable(torch.zeros(1, 784), requires_grad=True)\n    # do MLE for class means\n    mu = pyro.param(\"mean_of_class_\" + str(cll[0]), mean_param)\n    mu_param = softmax(mu)\n    pyro.observe(\"obs_\" + str(i), Bernoulli(mu_param), datum)\n    return cll\n\n\ndef local_guide(i, datum):\n    alpha = torch.ones(1, 10) * 0.1\n    beta_q = Variable(alpha, requires_grad=True)\n    beta_param = pyro.param(\"class_posterior_\", beta_q)\n    guide_params = softmax(beta_param)\n    cll = pyro.sample(\"class_of_datum_\" + str(i), Categorical(guide_params))\n    return cll\n\n\ndef inspect_posterior_samples(i):\n    cll = local_guide(i, None)\n    mean_param = Variable(torch.zeros(1, 784), requires_grad=True)\n    # do MLE for class means\n    mu = pyro.param(\"mean_of_class_\" + str(cll[0]), mean_param)\n    dat = pyro.sample(\"obs_\" + str(i), Bernoulli(mu))\n    return dat\n\n\noptim_fct = pyro.optim(torch.optim.Adam, {'lr': .0001})\n\ninference = KL_QP(local_model, local_guide, optim_fct)\n\nvis = visdom.Visdom()\n\nnr_epochs = 50\n# apply it to minibatches of data by hand:\n\nmnist_data = Variable(train_loader.dataset.train_data.float() / 255.)\nmnist_labels = Variable(train_loader.dataset.train_labels)\nmnist_size = mnist_data.size(0)\nbatch_size = 1  # 64\n\nall_batches = np.arange(0, mnist_size, batch_size)\n\nif all_batches[-1] != mnist_size:\n    all_batches = list(all_batches) + [mnist_size]\n\n\ndef main():\n    parser = argparse.ArgumentParser(description=\"parse args\")\n    parser.add_argument('-n', '--num-epochs', nargs='?', default=1000, type=int)\n    args = parser.parse_args()\n    for i in range(args.num_epochs):\n        epoch_loss = 0.\n        for ix, batch_start in enumerate(all_batches[:-1]):\n            batch_end = all_batches[ix + 1]\n            batch_data = mnist_data[batch_start:batch_end]\n            bs_size = batch_data.size(0)\n            batch_class_raw = mnist_labels[batch_start:batch_end]\n            batch_class = torch.zeros(bs_size, 10)  # maybe it needs a FloatTensor\n            batch_class.scatter_(1, batch_class_raw.data.view(-1, 1), 1)\n            batch_class = Variable(batch_class)\n            epoch_loss += inference.step(ix, batch_data)\n\n            # optional  visualization!\n            #      vis.image(batch_data[0].view(28, 28).data.numpy())\n            #      vis.image(sample[0].view(28, 28).data.numpy())\n            #      vis.image(sample_mu[0].view(28, 28).data.numpy())\n        print(\"epoch avg loss {}\".format(epoch_loss / float(mnist_size)))\n\n\nif __name__ == '__main__':\n    main()\n", "path": "examples/categorical_bmm.py"}], "after_files": [{"content": "import argparse\n\nimport numpy as np\nimport torch\nimport torchvision.datasets as dset\nimport torchvision.transforms as transforms\nimport visdom\nfrom torch.autograd import Variable\nfrom torch.nn import Softmax\n\nimport pyro\nfrom pyro.distributions import Bernoulli, Categorical\nfrom pyro.infer.kl_qp import KL_QP\n\n\nmnist = dset.MNIST(\n    root='./data',\n    train=True,\n    transform=None,\n    target_transform=None,\n    download=True)\nprint('dataset loaded')\n\nsoftmax = Softmax()\n\ntrain_loader = torch.utils.data.DataLoader(\n    dset.MNIST('../data', train=True, download=True,\n               transform=transforms.Compose([\n                   transforms.ToTensor(),\n                   transforms.Normalize((0.1307,), (0.3081,))\n               ])),\n    batch_size=128, shuffle=True)\ntest_loader = torch.utils.data.DataLoader(\n    dset.MNIST('../data', train=False, transform=transforms.Compose([\n        transforms.ToTensor(),\n        transforms.Normalize((0.1307,), (0.3081,))\n    ])),\n    batch_size=128, shuffle=True)\n\n\ndef local_model(i, datum):\n    beta = Variable(torch.ones(1, 10)) * 0.1\n    cll = pyro.sample(\"class_of_datum_\" + str(i), Categorical(beta))\n    mean_param = Variable(torch.zeros(1, 784), requires_grad=True)\n    # do MLE for class means\n    mu = pyro.param(\"mean_of_class_\" + str(cll[0]), mean_param)\n    mu_param = softmax(mu)\n    pyro.observe(\"obs_\" + str(i), Bernoulli(mu_param), datum.view(1, -1))\n    return cll\n\n\ndef local_guide(i, datum):\n    alpha = torch.ones(1, 10) * 0.1\n    beta_q = Variable(alpha, requires_grad=True)\n    beta_param = pyro.param(\"class_posterior_\", beta_q)\n    guide_params = softmax(beta_param)\n    cll = pyro.sample(\"class_of_datum_\" + str(i), Categorical(guide_params))\n    return cll\n\n\ndef inspect_posterior_samples(i):\n    cll = local_guide(i, None)\n    mean_param = Variable(torch.zeros(1, 784), requires_grad=True)\n    # do MLE for class means\n    mu = pyro.param(\"mean_of_class_\" + str(cll[0]), mean_param)\n    dat = pyro.sample(\"obs_\" + str(i), Bernoulli(mu))\n    return dat\n\n\noptim_fct = pyro.optim(torch.optim.Adam, {'lr': .0001})\n\ninference = KL_QP(local_model, local_guide, optim_fct)\n\nvis = visdom.Visdom()\n\nnr_epochs = 50\n# apply it to minibatches of data by hand:\n\nmnist_data = Variable(train_loader.dataset.train_data.float() / 255.)\nmnist_labels = Variable(train_loader.dataset.train_labels)\nmnist_size = mnist_data.size(0)\nbatch_size = 1  # 64\n\nall_batches = np.arange(0, mnist_size, batch_size)\n\nif all_batches[-1] != mnist_size:\n    all_batches = list(all_batches) + [mnist_size]\n\n\ndef main():\n    parser = argparse.ArgumentParser(description=\"parse args\")\n    parser.add_argument('-n', '--num-epochs', nargs='?', default=1000, type=int)\n    args = parser.parse_args()\n    for i in range(args.num_epochs):\n        epoch_loss = 0.\n        for ix, batch_start in enumerate(all_batches[:-1]):\n            batch_end = all_batches[ix + 1]\n            batch_data = mnist_data[batch_start:batch_end]\n            bs_size = batch_data.size(0)\n            batch_class_raw = mnist_labels[batch_start:batch_end]\n            batch_class = torch.zeros(bs_size, 10)  # maybe it needs a FloatTensor\n            batch_class.scatter_(1, batch_class_raw.data.view(-1, 1), 1)\n            batch_class = Variable(batch_class)\n            epoch_loss += inference.step(ix, batch_data)\n\n            # optional  visualization!\n            #      vis.image(batch_data[0].view(28, 28).data.numpy())\n            #      vis.image(sample[0].view(28, 28).data.numpy())\n            #      vis.image(sample_mu[0].view(28, 28).data.numpy())\n        print(\"epoch avg loss {}\".format(epoch_loss / float(mnist_size)))\n\n\nif __name__ == '__main__':\n    main()\n", "path": "examples/categorical_bmm.py"}]}