Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pattern for MultivariateNormal(affine) #245

Merged
merged 37 commits into from
Sep 21, 2019
Merged

Conversation

fritzo
Copy link
Member

@fritzo fritzo commented Sep 19, 2019

Addresses #72
pair coded with @eb8680

Tested

  • unit tests for MultivariateNormal(x + y, ...) etc.
  • sensor fusion example using dist.MultivariateNormal directly

@fritzo fritzo added the WIP label Sep 19, 2019
@fritzo fritzo changed the title WIP sketch multivariate pattern recognition Implement multivariate affine pattern recognition Sep 19, 2019
@fritzo fritzo changed the title Implement multivariate affine pattern recognition Add pattern for MultivariateNormal(affine) Sep 19, 2019
@fritzo fritzo mentioned this pull request Sep 20, 2019
2 tasks
@fritzo
Copy link
Member Author

fritzo commented Sep 20, 2019

OK, I've fixed most of the shape errors and we're down to numerical errors only 😄
I'd appreciate any help from reviewers.

@fritzo fritzo requested review from eb8680 and fehiepsi September 21, 2019 13:44
@fritzo
Copy link
Member Author

fritzo commented Sep 21, 2019

Yay all tests pass 😄 Thanks for all the help @fehiepsi !

@@ -13,8 +14,49 @@
from funsor.torch import Tensor


# This version constructs factors using funsor.distributions.
@pytest.mark.parametrize('state_dim,obs_dim', [(3, 2), (2, 3)])
def test_distributions(state_dim, obs_dim):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpchen you can follow the idioms of this test in your experiment.

Copy link
Member

@fehiepsi fehiepsi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks beautiful for me! There is just a small point which I don't understand yet.


# Compute log_prob using funsors.
scale_diag = Tensor(scale_tril.data.diagonal(dim1=-1, dim2=-2), scale_tril.inputs)
log_prob = (-0.5 * scale_diag.shape[0] * math.log(2 * math.pi)
Copy link
Member

@fehiepsi fehiepsi Sep 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: why we use shape[0] instead of shape[-1], scale_diag.log().sum() instead of scale_diag.log().sum(-1), and 0.5 * (const ** 2).sum() instead of 0.5 * (const ** 2).sum(-1) here?

Copy link
Member Author

@fritzo fritzo Sep 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are equivalent: scale_diag.shape[0] == scale_diag.shape[-1]. Here scale_diag is a funsor, and it separates "batch" .inputs from "event" .shape. In fact scale_diag.shape == (dim,) regardless of batching. This also allows us to call .sum() below rather than .sum(-1), since there is only one tensor dimension.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nice! Thanks for explaining.

@fritzo fritzo merged commit 6cc0a38 into master Sep 21, 2019
@fritzo fritzo deleted the multivariate-affine branch October 11, 2019 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants