Skip to content

Neural networks

posterior_nn(model, z_score_theta='independent', z_score_x='independent', hidden_features=50, num_transforms=5, num_bins=10, embedding_net=nn.Identity(), num_components=10, **kwargs)

Returns a function that builds a density estimator for learning the posterior.

This function will usually be used for SNPE. The returned function is to be passed to the inference class when using the flexible interface.

Parameters:

Name Type Description Default
model str

The type of density estimator that will be created. One of [mdn, made, maf, maf_rqs, nsf].

required
z_score_theta Optional[str]

Whether to z-score parameters \(\theta\) before passing them into the network, can take one of the following: - none, or None: do not z-score. - independent: z-score each dimension independently. - structured: treat dimensions as related, therefore compute mean and std over the entire batch, instead of per-dimension. Should be used when each sample is, for example, a time series or an image.

'independent'
z_score_x Optional[str]

Whether to z-score simulation outputs \(x\) before passing them into the network, same options as z_score_theta.

'independent'
hidden_features int

Number of hidden features.

50
num_transforms int

Number of transforms when a flow is used. Only relevant if density estimator is a normalizing flow (i.e. currently either a maf or a nsf). Ignored if density estimator is a mdn or made.

5
num_bins int

Number of bins used for the splines in nsf. Ignored if density estimator not nsf.

10
embedding_net Module

Optional embedding network for simulation outputs \(x\). This embedding net allows to learn features from potentially high-dimensional simulation outputs.

Identity()
num_components int

Number of mixture components for a mixture of Gaussians. Ignored if density estimator is not an mdn.

10
kwargs Any

additional custom arguments passed to downstream build functions.

{}
Source code in sbi/neural_nets/factory.py
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
def posterior_nn(
    model: str,
    z_score_theta: Optional[str] = "independent",
    z_score_x: Optional[str] = "independent",
    hidden_features: int = 50,
    num_transforms: int = 5,
    num_bins: int = 10,
    embedding_net: nn.Module = nn.Identity(),
    num_components: int = 10,
    **kwargs: Any,
) -> Callable:
    r"""
    Returns a function that builds a density estimator for learning the posterior.

    This function will usually be used for SNPE. The returned function is to be passed
    to the inference class when using the flexible interface.

    Args:
        model: The type of density estimator that will be created. One of [`mdn`,
            `made`, `maf`, `maf_rqs`, `nsf`].
        z_score_theta: Whether to z-score parameters $\theta$ before passing them into
            the network, can take one of the following:
            - `none`, or None: do not z-score.
            - `independent`: z-score each dimension independently.
            - `structured`: treat dimensions as related, therefore compute mean and std
            over the entire batch, instead of per-dimension. Should be used when each
            sample is, for example, a time series or an image.
        z_score_x: Whether to z-score simulation outputs $x$ before passing them into
            the network, same options as z_score_theta.
        hidden_features: Number of hidden features.
        num_transforms: Number of transforms when a flow is used. Only relevant if
            density estimator is a normalizing flow (i.e. currently either a `maf` or a
            `nsf`). Ignored if density estimator is a `mdn` or `made`.
        num_bins: Number of bins used for the splines in `nsf`. Ignored if density
            estimator not `nsf`.
        embedding_net: Optional embedding network for simulation outputs $x$. This
            embedding net allows to learn features from potentially high-dimensional
            simulation outputs.
        num_components: Number of mixture components for a mixture of Gaussians.
            Ignored if density estimator is not an mdn.
        kwargs: additional custom arguments passed to downstream build functions.
    """

    kwargs = dict(
        zip(
            (
                "z_score_x",
                "z_score_y",
                "hidden_features",
                "num_transforms",
                "num_bins",
                "embedding_net",
                "num_components",
            ),
            (
                z_score_theta,
                z_score_x,
                hidden_features,
                num_transforms,
                num_bins,
                check_net_device(embedding_net, "cpu", embedding_net_warn_msg),
                num_components,
            ),
        ),
        **kwargs,
    )

    def build_fn_snpe_a(batch_theta, batch_x, num_components):
        """Build function for SNPE-A

        Extract the number of components from the kwargs, such that they are exposed as
        a kwargs, offering the possibility to later override this kwarg with
        `functools.partial`. This is necessary in order to make sure that the MDN in
        SNPE-A only has one component when running the Algorithm 1 part.
        """
        return build_mdn(
            batch_x=batch_theta,
            batch_y=batch_x,
            num_components=num_components,
            **kwargs,
        )

    def build_fn(batch_theta, batch_x):
        if model not in model_builders:
            raise NotImplementedError(f"Model {model} in not implemented")

        # The naming might be a bit confusing.
        # batch_x are the latent variables, batch_y the conditioned variables.
        # batch_theta are the parameters and batch_x the observable variables.
        return model_builders[model](batch_x=batch_theta, batch_y=batch_x, **kwargs)

    if model == "mdn_snpe_a":
        if num_components != 10:
            raise ValueError(
                "You set `num_components`. For SNPE-A, this has to be done at "
                "instantiation of the inference object, i.e. "
                "`inference = SNPE_A(..., num_components=20)`"
            )
        kwargs.pop("num_components")

    return build_fn_snpe_a if model == "mdn_snpe_a" else build_fn

