Base Flow¶
Abstract base class for all flow-based generative models.
base_flow
¶
Base Flow Abstract Class
Defines the interface for all normalizing flow types in FlowPDE.
BaseFlow
¶
Bases: ABC, Module
Abstract base class for all normalizing flows.
This defines the common interface for invertible flow objects. Training objectives such as flow matching or maximum likelihood live outside the flow.
Key Concepts: - Forward: Map from data distribution to base distribution (e.g., training) - Inverse: Map from base distribution to data distribution (e.g., sampling) - Log probability computation for density estimation
Source code in flowpde/core/base_flow.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
model_device
property
¶
Get the device of the model parameters.
__init__(model, base_distribution='gaussian', target_key='u', condition_key='f', **kwargs)
¶
Initialize base flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Neural network that parameterizes the flow |
required |
base_distribution
|
str
|
Type of base distribution ('gaussian', 'uniform', etc.) |
'gaussian'
|
target_key
|
str
|
Default batch key for target data |
'u'
|
condition_key
|
str
|
Default batch key for conditioning data |
'f'
|
**kwargs
|
Any
|
Additional flow-specific parameters |
{}
|
Source code in flowpde/core/base_flow.py
forward_transform(x, condition=None, **kwargs)
abstractmethod
¶
Transform from data space to latent space (forward direction).
This is typically used during training to map data to the base distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor from data distribution (batch_size, dim) |
required |
condition
|
Optional[Tensor]
|
Optional conditioning information (batch_size, cond_dim) |
None
|
**kwargs
|
Any
|
Additional arguments |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
z |
Union[Tensor, Tuple[Tensor, Tensor]]
|
Transformed tensor in latent space |
log_det |
optional
|
Log determinant of Jacobian for probability computation |
Source code in flowpde/core/base_flow.py
inverse_transform(z, condition=None, **kwargs)
abstractmethod
¶
Transform from latent space to data space (inverse/sampling direction).
This is typically used during inference to generate samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Tensor
|
Input tensor from base distribution (batch_size, dim) |
required |
condition
|
Optional[Tensor]
|
Optional conditioning information (batch_size, cond_dim) |
None
|
**kwargs
|
Any
|
Additional arguments |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
Union[Tensor, Tuple[Tensor, Tensor]]
|
Transformed tensor in data space |
log_det |
optional
|
Log determinant of Jacobian for probability computation |
Source code in flowpde/core/base_flow.py
sample(condition, **kwargs)
abstractmethod
¶
Generate samples from the flow, given conditioning information.
Every flow in this package is conditional -- it models p(x | c) for a PDE condition c -- so the condition is the first argument and the batch size comes from it. Subclasses define the rest of their sampling controls (solver, step count, initial noise).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
Tensor
|
Conditioning information (batch_size, cond_dim) |
required |
**kwargs
|
Any
|
Flow-specific sampling parameters |
{}
|
Returns:
| Type | Description |
|---|---|
Union[Tensor, Tuple[Tensor, Tensor]]
|
Generated samples |
Source code in flowpde/core/base_flow.py
log_prob(x, condition, **kwargs)
abstractmethod
¶
Compute log probability of data under the flow.
The base measure and how its density is evaluated are the subclass's business: they have to agree with whatever that subclass samples from, and a generic implementation here could only guess.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Data points (batch_size, dim) |
required |
condition
|
Tensor
|
Conditioning information |
required |
**kwargs
|
Any
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Log probabilities (batch_size,) |
Source code in flowpde/core/base_flow.py
get_config()
¶
Get configuration dictionary for the flow.