bayesflow.helper_functions module#
- bayesflow.helper_functions.check_tensor_sanity(tensor, logger)[source]#
Tests for the presence of NaNs and Infs in a tensor.
- bayesflow.helper_functions.merge_left_into_right(left_dict, right_dict)[source]#
Function to merge nested dict left_dict into nested dict right_dict.
- bayesflow.helper_functions.build_meta_dict(user_dict: dict, default_setting: MetaDictSetting) dict [source]#
Integrates a user-defined dictionary into a default dictionary.
Takes a user-defined dictionary and a default dictionary.
Scan the user_dict for violations by unspecified mandatory fields.
Merge user_dict entries into the default_dict. Considers nested dict structure.
- Parameters:
- user_dictdict
The user’s dictionary
- default_settingMetaDictSetting
The specified default setting with attributes:
meta_dict: dictionary with default values.
mandatory_fields: list(str) keys that need to be specified by the user_dict
- Returns:
- merged_dict: dict
Merged dictionary.
- bayesflow.helper_functions.extract_current_lr(optimizer)[source]#
Extracts current learning rate from optimizer.
- Parameters:
- optimizerinstance of subclass of tf.keras.optimizers.Optimizer
Optimizer to extract the learning rate from
- Returns:
- current_lrnp.float or NoneType
Current learning rate, or None if it can’t be determined
- bayesflow.helper_functions.format_loss_string(ep, it, loss, avg_dict, slope=None, lr=None, ep_str='Epoch', it_str='Iter', scalar_loss_str='Loss')[source]#
Prepare loss string for displaying on progress bar.
- bayesflow.helper_functions.loss_to_string(ep, loss, ep_str='Epoch', scalar_loss_str='Loss')[source]#
Converts output from an amortizer into a string. For instance, if a
dict
is provided, it will be converted as, e.g.,: dictionary = {k1: v1, k2: v2} -> ‘k1: v1, k2: v2’
- bayesflow.helper_functions.backprop_step(input_dict, amortizer, optimizer, **kwargs)[source]#
Computes the loss of the provided amortizer given an input dictionary and applies gradients.
- Parameters:
- input_dictdict
The configured output of the generative model
- amortizertf.keras.Model
The custom amortizer. Needs to implement a compute_loss method.
- optimizertf.keras.optimizers.Optimizer
The optimizer used to update the amortizer’s parameters.
- **kwargsdict
Optional keyword arguments passed to the network’s compute_loss method
- Returns:
- lossdict
The outputs of the compute_loss() method of the amortizer comprising all loss components, such as divergences or regularization.
- bayesflow.helper_functions.check_posterior_prior_shapes(post_samples, prior_samples)[source]#
Checks requirements for the shapes of posterior and prior draws as necessitated by most diagnostic functions.
- Parameters:
- post_samplesnp.ndarray of shape (n_data_sets, n_post_draws, n_params)
The posterior draws obtained from n_data_sets
- prior_samplesnp.ndarray of shape (n_data_sets, n_params)
The prior draws obtained for generating n_data_sets
- Raises:
- ShapeError
If there is a deviation form the expected shapes of post_samples and prior_samples.