likelihood_nn(model, z_score_theta='independent', z_score_x='independent', hidden_features=50, num_transforms=5, num_bins=10, embedding_net=nn.Identity(), num_components=10, **kwargs)

Returns a function that builds a density estimator for learning the likelihood.

This function will usually be used for SNLE. The returned function is to be passed to the inference class when using the flexible interface.

Parameters:

Name Type Description Default
model str

The type of density estimator that will be created. One of [mdn, made, maf, maf_rqs, nsf].

required
z_score_theta Optional[str]

Whether to z-score parameters \(\theta\) before passing them into the network, can take one of the following: - none, or None: do not z-score. - independent: z-score each dimension independently. - structured: treat dimensions as related, therefore compute mean and std over the entire batch, instead of per-dimension. Should be used when each sample is, for example, a time series or an image.

'independent'
z_score_x Optional[str]

Whether to z-score simulation outputs \(x\) before passing them into the network, same options as z_score_theta.

'independent'
hidden_features int

Number of hidden features.

50
num_transforms int

Number of transforms when a flow is used. Only relevant if density estimator is a normalizing flow (i.e. currently either a maf or a nsf). Ignored if density estimator is a mdn or made.

5
num_bins int

Number of bins used for the splines in nsf. Ignored if density estimator not nsf.

10
embedding_net Module

Optional embedding network for parameters \(\theta\).

Identity()
num_components int

Number of mixture components for a mixture of Gaussians. Ignored if density estimator is not an mdn.

10
kwargs Any

additional custom arguments passed to downstream build functions.

{}
Source code in sbi/neural_nets/factory.py
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
def likelihood_nn(
    model: str,
    z_score_theta: Optional[str] = "independent",
    z_score_x: Optional[str] = "independent",
    hidden_features: int = 50,
    num_transforms: int = 5,
    num_bins: int = 10,
    embedding_net: nn.Module = nn.Identity(),
    num_components: int = 10,
    **kwargs: Any,
) -> Callable:
    r"""
    Returns a function that builds a density estimator for learning the likelihood.

    This function will usually be used for SNLE. The returned function is to be passed
    to the inference class when using the flexible interface.

    Args:
        model: The type of density estimator that will be created. One of [`mdn`,
            `made`, `maf`, `maf_rqs`, `nsf`].
        z_score_theta: Whether to z-score parameters $\theta$ before passing them into
            the network, can take one of the following:
            - `none`, or None: do not z-score.
            - `independent`: z-score each dimension independently.
            - `structured`: treat dimensions as related, therefore compute mean and std
            over the entire batch, instead of per-dimension. Should be used when each
            sample is, for example, a time series or an image.
        z_score_x: Whether to z-score simulation outputs $x$ before passing them into
            the network, same options as z_score_theta.
        hidden_features: Number of hidden features.
        num_transforms: Number of transforms when a flow is used. Only relevant if
            density estimator is a normalizing flow (i.e. currently either a `maf` or a
            `nsf`). Ignored if density estimator is a `mdn` or `made`.
        num_bins: Number of bins used for the splines in `nsf`. Ignored if density
            estimator not `nsf`.
        embedding_net: Optional embedding network for parameters $\theta$.
        num_components: Number of mixture components for a mixture of Gaussians.
            Ignored if density estimator is not an mdn.
        kwargs: additional custom arguments passed to downstream build functions.
    """

    kwargs = dict(
        zip(
            (
                "z_score_x",
                "z_score_y",
                "hidden_features",
                "num_transforms",
                "num_bins",
                "embedding_net",
                "num_components",
            ),
            (
                z_score_x,
                z_score_theta,
                hidden_features,
                num_transforms,
                num_bins,
                check_net_device(embedding_net, "cpu", embedding_net_warn_msg),
                num_components,
            ),
        ),
        **kwargs,
    )

    def build_fn(batch_theta, batch_x):
        if model not in model_builders:
            raise NotImplementedError(f"Model {model} in not implemented")

        return model_builders[model](batch_x=batch_x, batch_y=batch_theta, **kwargs)

    return build_fn

