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 [ |
required |
z_score_theta
|
Optional[str]
|
Whether to z-score parameters \(\theta\) before passing them into
the network, can take one of the following:
- |
'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 |
5
|
num_bins
|
int
|
Number of bins used for the splines in |
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 |
|
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 [ |
required |
z_score_theta
|
Optional[str]
|
Whether to z-score parameters \(\theta\) before passing them into
the network, can take one of the following:
- |
'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 |
5
|
num_bins
|
int
|
Number of bins used for the splines in |
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 |
|
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 [ |
required |
z_score_theta
|
Optional[str]
|
Whether to z-score parameters \(\theta\) before passing them into
the network, can take one of the following:
- |
'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 |
|
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:
- |
'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 |
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 |
|
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:
- |
'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 |
|
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 |
|
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 |
|
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
|
required |
condition
|
Tensor
|
Conditions of shape
|
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 |
|
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
|
required |
condition
|
Tensor
|
Conditions of shape |
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 |
|
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 |
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 |
|
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 |
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 |
|