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
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 |
|
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
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 |
|
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
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 |
|
DensityEstimator
¶
Bases: Module
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 (batch_size, input_size), where input_size 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/density_estimators/base.py
7 8 9 10 11 12 13 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 |
|
embedding_net: Optional[nn.Module]
property
¶
Return the embedding network if it exists.
__init__(net, condition_shape)
¶
Base class for density estimators.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
Module
|
Neural network. |
required |
condition_shape |
Size
|
Shape of the condition. If not provided, it will assume a 1D input. |
required |
Source code in sbi/neural_nets/density_estimators/base.py
22 23 24 25 26 27 28 29 30 31 32 |
|
log_prob(input, condition, **kwargs)
¶
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 (*batch_shape1, input_size). |
required |
condition |
Tensor
|
Conditions of shape (*batch_shape2, *condition_shape). |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If batch_shape1 and batch_shape2 are not broadcastable. |
Returns:
Type | Description |
---|---|
Tensor
|
Sample-wise log probabilities. |
Note
This function should support PyTorch’s automatic broadcasting. This means the function should behave as follows for different input and condition shapes: - (input_size,) + (batch_size,*condition_shape) -> (batch_size,) - (batch_size, input_size) + (*condition_shape) -> (batch_size,) - (batch_size, input_size) + (batch_size, *condition_shape) -> (batch_size,) - (batch_size1, input_size) + (batch_size2, *condition_shape) -> RuntimeError i.e. not broadcastable - (batch_size1,1, input_size) + (batch_size2, *condition_shape) -> (batch_size1,batch_size2) - (batch_size1, input_size) + (batch_size2,1, *condition_shape) -> (batch_size2,batch_size1)
Source code in sbi/neural_nets/density_estimators/base.py
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 |
|
loss(input, condition, **kwargs)
¶
Return the loss for training the density estimator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
Tensor
|
Inputs to evaluate the loss on of shape (batch_size, input_size). |
required |
condition |
Tensor
|
Conditions of shape (batch_size, *condition_shape). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Loss of shape (batch_size,) |
Source code in sbi/neural_nets/density_estimators/base.py
71 72 73 74 75 76 77 78 79 80 81 82 |
|
sample(sample_shape, condition, **kwargs)
¶
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_shape, *condition_shape). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Samples of shape (*batch_shape, *sample_shape, input_size). |
Note
This function should support batched conditions and should admit the following behavior for different condition shapes: - (*condition_shape) -> (*sample_shape, input_size) - (*batch_shape, *condition_shape) -> (*batch_shape, *sample_shape, input_size)
Source code in sbi/neural_nets/density_estimators/base.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
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_shape, *condition_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/density_estimators/base.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|