classifier_nn(model, z_score_theta='independent', z_score_x='independent', hidden_features=50, embedding_net_theta=nn.Identity(), embedding_net_x=nn.Identity(), **kwargs)

Returns a function that builds a classifier for learning density ratios.

This function will usually be used for SNRE. The returned function is to be passed to the inference class when using the flexible interface.

Note that in the view of the SNRE classifier we build below, x=theta and y=x.

Parameters:

Name Type Description Default
model str

The type of classifier that will be created. One of [linear, mlp, resnet].

required
z_score_theta Optional[str]

Whether to z-score parameters \(\theta\) before passing them into the network, can take one of the following: - none, or None: do not z-score. - independent: z-score each dimension independently. - structured: treat dimensions as related, therefore compute mean and std over the entire batch, instead of per-dimension. Should be used when each sample is, for example, a time series or an image.

'independent'
z_score_x Optional[str]

Whether to z-score simulation outputs \(x\) before passing them into the network, same options as z_score_theta.

'independent'
hidden_features int

Number of hidden features.

50
embedding_net_theta Module

Optional embedding network for parameters \(\theta\).

Identity()
embedding_net_x Module

Optional embedding network for simulation outputs \(x\). This embedding net allows to learn features from potentially high-dimensional simulation outputs.

Identity()
kwargs Any

additional custom arguments passed to downstream build functions.

{}
Source code in sbi/neural_nets/factory.py
 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
def classifier_nn(
    model: str,
    z_score_theta: Optional[str] = "independent",
    z_score_x: Optional[str] = "independent",
    hidden_features: int = 50,
    embedding_net_theta: nn.Module = nn.Identity(),
    embedding_net_x: nn.Module = nn.Identity(),
    **kwargs: Any,
) -> Callable:
    r"""
    Returns a function that builds a classifier for learning density ratios.

    This function will usually be used for SNRE. The returned function is to be passed
    to the inference class when using the flexible interface.

    Note that in the view of the SNRE classifier we build below, x=theta and y=x.

    Args:
        model: The type of classifier that will be created. One of [`linear`, `mlp`,
            `resnet`].
        z_score_theta: Whether to z-score parameters $\theta$ before passing them into
            the network, can take one of the following:
            - `none`, or None: do not z-score.
            - `independent`: z-score each dimension independently.
            - `structured`: treat dimensions as related, therefore compute mean and std
            over the entire batch, instead of per-dimension. Should be used when each
            sample is, for example, a time series or an image.
        z_score_x: Whether to z-score simulation outputs $x$ before passing them into
            the network, same options as z_score_theta.
        hidden_features: Number of hidden features.
        embedding_net_theta:  Optional embedding network for parameters $\theta$.
        embedding_net_x:  Optional embedding network for simulation outputs $x$. This
            embedding net allows to learn features from potentially high-dimensional
            simulation outputs.
        kwargs: additional custom arguments passed to downstream build functions.
    """

    kwargs = dict(
        zip(
            (
                "z_score_x",
                "z_score_y",
                "hidden_features",
                "embedding_net_x",
                "embedding_net_y",
            ),
            (
                z_score_theta,
                z_score_x,
                hidden_features,
                check_net_device(embedding_net_theta, "cpu", embedding_net_warn_msg),
                check_net_device(embedding_net_x, "cpu", embedding_net_warn_msg),
            ),
        ),
        **kwargs,
    )

    def build_fn(batch_theta, batch_x):
        if model == "linear":
            return build_linear_classifier(
                batch_x=batch_theta, batch_y=batch_x, **kwargs
            )
        if model == "mlp":
            return build_mlp_classifier(batch_x=batch_theta, batch_y=batch_x, **kwargs)
        if model == "resnet":
            return build_resnet_classifier(
                batch_x=batch_theta, batch_y=batch_x, **kwargs
            )
        else:
            raise NotImplementedError

    return build_fn

flowmatching_nn(model, z_score_theta='independent', z_score_x='independent', hidden_features=64, num_layers=5, num_blocks=5, num_frequencies=3, embedding_net=nn.Identity(), **kwargs)

Returns a function that builds a neural net that can act as a vector field estimator for Flow Matching. This function will usually be used for Flow Matching. The returned function is to be passed to the

Parameters:

Name Type Description Default
model str

the type of regression network to learn the vector field. One of [‘mlp’, ‘resnet’].

required
z_score_theta Optional[str]

Whether to z-score parameters \(\theta\) before passing them into the network, can take one of the following: - none, or None: do not z-score. - independent: z-score each dimension independently. - structured: treat dimensions as related, therefore compute mean and std over the entire batch, instead of per-dimension. Should be used when each sample is, for example, a time series or an image.

'independent'
z_score_x Optional[str]

Whether to z-score simulation outputs \(x\) before passing them into the network, same options as z_score_theta.

'independent'
hidden_features int

Number of hidden features.

64
num_layers int

Number of transforms when a flow is used. Only relevant if density estimator is a normalizing flow (i.e. currently either a maf or a nsf). Ignored if density estimator is a mdn or made.

5
num_blocks int

Number of blocks if a ResNet is used.

5
num_frequencies int

Number of frequencies for the time embedding.

3
embedding_net Module

Optional embedding network for the condition.

Identity()
kwargs Any

additional custom arguments passed to downstream build functions.

{}
Source code in sbi/neural_nets/factory.py
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
def flowmatching_nn(
    model: str,
    z_score_theta: Optional[str] = "independent",
    z_score_x: Optional[str] = "independent",
    hidden_features: int = 64,
    num_layers: int = 5,
    num_blocks: int = 5,
    num_frequencies: int = 3,
    embedding_net: nn.Module = nn.Identity(),
    **kwargs: Any,
) -> Callable:
    r"""Returns a function that builds a neural net that can act as
    a vector field estimator for Flow Matching. This function will usually
    be used for Flow Matching. The returned function is to be passed to the

    Args:
        model: the type of regression network to learn the vector field. One of ['mlp',
            'resnet'].
        z_score_theta: Whether to z-score parameters $\theta$ before passing them into
            the network, can take one of the following:
            - `none`, or None: do not z-score.
            - `independent`: z-score each dimension independently.
            - `structured`: treat dimensions as related, therefore compute mean and std
            over the entire batch, instead of per-dimension. Should be used when each
            sample is, for example, a time series or an image.
        z_score_x: Whether to z-score simulation outputs $x$ before passing them into
            the network, same options as z_score_theta.
        hidden_features: Number of hidden features.
        num_layers: Number of transforms when a flow is used. Only relevant if
            density estimator is a normalizing flow (i.e. currently either a `maf` or a
            `nsf`). Ignored if density estimator is a `mdn` or `made`.
        num_blocks: Number of blocks if a ResNet is used.
        num_frequencies: Number of frequencies for the time embedding.
        embedding_net: Optional embedding network for the condition.
        kwargs: additional custom arguments passed to downstream build functions.
    """
    implemented_models = ["mlp", "resnet"]

    if model not in implemented_models:
        raise NotImplementedError(f"Model {model} in not implemented for FMPE")

    model_str = model + "_flowmatcher"

    def build_fn(batch_theta, batch_x):
        return model_builders[model_str](
            batch_x=batch_theta,
            batch_y=batch_x,
            z_score_x=z_score_theta,
            z_score_y=z_score_x,
            hidden_features=hidden_features,
            num_layers=num_layers,
            num_blocks=num_blocks,
            num_freqs=num_frequencies,
            embedding_net=check_net_device(
                embedding_net, "cpu", embedding_net_warn_msg
            ),
            **kwargs,
        )

    return build_fn

posterior_score_nn(sde_type, score_net_type='mlp', z_score_theta='independent', z_score_x='independent', t_embedding_dim=16, hidden_features=50, embedding_net=nn.Identity(), **kwargs)

Build util function that builds a ScoreEstimator object for score-based posteriors.

