Repository: pyro-ppl/pyro

Issue:
Low count accuracy of AIR model
Hi,

I have been experimenting with the Attend Infer Repeat model. I downloaded the code from examples and run the code with the args provided in the tutorial (http://pyro.ai/examples/air.html), which is:

python main.py -n 200000 -blr 0.1 --z-pres-prior 0.01 --scale-prior-sd 0.2 --predict-net 200 --bl-predict-net 200 --decoder-output-use-sigmoid --decoder-output-bias -2 --seed 287710

But the highest count accuracy I get is around 76%. Could someone let me know if the args are not correct? I did not change any part of the code.

Also I am using pyro version 0.2.1 and pytorch version 0.4.0

Many thanks




Discussion:
Hi, did you let inference run to completion or are you stopping it after a few epochs?  Are you talking about count accuracy on the training images (as in the tutorial) or some unseen test images?

cc @null-a 
> Could someone let me know if the args are not correct?

The command line args in the tutorial are the ones I used, and it looks like you're using the same.

As well as answers to eb8680's questions, I'd be interested in hearing what other values for count accuracy you obtained, if that's possible. Thanks.
@eb8680 @null-a 
I let the inference run till the end (I believe it is specified by -n 200000). I am using the count accuracy function provided in 'main.py'. I just specified the args '--eval-every' to 1000. I have attached a plot of the count accuracy obtained. This plot looks very different from the plot given in the tutorial.

![acc_plot](https://user-images.githubusercontent.com/8089252/45356239-a5806600-b5ba-11e8-9bb2-e2b819eaf611.jpg)

@thematrixduo How many times have you run inference? What other values for final count accuracy have you obtained? Setting the random seed doesn't make this deterministic, so some variance is expected. If you're seeing consistently poor results, I'll try running it myself and see if I can spot anything odd going on. Thanks.
@null-a I have tried at least 5 times but for none of them did the count accuracy go above 80%. I have attached another plot. 
![acc_plot_2](https://user-images.githubusercontent.com/8089252/45372635-6a475c80-b5e5-11e8-8d74-7c4a8d3b9cf6.jpg)

@thematrixduo Great, thanks for the info. That sounds worse than I would expect, so I'll take a look. To begin, I guess I'll confirm I'm getting similar results to you, and then I'll go back and try running against the Pyro commit recorded in the tutorial.
@thematrixduo It looks like performance may have degraded after c99ea673. If you apply the following patch (to v0.2.1 say) and then re-try I think you will see performance similar to that reported in the tutorial.

```diff
diff --git a/examples/air/main.py b/examples/air/main.py
index df89df2..09bdf25 100644
--- a/examples/air/main.py
+++ b/examples/air/main.py
@@ -195,7 +195,7 @@ def main(**kwargs):
         vis.images(draw_many(x, tensor_to_objs(latents_to_tensor(z))))
 
     def per_param_optim_args(module_name, param_name):
-        lr = args.baseline_learning_rate if 'bl_' in param_name else args.learning_rate
+        lr = args.baseline_learning_rate if 'bl_' in param_name or 'bl_' in module_name else args.learning_rate
         return {'lr': lr}
 
     svi = SVI(air.model, air.guide,
```
If we confirm this fixes the problem, then I'll open a PR to apply the patch to dev. (BTW, I've not been using a fixed random seed when I run this locally, and I seem to achieve OK results consistently.)
@null-a good catch!
@null-a Thanks for the help!

Resolving patch:
diff --git a/examples/air/air.py b/examples/air/air.py
--- a/examples/air/air.py
+++ b/examples/air/air.py
@@ -282,7 +282,7 @@ def baseline_step(self, prev, inputs):
         if self.baseline_scalar is not None:
             bl_value = bl_value * self.baseline_scalar
 
-        infer_dict = dict(baseline_value=bl_value.squeeze(-1))
+        infer_dict = dict(baseline=dict(baseline_value=bl_value.squeeze(-1)))
         return infer_dict, bl_h, bl_c
 
 
diff --git a/examples/air/main.py b/examples/air/main.py
--- a/examples/air/main.py
+++ b/examples/air/main.py
@@ -194,8 +194,11 @@ def z_pres_prior_p(opt_step, time_step):
         z, x = air.prior(5, z_pres_prior_p=partial(z_pres_prior_p, 0))
         vis.images(draw_many(x, tensor_to_objs(latents_to_tensor(z))))
 
+    def isBaselineParam(module_name, param_name):
+        return 'bl_' in module_name or 'bl_' in param_name
+
     def per_param_optim_args(module_name, param_name):
-        lr = args.baseline_learning_rate if 'bl_' in param_name else args.learning_rate
+        lr = args.baseline_learning_rate if isBaselineParam(module_name, param_name) else args.learning_rate
         return {'lr': lr}
 
     adam = optim.Adam(per_param_optim_args)


Test patch: