NormalizingFlow#
- class deepinv.optim.prior.NormalizingFlow(dimension, num_layers, subnet, clamp=1.6)[source]#
Bases:
ModuleSequential normalizing flow built from GLOW-style affine coupling blocks.
The flow maps an input sample
xto a latent representationzby passing it throughnum_layersinvertible coupling blocks in sequence. The log-determinant of the full Jacobian is accumulated additively across blocks. Settingrev=Trueruns the blocks in reverse order to recover the original sample from a latent code.The architecture follows the generative flow of Kingma and Dhariwal[1], using
deepinv.optim.prior.GLOWCouplingBlockas the building block.- Parameters:
dimension (int) – dimension of each input sample (flattened patch size).
num_layers (int) – number of coupling blocks to stack.
subnet (Callable) – a callable
subnet(channels_in, channels_out) -> nn.Modulethat constructs the subnetworks used inside each coupling block.clamp (float) – soft-clamping magnitude passed to every coupling block. Default is
1.6.
- Examples:
>>> import torch >>> import torch.nn as nn >>> subnet = lambda c_in, c_out: nn.Sequential( ... nn.Linear(c_in, 32), nn.ReLU(), ... nn.Linear(32, 32), nn.ReLU(), ... nn.Linear(32, c_out), ... ) >>> flow = NormalizingFlow(dimension=8, num_layers=2, subnet=subnet) >>> x = torch.randn(4, 8) >>> z, log_det = flow(x) >>> z.shape torch.Size([4, 8]) >>> log_det.shape torch.Size([4]) >>> x_rec, _ = flow(z, rev=True) # inverse flow recovers the input >>> torch.allclose(x, x_rec, atol=1e-5) True
- References:
- forward(x, rev=False)[source]#
Passes the input through all coupling blocks sequentially.
- Parameters:
x (torch.Tensor) – input tensor of shape
(N, dimension).rev (bool) – if
True, applies the blocks in reverse order (inverse flow). Default isFalse.
- Returns:
tuple
(z, log_det)wherez(torch.Tensor) is the latent representation of shape(N, dimension)andlog_det(torch.Tensor) is the total log-determinant of the Jacobian of shape(N,).
Examples using NormalizingFlow:#
Patch priors for limited-angle computed tomography