Parameters:

Name Type Description Default
sde_type str

SDE type used, which defines the mean and std functions. One of: - ‘vp’: Variance preserving. - ‘subvp’: Sub-variance preserving. - ‘ve’: Variance exploding. Defaults to ‘vp’.

required
score_net

Type of regression network. One of: - ‘mlp’: Fully connected feed-forward network. - ‘resnet’: Residual network (NOT IMPLEMENTED). - nn.Module: Custom network Defaults to ‘mlp’.

required
z_score_theta Optional[str]

Whether to z-score thetas passing into the network, can be one of: - none, or None: do not z-score. - independent: z-score each dimension independently. - structured: treat dimensions as related, therefore compute mean and std over the entire batch, instead of per-dimension. Should be used when each sample is, for example, a time series or an image.

'independent'
z_score_x Optional[str]

Whether to z-score xs passing into the network, same options as z_score_theta.

'independent'
t_embedding_dim int

Embedding dimension of diffusion time. Defaults to 16.

16
hidden_features int

Number of hidden units per layer. Defaults to 50.

50
embedding_net Module

Embedding network for x (conditioning variable). Defaults to nn.Identity().

Identity()

Returns:

Type Description
Callable

Constructor function for NPSE.

Source code in sbi/neural_nets/factory.py
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
def posterior_score_nn(
    sde_type: str,
    score_net_type: Union[str, nn.Module] = "mlp",
    z_score_theta: Optional[str] = "independent",
    z_score_x: Optional[str] = "independent",
    t_embedding_dim: int = 16,
    hidden_features: int = 50,
    embedding_net: nn.Module = nn.Identity(),
    **kwargs: Any,
) -> Callable:
    """Build util function that builds a ScoreEstimator object for score-based
    posteriors.

    Args:
        sde_type: SDE type used, which defines the mean and std functions. One of:
            - 'vp': Variance preserving.
            - 'subvp': Sub-variance preserving.
            - 've': Variance exploding.
            Defaults to 'vp'.
        score_net: Type of regression network. One of:
            - 'mlp': Fully connected feed-forward network.
            - 'resnet': Residual network (NOT IMPLEMENTED).
            -  nn.Module: Custom network
            Defaults to 'mlp'.
        z_score_theta: Whether to z-score thetas passing into the network, can be one
            of:
            - `none`, or None: do not z-score.
            - `independent`: z-score each dimension independently.
            - `structured`: treat dimensions as related, therefore compute mean and std
            over the entire batch, instead of per-dimension. Should be used when each
            sample is, for example, a time series or an image.
        z_score_x: Whether to z-score xs passing into the network, same options as
            z_score_theta.
        t_embedding_dim: Embedding dimension of diffusion time. Defaults to 16.
        hidden_features: Number of hidden units per layer. Defaults to 50.
        embedding_net: Embedding network for x (conditioning variable). Defaults to
            nn.Identity().

    Returns:
        Constructor function for NPSE.
    """

    kwargs = dict(
        zip(
            (
                "z_score_x",
                "z_score_y",
                "sde_type",
                "score_net",
                "t_embedding_dim",
                "hidden_features",
                "embedding_net_y",
            ),
            (
                z_score_theta,
                z_score_x,
                sde_type,
                score_net_type,
                t_embedding_dim,
                hidden_features,
                embedding_net,
            ),
        ),
        **kwargs,
    )

    def build_fn(batch_theta, batch_x):
        """Build function wrapper for the build_score_estimator function that
        is required for the score posterior class.

        Args:
            batch_theta: a batch of theta.
            batch_x: a batch of x.

        Returns:
            Callable: a ScoreEstimator object.
        """
        return build_score_estimator(batch_x=batch_theta, batch_y=batch_x, **kwargs)

    return build_fn

ConditionalDensityEstimator

Bases: ConditionalEstimator

Base class for density estimators.

The density estimator class is a wrapper around neural networks that allows to evaluate the log_prob, sample, and provide the loss of \(\theta,x\) pairs. Here \(\theta\) would be the input and \(x\) would be the condition.

Note

We assume that the input to the density estimator is a tensor of shape (sample_dim, batch_dim, *input_shape), where input_shape is the dimensionality of the input. The condition is a tensor of shape (batch_size, *condition_shape), where condition_shape is the shape of the condition tensor.

Source code in sbi/neural_nets/estimators/base.py
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
class ConditionalDensityEstimator(ConditionalEstimator):
    r"""Base class for density estimators.

    The density estimator class is a wrapper around neural networks that allows to
    evaluate the `log_prob`, `sample`, and provide the `loss` of $\theta,x$ pairs. Here
    $\theta$ would be the `input` and $x$ would be the `condition`.

    Note:
        We assume that the input to the density estimator is a tensor of shape
        (sample_dim, batch_dim, *input_shape), where input_shape is the dimensionality
        of the input. The condition is a tensor of shape (batch_size, *condition_shape),
        where condition_shape is the shape of the condition tensor.

    """

    def __init__(
        self, net: nn.Module, input_shape: torch.Size, condition_shape: torch.Size
    ) -> None:
        r"""Base class for density estimators.

        Args:
            net: Neural network or any parameterized model that is used to estimate the
                probability density of the input given a condition.
            input_shape: Event shape of the input at which the density is being
                evaluated (and which is also the event_shape of samples).
            condition_shape: Shape of the condition.
        """
        super().__init__(input_shape, condition_shape)
        self.net = net

    @property
    def embedding_net(self) -> Optional[nn.Module]:
        r"""Return the embedding network if it exists."""
        return None

    @abstractmethod
    def log_prob(self, input: Tensor, condition: Tensor, **kwargs) -> Tensor:
        r"""Return the log probabilities of the inputs given a condition or multiple
        i.e. batched conditions.

        Args:
            input: Inputs to evaluate the log probability on of shape
                    `(sample_dim_input, batch_dim_input, *event_shape_input)`.
            condition: Conditions of shape
                `(batch_dim_condition, *event_shape_condition)`.

        Raises:
            RuntimeError: If batch_dim_input and batch_dim_condition do not match.

        Returns:
            Sample-wise log probabilities.
        """

        pass

    @abstractmethod
    def loss(self, input: Tensor, condition: Tensor, **kwargs) -> Tensor:
        r"""Return the loss for training the density estimator.

        Args:
            input: Inputs to evaluate the loss on of shape
                `(batch_dim, *input_event_shape)`.
            condition: Conditions of shape `(batch_dim, *event_shape_condition)`.

        Returns:
            Loss of shape (batch_dim,)
        """

        pass

    @abstractmethod
    def sample(self, sample_shape: torch.Size, condition: Tensor, **kwargs) -> Tensor:
        r"""Return samples from the density estimator.

        Args:
            sample_shape: Shape of the samples to return.
            condition: Conditions of shape `(batch_dim, *event_shape_condition)`.

        Returns:
            Samples of shape (*sample_shape, batch_dim, *event_shape_input).
        """

        pass

    def sample_and_log_prob(
        self, sample_shape: torch.Size, condition: Tensor, **kwargs
    ) -> Tuple[Tensor, Tensor]:
        r"""Return samples and their density from the density estimator.

        Args:
            sample_shape: Shape of the samples to return.
            condition: Conditions of shape `(batch_dim, *event_shape_condition)`.

        Returns:
            Samples and associated log probabilities.

        Note:
            For some density estimators, computing log_probs for samples is
            more efficient than computing them separately. This method should
            then be overwritten to provide a more efficient implementation.
        """

        samples = self.sample(sample_shape, condition, **kwargs)
        log_probs = self.log_prob(samples, condition, **kwargs)
        return samples, log_probs

embedding_net: Optional[nn.Module] property

Return the embedding network if it exists.

__init__(net, input_shape, condition_shape)

Base class for density estimators.

Parameters:

Name Type Description Default
net Module

Neural network or any parameterized model that is used to estimate the probability density of the input given a condition.

required
input_shape Size

Event shape of the input at which the density is being evaluated (and which is also the event_shape of samples).

required
condition_shape Size

Shape of the condition.

required
Source code in sbi/neural_nets/estimators/base.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def __init__(
    self, net: nn.Module, input_shape: torch.Size, condition_shape: torch.Size
) -> None:
    r"""Base class for density estimators.

    Args:
        net: Neural network or any parameterized model that is used to estimate the
            probability density of the input given a condition.
        input_shape: Event shape of the input at which the density is being
            evaluated (and which is also the event_shape of samples).
        condition_shape: Shape of the condition.
    """
    super().__init__(input_shape, condition_shape)
    self.net = net

