-
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.
Merge pull request #6 from davidnabergoj/dev
Performance improvements and fixes
- Loading branch information
Showing
31 changed files
with
270 additions
and
172 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
We show how to fit normalizing flows using stochastic variational inference (SVI). Whereas traditional maximum | ||
likelihood estimation requires a fixed dataset of samples, SVI lets us optimize NF parameters with the unnormalized | ||
target log density function. | ||
|
||
As an example, we define the unnormalized log density of a diagonal Gaussian. We assume this target has 10 dimensions | ||
with mean 5 and variance 9 in each dimension: | ||
|
||
```python | ||
import torch | ||
|
||
torch.manual_seed(0) | ||
|
||
event_shape = (10,) | ||
true_mean = torch.full(size=event_shape, fill_value=5.0) | ||
true_variance = torch.full(size=event_shape, fill_value=9.0) | ||
|
||
|
||
def target_log_prob(x: torch.Tensor): | ||
return torch.sum(-((x - true_mean) ** 2 / (2 * true_variance)), dim=1) | ||
``` | ||
|
||
We define the flow and run the variational fit: | ||
|
||
```python | ||
from normalizing_flows import Flow | ||
from normalizing_flows.bijections import RealNVP | ||
|
||
torch.manual_seed(0) | ||
flow = Flow(RealNVP(event_shape=event_shape)) | ||
flow.variational_fit(target_log_prob, show_progress=True) | ||
``` | ||
|
||
We plot samples from the trained flow. We also print estimated marginal means and variances. We see that the estimates are roughly accurate. | ||
```python | ||
import matplotlib.pyplot as plt | ||
|
||
torch.manual_seed(0) | ||
x_flow = flow.sample(10000).detach() | ||
|
||
plt.figure() | ||
plt.scatter(x_flow[:, 0], x_flow[:, 1]) | ||
plt.show() | ||
|
||
print(f'{torch.mean(x_flow, dim=0) = }') | ||
print(f'{torch.var(x_flow, dim=0) = }') | ||
``` |
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
21 changes: 0 additions & 21 deletions
21
normalizing_flows/bijections/finite/autoregressive/conditioners/base.py
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
normalizing_flows/bijections/finite/autoregressive/conditioners/graphical.py
This file was deleted.
Oops, something went wrong.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.