core.samplers.Sampler
core.samplers.Sampler(model)Sampler class that wraps a PosteriorModel. Allows for conditional and unconditional models.
Draws samples from the model based on (optional) context data.
This is intended for use either as a standalone sampler, or as a sampler producing initial sample points for a GNPE sampler.
Attributes
| Name | Type | Description |
|---|---|---|
| model | BasePosteriorModel | |
| inference_parameters | list | |
| samples | DataFrame | Samples produced from the model by run_sampler(). |
| context | dict | |
| metadata | dict | |
| event_metadata | dict | |
| unconditional_model | bool | Whether the model is unconditional, in which case it is not provided context information. |
| transform_pre, transform_post | Transform | Transforms to be applied to data and parameters during inference. These are typically implemented in a subclass. |
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | BasePosteriorModel | required |
Methods
| Name | Description |
|---|---|
| log_prob | Calculate the model log probability at specific sample points. |
| run_sampler | Generates samples and stores them in self.samples. Conditions the model on |
| to_hdf5 | |
| to_result | Export samples, metadata, and context information to a Result instance, |
log_prob
core.samplers.Sampler.log_prob(samples)Calculate the model log probability at specific sample points.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| samples | pd.DataFrame | dict | Sample points at which to calculate the log probability. | required |
Returns
| Name | Type | Description |
|---|---|---|
| np.array of log probabilities. |
run_sampler
core.samplers.Sampler.run_sampler(num_samples, batch_size=None)Generates samples and stores them in self.samples. Conditions the model on self.context if appropriate (i.e., if the model is not unconditional).
If possible, it also calculates the log_prob and saves it as a column in self.samples. When using GNPE it is not possible to obtain the log_prob due to the many Gibbs iterations. However, in the case of just one iteration, and when starting from a sampler for the proxy, the GNPESampler does calculate the log_prob.
Allows for batched sampling, e.g., if limited by GPU memory. Actual sampling for each batch is performed by _run_sampler(), which will differ for Sampler and GNPESampler.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| num_samples | int | Number of samples requested. | required |
| batch_size | int | Batch size for sampler. | None |
to_hdf5
core.samplers.Sampler.to_hdf5(label='result', outdir='.')to_result
core.samplers.Sampler.to_result()Export samples, metadata, and context information to a Result instance, which can be used for saving or, e.g., importance sampling, training an unconditional flow, etc.
Returns
| Name | Type | Description |
|---|---|---|
| Result |