-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
474460f
commit 4fb4a00
Showing
2 changed files
with
79 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from normalizing_flows.flows import FlowMixture, Flow | ||
from normalizing_flows.architectures import RealNVP, NICE, CouplingRQNSF | ||
import torch | ||
|
||
|
||
def test_basic(): | ||
torch.manual_seed(0) | ||
|
||
n_data = 100 | ||
n_dim = 10 | ||
x = torch.randn(size=(n_data, n_dim)) | ||
|
||
mixture = FlowMixture([ | ||
Flow(RealNVP(event_shape=(n_dim,))), | ||
Flow(NICE(event_shape=(n_dim,))), | ||
Flow(CouplingRQNSF(event_shape=(n_dim,))) | ||
]) | ||
|
||
log_prob = mixture.log_prob(x) | ||
assert log_prob.shape == (n_data,) | ||
assert torch.all(torch.isfinite(log_prob)) | ||
|
||
x_sampled = mixture.sample(n_data) | ||
assert x_sampled.shape == x.shape | ||
assert torch.all(torch.isfinite(x_sampled)) | ||
|
||
|
||
def test_medium(): | ||
torch.manual_seed(0) | ||
|
||
n_data = 1000 | ||
n_dim = 100 | ||
x = torch.randn(size=(n_data, n_dim)) | ||
|
||
mixture = FlowMixture([ | ||
Flow(RealNVP(event_shape=(n_dim,))), | ||
Flow(NICE(event_shape=(n_dim,))), | ||
Flow(CouplingRQNSF(event_shape=(n_dim,))) | ||
]) | ||
|
||
log_prob = mixture.log_prob(x) | ||
assert log_prob.shape == (n_data,) | ||
assert torch.all(torch.isfinite(log_prob)) | ||
|
||
x_sampled = mixture.sample(n_data) | ||
assert x_sampled.shape == x.shape | ||
assert torch.all(torch.isfinite(x_sampled)) | ||
|
||
|
||
def test_complex_event(): | ||
torch.manual_seed(0) | ||
|
||
n_data = 1000 | ||
event_shape = (2, 3, 4, 5) | ||
x = torch.randn(size=(n_data, *event_shape)) | ||
|
||
mixture = FlowMixture([ | ||
Flow(RealNVP(event_shape=event_shape)), | ||
Flow(NICE(event_shape=event_shape)), | ||
Flow(CouplingRQNSF(event_shape=event_shape)) | ||
]) | ||
|
||
log_prob = mixture.log_prob(x) | ||
assert log_prob.shape == (n_data,) | ||
assert torch.all(torch.isfinite(log_prob)) | ||
|
||
x_sampled = mixture.sample(n_data) | ||
assert x_sampled.shape == x.shape | ||
assert torch.all(torch.isfinite(x_sampled)) |