Variational Guides¶
Variational guide distributions for approximate inference.
guide
¶
Variational guide families for approximate posterior inference.
A guide is a parameterized distribution q(z | x) over latent variables that approximates the true posterior p(z | x, y_obs). Guides are used by SVI to optimize the ELBO.
This module provides:
Guide— abstract base class for all guidesAutoNormalGuide— mean-field Normal over all continuous latentsAutoDeltaGuide— point-estimate (MAP) guide
Guide
¶
Bases: Module, ABC
Abstract variational guide.
A guide provides a parameterized approximate posterior q(z | x) over latent variables. It must support reparameterized sampling and log-density evaluation.
latent_names
abstractmethod
property
¶
latent_names: list[str]
Names of latent variables this guide covers.
rsample
abstractmethod
¶
rsample(x: Tensor) -> dict[str, Tensor]
Sample latent variables from the guide.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input. Shape (batch, ...).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Sampled values for each latent variable. |
Source code in src/quivers/inference/guide.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
log_prob
abstractmethod
¶
log_prob(x: Tensor, sites: dict[str, Tensor]) -> Tensor
Log-density of latent values under the guide.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input. Shape (batch, ...).
TYPE:
|
sites
|
Values for each latent variable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Total log-density. Shape (batch,). |
Source code in src/quivers/inference/guide.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
AutoNormalGuide
¶
AutoNormalGuide(model: MonadicProgram, observed_names: set[str], init_scale: float = 0.1)
Bases: Guide
Mean-field Normal guide with learnable loc and scale per latent.
Inspects the model's step specs to discover latent (non-observed) sites and creates a pair of parameters (loc, log_scale) for each.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
The generative model to build a guide for.
TYPE:
|
observed_names
|
Names of observed variables (excluded from the guide).
TYPE:
|
init_scale
|
Initial scale for all latent sites.
TYPE:
|
Source code in src/quivers/inference/guide.py
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 | |
rsample
¶
rsample(x: Tensor) -> dict[str, Tensor]
Sample from mean-field Normal for each latent.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input (used for batch size).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Sampled latent values. |
Source code in src/quivers/inference/guide.py
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 185 186 187 188 189 190 191 192 | |
log_prob
¶
log_prob(x: Tensor, sites: dict[str, Tensor]) -> Tensor
Log-density under the mean-field Normal guide.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input (used for batch size).
TYPE:
|
sites
|
Latent variable values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Total log-density. Shape (batch,). |
Source code in src/quivers/inference/guide.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
AutoDeltaGuide
¶
AutoDeltaGuide(model: MonadicProgram, observed_names: set[str])
Bases: Guide
Point-estimate (MAP) guide with a learnable value per latent.
The guide distribution is a delta at the learned point, so log_prob returns 0 for all sites (the delta contribution cancels in the ELBO).
| PARAMETER | DESCRIPTION |
|---|---|
model
|
The generative model.
TYPE:
|
observed_names
|
Names of observed variables.
TYPE:
|
Source code in src/quivers/inference/guide.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
rsample
¶
rsample(x: Tensor) -> dict[str, Tensor]
Return the learned point estimates.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input (used for batch size).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Point-estimate values for each latent. |
Source code in src/quivers/inference/guide.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
log_prob
¶
log_prob(x: Tensor, sites: dict[str, Tensor]) -> Tensor
Log-density under the delta guide (always zero).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input.
TYPE:
|
sites
|
Latent variable values (ignored).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Zeros. Shape (batch,). |
Source code in src/quivers/inference/guide.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |