{"action":"created","comment":{"url":"https://api.github.com/repos/uber/pyro/pulls/comments/151017671","pull_request_review_id":76657511,"id":151017671,"diff_hunk":"@@ -0,0 +1,36 @@\n+from __future__ import absolute_import, division, print_function\n+\n+from torch.autograd import Variable\n+import pyro  # XXX remove this cyclic dependency\n+from pyro.params import _PYRO_PARAM_STORE\n+\n+from .poutine import Poutine\n+\n+\n+class LowerPoutine(Poutine):\n+    \"\"\"\n+    Poutine which \"lowers\" sample sites to param + observe sites.\n+\n+    Given a stochastic function with sample calls from a prior, creates a\n+    stochastic function where each sample site is replaced by a param site\n+    (denoting the parameter) plus an observe site (denoting the prior).\n+\n+    :param fn: stochastic function\n+    \"\"\"\n+    def _pyro_sample(self, msg):\n+        if msg[\"is_observed\"]:\n+            return super(LowerPoutine, self)._pyro_sample(msg)\n+        param_name = msg[\"name\"]\n+        msg[\"name\"] = \"prior.{}\".format(param_name)\n+        if param_name in _PYRO_PARAM_STORE:\n+            # Treat existing param value as an observation.\n+            msg[\"is_observed\"] = True\n+            msg[\"value\"] = _PYRO_PARAM_STORE.get_param(param_name)\n+            value = super(LowerPoutine, self)._pyro_sample(msg)\n+        else:\n+            # Sample an initial param value from the prior.\n+            value = super(LowerPoutine, self)._pyro_sample(msg)\n+            value = Variable(value.data, requires_grad=True)\n+            msg[\"value\"] = value\n+            msg[\"is_observed\"] = True\n+        return pyro.param(param_name, value)","path":"pyro/poutine/lower_poutine.py","position":36,"original_position":36,"commit_id":"bd09e96719b6d7b65d3e094d49c3c21029b64f6c","original_commit_id":"bd09e96719b6d7b65d3e094d49c3c21029b64f6c","user":{"login":"eb8680","id":2032320,"avatar_url":"https://avatars0.githubusercontent.com/u/2032320?v=4","gravatar_id":"","url":"https://api.github.com/users/eb8680","html_url":"https://github.com/eb8680","followers_url":"https://api.github.com/users/eb8680/followers","following_url":"https://api.github.com/users/eb8680/following{/other_user}","gists_url":"https://api.github.com/users/eb8680/gists{/gist_id}","starred_url":"https://api.github.com/users/eb8680/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eb8680/subscriptions","organizations_url":"https://api.github.com/users/eb8680/orgs","repos_url":"https://api.github.com/users/eb8680/repos","events_url":"https://api.github.com/users/eb8680/events{/privacy}","received_events_url":"https://api.github.com/users/eb8680/received_events","type":"User","site_admin":false},"body":"Wouldn't it be better if `param_name` were changed and `msg[\"name\"]` were preserved?","created_at":"2017-11-15T02:05:28Z","updated_at":"2017-11-15T02:05:28Z","html_url":"https://github.com/uber/pyro/pull/573#discussion_r151017671","pull_request_url":"https://api.github.com/repos/uber/pyro/pulls/573","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/uber/pyro/pulls/comments/151017671"},"html":{"href":"https://github.com/uber/pyro/pull/573#discussion_r151017671"},"pull_request":{"href":"https://api.github.com/repos/uber/pyro/pulls/573"}}},"pull_request":{"url":"https://api.github.com/repos/uber/pyro/pulls/573","id":152428927,"html_url":"https://github.com/uber/pyro/pull/573","diff_url":"https://github.com/uber/pyro/pull/573.diff","patch_url":"https://github.com/uber/pyro/pull/573.patch","issue_url":"https://api.github.com/repos/uber/pyro/issues/573","number":573,"state":"open","locked":false,"title":"Implement poutine.lower and MAP inference","user":{"login":"fritzo","id":648532,"avatar_url":"https://avatars0.githubusercontent.com/u/648532?v=4","gravatar_id":"","url":"https://api.github.com/users/fritzo","html_url":"https://github.com/fritzo","followers_url":"https://api.github.com/users/fritzo/followers","following_url":"https://api.github.com/users/fritzo/following{/other_user}","gists_url":"https://api.github.com/users/fritzo/gists{/gist_id}","starred_url":"https://api.github.com/users/fritzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fritzo/subscriptions","organizations_url":"https://api.github.com/users/fritzo/orgs","repos_url":"https://api.github.com/users/fritzo/repos","events_url":"https://api.github.com/users/fritzo/events{/privacy}","received_events_url":"https://api.github.com/users/fritzo/received_events","type":"User","site_admin":false},"body":"Fixes #570 \r\nFixes #577 \r\n\r\n## Why?\r\n\r\n@nbren12 requested a MAP wrapper around SVI in #570 \r\n\r\nMAP inference is also useful for beginner tutorials, since MAP is much more widely understood than Black-Box SVI.\r\n\r\n## How?\r\n\r\n`poutine.lower` transforms each sample site to a param site + a model site (see #577). There is a design choice regarding whether to (1) transform the sample site to a param site and create a new observe site, or (ii) transform the sample site to an observe site and add a new param site. This PR opts for (ii) since it is simpler and a little less intrusive.\r\n\r\nThis implementation `MAP` uses Maximum Likelihood inference for `pyro.param` sites and Maximum A Posterior inference for `pyro.sample` sites, both conditioned on `pyro.observe` sites.\r\n\r\n## Caveats\r\n\r\nThis PR surfaces two deep issues beyond the scope of this PR.\r\n\r\nDiscrete distributions cannot be `MAP` optimized. Since `pyro.distributions` does not expose a `.is_discrete` property, we cannot in general detect this. This suggests adding a `.is_discrete` property to distributions.\r\n\r\nWe currently have no way to restrict optimizer moves to the support of constrained distributions (like `Exponential` or `HalfCauchy`). This suggests exposing some sort of bijector to distributions classes, such as:\r\n```py\r\nclass Distribution(object):\r\n    def biject_params(self):\r\n        \"\"\"\r\n        Returns a bijector between constrained param space and unconstrained space.\r\n        This is often used for optimizing parameters in SVI or Maximum Likelihood.\r\n        \"\"\"\r\n    def biject_value(self):\r\n        \"\"\"\r\n        Returns a bjiector between constrained results and an unconstrained space.\r\n        This is often used for MAP inference or Hamiltonian Monte Carlo.\r\n        \"\"\"\r\n```\r\n\r\n## Tested\r\n\r\n- Added a test of `infer.MAP`\r\n- TODO add tests of `poutine.lower`","created_at":"2017-11-14T07:13:18Z","updated_at":"2017-11-15T02:05:28Z","closed_at":null,"merged_at":null,"merge_commit_sha":"cdbaed68b28dcc5bd79969b03e9882a6fa8990b9","assignee":null,"assignees":[],"requested_reviewers":[{"login":"neerajprad","id":1762463,"avatar_url":"https://avatars3.githubusercontent.com/u/1762463?v=4","gravatar_id":"","url":"https://api.github.com/users/neerajprad","html_url":"https://github.com/neerajprad","followers_url":"https://api.github.com/users/neerajprad/followers","following_url":"https://api.github.com/users/neerajprad/following{/other_user}","gists_url":"https://api.github.com/users/neerajprad/gists{/gist_id}","starred_url":"https://api.github.com/users/neerajprad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/neerajprad/subscriptions","organizations_url":"https://api.github.com/users/neerajprad/orgs","repos_url":"https://api.github.com/users/neerajprad/repos","events_url":"https://api.github.com/users/neerajprad/events{/privacy}","received_events_url":"https://api.github.com/users/neerajprad/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/uber/pyro/pulls/573/commits","review_comments_url":"https://api.github.com/repos/uber/pyro/pulls/573/comments","review_comment_url":"https://api.github.com/repos/uber/pyro/pulls/comments{/number}","comments_url":"https://api.github.com/repos/uber/pyro/issues/573/comments","statuses_url":"https://api.github.com/repos/uber/pyro/statuses/bd09e96719b6d7b65d3e094d49c3c21029b64f6c","head":{"label":"uber:map","ref":"map","sha":"bd09e96719b6d7b65d3e094d49c3c21029b64f6c","user":{"login":"uber","id":538264,"avatar_url":"https://avatars3.githubusercontent.com/u/538264?v=4","gravatar_id":"","url":"https://api.github.com/users/uber","html_url":"https://github.com/uber","followers_url":"https://api.github.com/users/uber/followers","following_url":"https://api.github.com/users/uber/following{/other_user}","gists_url":"https://api.github.com/users/uber/gists{/gist_id}","starred_url":"https://api.github.com/users/uber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uber/subscriptions","organizations_url":"https://api.github.com/users/uber/orgs","repos_url":"https://api.github.com/users/uber/repos","events_url":"https://api.github.com/users/uber/events{/privacy}","received_events_url":"https://api.github.com/users/uber/received_events","type":"Organization","site_admin":false},"repo":{"id":94506832,"name":"pyro","full_name":"uber/pyro","owner":{"login":"uber","id":538264,"avatar_url":"https://avatars3.githubusercontent.com/u/538264?v=4","gravatar_id":"","url":"https://api.github.com/users/uber","html_url":"https://github.com/uber","followers_url":"https://api.github.com/users/uber/followers","following_url":"https://api.github.com/users/uber/following{/other_user}","gists_url":"https://api.github.com/users/uber/gists{/gist_id}","starred_url":"https://api.github.com/users/uber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uber/subscriptions","organizations_url":"https://api.github.com/users/uber/orgs","repos_url":"https://api.github.com/users/uber/repos","events_url":"https://api.github.com/users/uber/events{/privacy}","received_events_url":"https://api.github.com/users/uber/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/uber/pyro","description":"Deep universal probabilistic programming with Python and PyTorch","fork":false,"url":"https://api.github.com/repos/uber/pyro","forks_url":"https://api.github.com/repos/uber/pyro/forks","keys_url":"https://api.github.com/repos/uber/pyro/keys{/key_id}","collaborators_url":"https://api.github.com/repos/uber/pyro/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/uber/pyro/teams","hooks_url":"https://api.github.com/repos/uber/pyro/hooks","issue_events_url":"https://api.github.com/repos/uber/pyro/issues/events{/number}","events_url":"https://api.github.com/repos/uber/pyro/events","assignees_url":"https://api.github.com/repos/uber/pyro/assignees{/user}","branches_url":"https://api.github.com/repos/uber/pyro/branches{/branch}","tags_url":"https://api.github.com/repos/uber/pyro/tags","blobs_url":"https://api.github.com/repos/uber/pyro/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/uber/pyro/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/uber/pyro/git/refs{/sha}","trees_url":"https://api.github.com/repos/uber/pyro/git/trees{/sha}","statuses_url":"https://api.github.com/repos/uber/pyro/statuses/{sha}","languages_url":"https://api.github.com/repos/uber/pyro/languages","stargazers_url":"https://api.github.com/repos/uber/pyro/stargazers","contributors_url":"https://api.github.com/repos/uber/pyro/contributors","subscribers_url":"https://api.github.com/repos/uber/pyro/subscribers","subscription_url":"https://api.github.com/repos/uber/pyro/subscription","commits_url":"https://api.github.com/repos/uber/pyro/commits{/sha}","git_commits_url":"https://api.github.com/repos/uber/pyro/git/commits{/sha}","comments_url":"https://api.github.com/repos/uber/pyro/comments{/number}","issue_comment_url":"https://api.github.com/repos/uber/pyro/issues/comments{/number}","contents_url":"https://api.github.com/repos/uber/pyro/contents/{+path}","compare_url":"https://api.github.com/repos/uber/pyro/compare/{base}...{head}","merges_url":"https://api.github.com/repos/uber/pyro/merges","archive_url":"https://api.github.com/repos/uber/pyro/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/uber/pyro/downloads","issues_url":"https://api.github.com/repos/uber/pyro/issues{/number}","pulls_url":"https://api.github.com/repos/uber/pyro/pulls{/number}","milestones_url":"https://api.github.com/repos/uber/pyro/milestones{/number}","notifications_url":"https://api.github.com/repos/uber/pyro/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/uber/pyro/labels{/name}","releases_url":"https://api.github.com/repos/uber/pyro/releases{/id}","deployments_url":"https://api.github.com/repos/uber/pyro/deployments","created_at":"2017-06-16T05:03:47Z","updated_at":"2017-11-15T01:07:17Z","pushed_at":"2017-11-14T23:26:54Z","git_url":"git://github.com/uber/pyro.git","ssh_url":"git@github.com:uber/pyro.git","clone_url":"https://github.com/uber/pyro.git","svn_url":"https://github.com/uber/pyro","homepage":"http://pyro.ai","size":23899,"stargazers_count":1865,"watchers_count":1865,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":165,"mirror_url":null,"archived":false,"open_issues_count":65,"forks":165,"open_issues":65,"watchers":1865,"default_branch":"dev"}},"base":{"label":"uber:dev","ref":"dev","sha":"df42d43d0da04b3c1cb8b83caf10551fccdf8a8e","user":{"login":"uber","id":538264,"avatar_url":"https://avatars3.githubusercontent.com/u/538264?v=4","gravatar_id":"","url":"https://api.github.com/users/uber","html_url":"https://github.com/uber","followers_url":"https://api.github.com/users/uber/followers","following_url":"https://api.github.com/users/uber/following{/other_user}","gists_url":"https://api.github.com/users/uber/gists{/gist_id}","starred_url":"https://api.github.com/users/uber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uber/subscriptions","organizations_url":"https://api.github.com/users/uber/orgs","repos_url":"https://api.github.com/users/uber/repos","events_url":"https://api.github.com/users/uber/events{/privacy}","received_events_url":"https://api.github.com/users/uber/received_events","type":"Organization","site_admin":false},"repo":{"id":94506832,"name":"pyro","full_name":"uber/pyro","owner":{"login":"uber","id":538264,"avatar_url":"https://avatars3.githubusercontent.com/u/538264?v=4","gravatar_id":"","url":"https://api.github.com/users/uber","html_url":"https://github.com/uber","followers_url":"https://api.github.com/users/uber/followers","following_url":"https://api.github.com/users/uber/following{/other_user}","gists_url":"https://api.github.com/users/uber/gists{/gist_id}","starred_url":"https://api.github.com/users/uber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uber/subscriptions","organizations_url":"https://api.github.com/users/uber/orgs","repos_url":"https://api.github.com/users/uber/repos","events_url":"https://api.github.com/users/uber/events{/privacy}","received_events_url":"https://api.github.com/users/uber/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/uber/pyro","description":"Deep universal probabilistic programming with Python and PyTorch","fork":false,"url":"https://api.github.com/repos/uber/pyro","forks_url":"https://api.github.com/repos/uber/pyro/forks","keys_url":"https://api.github.com/repos/uber/pyro/keys{/key_id}","collaborators_url":"https://api.github.com/repos/uber/pyro/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/uber/pyro/teams","hooks_url":"https://api.github.com/repos/uber/pyro/hooks","issue_events_url":"https://api.github.com/repos/uber/pyro/issues/events{/number}","events_url":"https://api.github.com/repos/uber/pyro/events","assignees_url":"https://api.github.com/repos/uber/pyro/assignees{/user}","branches_url":"https://api.github.com/repos/uber/pyro/branches{/branch}","tags_url":"https://api.github.com/repos/uber/pyro/tags","blobs_url":"https://api.github.com/repos/uber/pyro/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/uber/pyro/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/uber/pyro/git/refs{/sha}","trees_url":"https://api.github.com/repos/uber/pyro/git/trees{/sha}","statuses_url":"https://api.github.com/repos/uber/pyro/statuses/{sha}","languages_url":"https://api.github.com/repos/uber/pyro/languages","stargazers_url":"https://api.github.com/repos/uber/pyro/stargazers","contributors_url":"https://api.github.com/repos/uber/pyro/contributors","subscribers_url":"https://api.github.com/repos/uber/pyro/subscribers","subscription_url":"https://api.github.com/repos/uber/pyro/subscription","commits_url":"https://api.github.com/repos/uber/pyro/commits{/sha}","git_commits_url":"https://api.github.com/repos/uber/pyro/git/commits{/sha}","comments_url":"https://api.github.com/repos/uber/pyro/comments{/number}","issue_comment_url":"https://api.github.com/repos/uber/pyro/issues/comments{/number}","contents_url":"https://api.github.com/repos/uber/pyro/contents/{+path}","compare_url":"https://api.github.com/repos/uber/pyro/compare/{base}...{head}","merges_url":"https://api.github.com/repos/uber/pyro/merges","archive_url":"https://api.github.com/repos/uber/pyro/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/uber/pyro/downloads","issues_url":"https://api.github.com/repos/uber/pyro/issues{/number}","pulls_url":"https://api.github.com/repos/uber/pyro/pulls{/number}","milestones_url":"https://api.github.com/repos/uber/pyro/milestones{/number}","notifications_url":"https://api.github.com/repos/uber/pyro/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/uber/pyro/labels{/name}","releases_url":"https://api.github.com/repos/uber/pyro/releases{/id}","deployments_url":"https://api.github.com/repos/uber/pyro/deployments","created_at":"2017-06-16T05:03:47Z","updated_at":"2017-11-15T01:07:17Z","pushed_at":"2017-11-14T23:26:54Z","git_url":"git://github.com/uber/pyro.git","ssh_url":"git@github.com:uber/pyro.git","clone_url":"https://github.com/uber/pyro.git","svn_url":"https://github.com/uber/pyro","homepage":"http://pyro.ai","size":23899,"stargazers_count":1865,"watchers_count":1865,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":165,"mirror_url":null,"archived":false,"open_issues_count":65,"forks":165,"open_issues":65,"watchers":1865,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/uber/pyro/pulls/573"},"html":{"href":"https://github.com/uber/pyro/pull/573"},"issue":{"href":"https://api.github.com/repos/uber/pyro/issues/573"},"comments":{"href":"https://api.github.com/repos/uber/pyro/issues/573/comments"},"review_comments":{"href":"https://api.github.com/repos/uber/pyro/pulls/573/comments"},"review_comment":{"href":"https://api.github.com/repos/uber/pyro/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/uber/pyro/pulls/573/commits"},"statuses":{"href":"https://api.github.com/repos/uber/pyro/statuses/bd09e96719b6d7b65d3e094d49c3c21029b64f6c"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"eb8680"}}