-
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 #1 from davidnabergoj/dev
Add install instructions and examples
- Loading branch information
Showing
3 changed files
with
73 additions
and
7 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
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 @@ | ||
# Examples | ||
|
||
We provide minimal working examples on how to perform various common tasks with normalizing flows. | ||
We use Real NVP as an example, but you can replace it with any other bijection from `normalizing_flows.bijections`. | ||
|
||
## Training a normalizing flow on a fixed dataset | ||
```python | ||
import torch | ||
from normalizing_flows import Flow | ||
from normalizing_flows.bijections import RealNVP | ||
|
||
torch.manual_seed(0) | ||
|
||
# We support arbitrary event and batch shapes | ||
event_shape = (2, 3) | ||
batch_shape = (5, 7) | ||
x_train = torch.randn(size=(*batch_shape, *event_shape)) | ||
|
||
bijection = RealNVP(event_shape=event_shape) | ||
flow = Flow(bijection) | ||
|
||
flow.fit(x_train, show_progress=True) | ||
``` | ||
|
||
## Computing the log determinant of the Jacobian transformation given a Flow | ||
```python | ||
import torch | ||
from normalizing_flows import Flow | ||
from normalizing_flows.bijections import RealNVP | ||
|
||
torch.manual_seed(0) | ||
|
||
batch_shape = (5, 7) | ||
event_shape = (2, 3) | ||
x = torch.randn(size=(*batch_shape, *event_shape)) | ||
z = torch.randn(size=(*batch_shape, *event_shape)) | ||
|
||
bijection = RealNVP(event_shape=event_shape) | ||
flow = Flow(bijection) | ||
|
||
_, log_det_forward = flow.bijection.forward(x) | ||
# log_det_forward.shape == batch_shape | ||
|
||
_, log_det_inverse = flow.bijection.inverse(z) | ||
# log_det_inverse.shape == batch_shape | ||
``` |