log_prob(input, condition, **kwargs) abstractmethod

Return the log probabilities of the inputs given a condition or multiple i.e. batched conditions.

Parameters:

Name Type Description Default
input Tensor

Inputs to evaluate the log probability on of shape (sample_dim_input, batch_dim_input, *event_shape_input).

required
condition Tensor

Conditions of shape (batch_dim_condition, *event_shape_condition).

required

Raises:

Type Description
RuntimeError

If batch_dim_input and batch_dim_condition do not match.

Returns:

Type Description
Tensor

Sample-wise log probabilities.

Source code in sbi/neural_nets/estimators/base.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
@abstractmethod
def log_prob(self, input: Tensor, condition: Tensor, **kwargs) -> Tensor:
    r"""Return the log probabilities of the inputs given a condition or multiple
    i.e. batched conditions.

    Args:
        input: Inputs to evaluate the log probability on of shape
                `(sample_dim_input, batch_dim_input, *event_shape_input)`.
        condition: Conditions of shape
            `(batch_dim_condition, *event_shape_condition)`.

    Raises:
        RuntimeError: If batch_dim_input and batch_dim_condition do not match.

    Returns:
        Sample-wise log probabilities.
    """

    pass

loss(input, condition, **kwargs) abstractmethod

Return the loss for training the density estimator.

Parameters:

Name Type Description Default
input Tensor

Inputs to evaluate the loss on of shape (batch_dim, *input_event_shape).

required
condition Tensor

Conditions of shape (batch_dim, *event_shape_condition).

required

Returns:

Type Description
Tensor

Loss of shape (batch_dim,)

Source code in sbi/neural_nets/estimators/base.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
@abstractmethod
def loss(self, input: Tensor, condition: Tensor, **kwargs) -> Tensor:
    r"""Return the loss for training the density estimator.

    Args:
        input: Inputs to evaluate the loss on of shape
            `(batch_dim, *input_event_shape)`.
        condition: Conditions of shape `(batch_dim, *event_shape_condition)`.

    Returns:
        Loss of shape (batch_dim,)
    """

    pass

sample(sample_shape, condition, **kwargs) abstractmethod

Return samples from the density estimator.

Parameters:

Name Type Description Default
sample_shape Size

Shape of the samples to return.

required
condition Tensor

Conditions of shape (batch_dim, *event_shape_condition).

required

Returns:

Type Description
Tensor

Samples of shape (*sample_shape, batch_dim, *event_shape_input).

Source code in sbi/neural_nets/estimators/base.py
189
190
191
192
193
194
195
196
197
198
199
200
201
@abstractmethod
def sample(self, sample_shape: torch.Size, condition: Tensor, **kwargs) -> Tensor:
    r"""Return samples from the density estimator.

    Args:
        sample_shape: Shape of the samples to return.
        condition: Conditions of shape `(batch_dim, *event_shape_condition)`.

    Returns:
        Samples of shape (*sample_shape, batch_dim, *event_shape_input).
    """

    pass

sample_and_log_prob(sample_shape, condition, **kwargs)

Return samples and their density from the density estimator.

Parameters:

Name Type Description Default
sample_shape Size

Shape of the samples to return.

required
condition Tensor

Conditions of shape (batch_dim, *event_shape_condition).

required

Returns:

Type Description
Tuple[Tensor, Tensor]

Samples and associated log probabilities.

Note

For some density estimators, computing log_probs for samples is more efficient than computing them separately. This method should then be overwritten to provide a more efficient implementation.

Source code in sbi/neural_nets/estimators/base.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def sample_and_log_prob(
    self, sample_shape: torch.Size, condition: Tensor, **kwargs
) -> Tuple[Tensor, Tensor]:
    r"""Return samples and their density from the density estimator.

    Args:
        sample_shape: Shape of the samples to return.
        condition: Conditions of shape `(batch_dim, *event_shape_condition)`.

    Returns:
        Samples and associated log probabilities.

    Note:
        For some density estimators, computing log_probs for samples is
        more efficient than computing them separately. This method should
        then be overwritten to provide a more efficient implementation.
    """

    samples = self.sample(sample_shape, condition, **kwargs)
    log_probs = self.log_prob(samples, condition, **kwargs)
    return samples, log_probs