-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
cd694eb
WIP sketch multivariate pattern recognition
fritzo 3c51566
WIP more progress on eager_mvn and .extract_affine()
fritzo c0ec203
Add minimal failing tests for sensor fusion example (#247)
fritzo 85dc66a
Add ReshapeOp, .shape property, .reshape() method
fritzo 4580d5f
Add Tensor special case and tests
fritzo 98b4ce1
Merge branch 'master' into multivariate-affine
fritzo 708307e
Start writing a test
fritzo fb50652
Fix bugs in Einsum
fritzo 4ee22e6
Add more affine tests
fritzo 155faaa
Add more tests
fritzo 07b3da6
Revert changes to cnf.py
fritzo 279d7a9
Implement affine funsor approximation
fritzo 0a83471
Add test for Einsum batching
fritzo 437c3a4
Merge branch 'extract-affine' into multivariate-affine
fritzo d9138fc
Fix docs
fritzo ad70e1d
Address review comments
fritzo 8df92c0
Merge branch 'extract-affine' into multivariate-affine
fritzo 30dab86
Merge branch 'master' into multivariate-affine
fritzo c7d10c6
Add sensor fusion test using dist.MultivariateNormal
fritzo 8d57895
WIP attempt to fix eager_mvn
fritzo 4f55bd6
Add ops.matmul
fritzo 3e543d3
Use matmul and expand=True in distributions and pyro.convert
fritzo 6161a86
Merge branch 'matmul-op' into multivariate-affine
fritzo 45878c5
More fixes to eager_mvn
fritzo dc848d6
Merge branch 'master' into multivariate-affine
fritzo 4eec408
Remove debug statements
fritzo f36e864
Fix some typos
fritzo 3e8eed1
Fix shape errors
fritzo cc181f0
Fix const computation
fritzo ffe0c55
Add failing tests
fritzo 4768323
Add more failing tests
fritzo 914b223
Address review comments
fritzo b17cc7e
Simplify
fritzo 9e001bb
Simplify more
fritzo 3c9e3e5
Fix bugs
fritzo 2da0e3b
Strengthen tests
fritzo c7f71a5
Revert unnecessary change
fritzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import pytest | ||
import torch | ||
|
||
import funsor.distributions as dist | ||
import funsor.ops as ops | ||
from funsor.cnf import Contraction | ||
from funsor.domains import bint, reals | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jpchen you can follow the idioms of this test in your experiment. |
||
data = Tensor(torch.randn(2, obs_dim))["time"] | ||
|
||
bias = Variable("bias", reals(obs_dim)) | ||
bias_dist = dist_to_funsor(random_mvn((), obs_dim))(value=bias) | ||
|
||
prev = Variable("prev", reals(state_dim)) | ||
curr = Variable("curr", reals(state_dim)) | ||
trans_mat = Tensor(torch.eye(state_dim) + 0.1 * torch.randn(state_dim, state_dim)) | ||
trans_mvn = random_mvn((), state_dim) | ||
trans_dist = dist.MultivariateNormal( | ||
loc=trans_mvn.loc, | ||
scale_tril=trans_mvn.scale_tril, | ||
value=curr - prev @ trans_mat) | ||
|
||
state = Variable("state", reals(state_dim)) | ||
obs = Variable("obs", reals(obs_dim)) | ||
obs_mat = Tensor(torch.randn(state_dim, obs_dim)) | ||
obs_mvn = random_mvn((), obs_dim) | ||
obs_dist = dist.MultivariateNormal( | ||
loc=obs_mvn.loc, | ||
scale_tril=obs_mvn.scale_tril, | ||
value=state @ obs_mat + bias - obs) | ||
|
||
log_prob = 0 | ||
log_prob += bias_dist | ||
|
||
state_0 = Variable("state_0", reals(state_dim)) | ||
log_prob += obs_dist(state=state_0, obs=data(time=0)) | ||
|
||
state_1 = Variable("state_1", reals(state_dim)) | ||
log_prob += trans_dist(prev=state_0, curr=state_1) | ||
log_prob += obs_dist(state=state_1, obs=data(time=1)) | ||
|
||
log_prob = log_prob.reduce(ops.logaddexp) | ||
assert isinstance(log_prob, Tensor), log_prob.pretty() | ||
|
||
|
||
# This version constructs factors using funsor.pyro.convert. | ||
@pytest.mark.xfail(reason="missing pattern") | ||
def test_end_to_end(): | ||
def test_pyro_convert(): | ||
data = Tensor(torch.randn(2, 2), OrderedDict([("time", bint(2))])) | ||
|
||
bias_dist = dist_to_funsor(random_mvn((), 2)) | ||
|
@@ -44,7 +86,7 @@ def test_end_to_end(): | |
|
||
@pytest.mark.xfail(reason="missing pattern") | ||
def test_affine_subs(): | ||
# This was recorded from test_end_to_end. | ||
# This was recorded from test_pyro_convert. | ||
x = Subs( | ||
Gaussian( | ||
torch.tensor([1.3027106523513794, 1.4167094230651855, -0.9750942587852478, 0.5321089029312134, -0.9039931297302246], dtype=torch.float32), # noqa | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofshape[-1]
,scale_diag.log().sum()
instead ofscale_diag.log().sum(-1)
, and0.5 * (const ** 2).sum()
instead of0.5 * (const ** 2).sum(-1)
here?There was a problem hiding this comment.
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]
. Herescale_diag
is a funsor, and it separates "batch".inputs
from "event".shape
. In factscale_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.There was a problem hiding this comment.
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.