stochastic
Script containing the common basic blocks (nn.Module) reused by the LadderVAE architecture.
NormalStochasticBlock
#
Bases: Module
Stochastic block used in the Top-Down inference pass.
Algorithm: - map input parameters to q(z) and (optionally) p(z) via convolution - sample a latent tensor z ~ q(z) - feed z to convolution and return.
NOTE 1: If parameters for q are not given, sampling is done from p(z).
NOTE 2: The restricted KL divergence is obtained by first computing the element-wise KL divergence (i.e., the KL computed for each element of the latent tensors). Then, the restricted version is computed by summing over the channels and the spatial dimensions associated only to the portion of the latent tensor that is used for prediction.
Source code in src/careamics/models/lvae/stochastic.py
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 185 186 187 188 189 190 191 192 193 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 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 285 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
__init__(c_in, c_vars, c_out, conv_dims=2, kernel=3, transform_p_params=True, vanilla_latent_hw=None, use_naive_exponential=False)
#
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c_in | int | The number of channels of the input tensor. | required |
c_vars | int | The number of channels of the latent space tensor. | required |
c_out | int | The output of the stochastic layer. Note that this is different from the sampled latent z. | required |
conv_dims | int | The number of dimensions of the convolutional layers (2D or 3D). Default is 2. | 2 |
kernel | int | The size of the kernel used in convolutional layers. Default is 3. | 3 |
transform_p_params | bool | Whether a transformation should be applied to the | True |
vanilla_latent_hw | int | The shape of the latent tensor used for prediction (i.e., it influences the computation of restricted KL). Default is | None |
use_naive_exponential | bool | If | False |
Source code in src/careamics/models/lvae/stochastic.py
compute_kl_metrics(p, p_params, q, q_params, mode_pred, analytical_kl, z)
#
Compute KL (analytical or MC estimate) and then process it, extracting composed versions of the metric. Specifically, the different versions of the KL loss terms are: - kl_elementwise
: KL term for each single element of the latent tensor [Shape: (batch, ch, h, w)]. - kl_samplewise
: KL term associated to each sample in the batch [Shape: (batch, )]. - kl_samplewise_restricted
: KL term only associated to the portion of the latent tensor that is used for prediction and summed over channel and spatial dimensions [Shape: (batch, )]. - kl_channelwise
: KL term associated to each sample and each channel [Shape: (batch, ch, )]. - kl_spatial
: KL term summed over the channels, i.e., retaining the spatial dimensions [Shape: (batch, h, w)]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p | Normal | The prior generative distribution p(z_i|z_{i+1}) (or p(z_L)). | required |
p_params | Tensor | The parameters of the prior generative distribution. | required |
q | Normal | The inference distribution q(z_i|z_{i+1}) (or q(z_L|x)). | required |
q_params | Tensor | The parameters of the inference distribution. | required |
mode_pred | bool | Whether the model is in prediction mode. | required |
analytical_kl | bool | Whether to compute the KL divergence analytically or using Monte Carlo estimation. | required |
z | Tensor | The sampled latent tensor. | required |
Source code in src/careamics/models/lvae/stochastic.py
forward(p_params, q_params=None, forced_latent=None, force_constant_output=False, analytical_kl=False, mode_pred=False, use_uncond_mode=False, var_clip_max=None)
#
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_params | Tensor | The output tensor of the top-down layer above (i.e., mu_{p,i+1}, sigma_{p,i+1}). | required |
q_params | Union[Tensor, None] | The tensor resulting from merging the bu_value tensor at the same hierarchical level from the bottom-up pass and the | None |
forced_latent | Union[Tensor, None] | A pre-defined latent tensor. If it is not | None |
force_constant_output | bool | Whether to copy the first sample (and rel. distrib parameters) over the whole batch. This is used when doing experiment from the prior - q is not used. Default is | False |
analytical_kl | bool | Whether to compute the KL divergence analytically or using Monte Carlo estimation. Default is | False |
mode_pred | bool | Whether the model is in prediction mode. Default is | False |
use_uncond_mode | bool | Whether to use the uncoditional distribution p(z) to sample latents in prediction mode. Default is | False |
var_clip_max | Union[float, None] | The maximum value reachable by the log-variance of the latent distribution. Values exceeding this threshold are clipped. Default is | None |
Source code in src/careamics/models/lvae/stochastic.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
get_z(sampling_distrib, forced_latent, mode_pred, use_uncond_mode)
#
Sample a latent tensor from the given latent distribution.
Latent tensor can be obtained is several ways: - Sampled from the (Gaussian) latent distribution. - Taken as a pre-defined forced latent. - Taken as the mode (mean) of the latent distribution. - In prediction mode (mode_pred==True
), can be either sample or taken as the distribution mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sampling_distrib | Normal | The Gaussian distribution from which latent tensor is sampled. | required |
forced_latent | Union[Tensor, None] | A pre-defined latent tensor. If it is not | required |
mode_pred | bool | Whether the model is prediction mode. | required |
use_uncond_mode | bool | Whether to use the uncoditional distribution p(z) to sample latents in prediction mode. | required |
Source code in src/careamics/models/lvae/stochastic.py
process_p_params(p_params, var_clip_max)
#
Process the input parameters to get the prior distribution p(z_i|z_{i+1}) (or p(z_L)).
Processing consists in: - (optionally) 2D convolution on the input tensor to increase number of channels. - split the resulting tensor into two chunks, the mean and the log-variance. - (optionally) clip the log-variance to an upper threshold. - define the normal distribution p(z) given the parameter tensors above.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_params | Tensor | The input tensor to be processed. | required |
var_clip_max | float | The maximum value reachable by the log-variance of the latent distribution. Values exceeding this threshold are clipped. | required |
Source code in src/careamics/models/lvae/stochastic.py
process_q_params(q_params, var_clip_max, allow_oddsizes=False)
#
Process the input parameters to get the inference distribution q(z_i|z_{i+1}) (or q(z|x)).
Processing consists in: - convolution on the input tensor to double the number of channels. - split the resulting tensor into 2 chunks, respectively mean and log-var. - (optionally) clip the log-variance to an upper threshold. - (optionally) crop the resulting tensors to ensure that the last spatial dimension is even. - define the normal distribution q(z) given the parameter tensors above.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_params | The input tensor to be processed. | required | |
var_clip_max | float | The maximum value reachable by the log-variance of the latent distribution. Values exceeding this threshold are clipped. | required |
Source code in src/careamics/models/lvae/stochastic.py
sample_from_q(q_params, var_clip_max)
#
Given an input parameter tensor defining q(z), it processes it by calling process_q_params()
method and sample a latent tensor from the resulting distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q_params | Tensor | The input tensor to be processed. | required |
var_clip_max | float | The maximum value reachable by the log-variance of the latent distribution. Values exceeding this threshold are clipped. | required |