FormulaEvaluator

class hybrid_learning.experimentation.fuzzy_exp.fuzzy_exp_helpers.FormulaEvaluator(conf=None, formulas=None, additional_formulas=None, predicates=None, formula_caches=None, predicates_cache=None, constants=None, changed_constants=None, changed_root=None, device=None)[source]

Bases: object

Evaluate predicates and formulas with intermediate values cached according to a config.

Public Data Attributes:

used_constants

The constants actually used in one of the formulas.

Public Methods:

restrict_to_used_constants()

Set constants to a dict only containing the used constants.

calc_results_for(images[, initial_masks, ...])

Calculate predicate and formula outputs for a batch of input images.

calc_result_for(image, initial_masks[, ...])

Wrapper around calc_results_for that handles single inputs instead of a batch.

Special Methods:

__init__([conf, formulas, ...])

Init.


Parameters
__init__(conf=None, formulas=None, additional_formulas=None, predicates=None, formula_caches=None, predicates_cache=None, constants=None, changed_constants=None, changed_root=None, device=None)[source]

Init. To disable caching, set conf['results_cache_dir']=None or assign caching.NoCache() to formula_caches and predicates_cache.

Parameters
calc_result_for(image, initial_masks, desc=None, only_formula_out=False)[source]

Wrapper around calc_results_for that handles single inputs instead of a batch.

Returns

output of calc_results_for() but with singular batch dimension (first dimension) squeezed for all tensors

Parameters
calc_results_for(images, initial_masks=None, descs=None, only_formula_out=False)[source]

Calculate predicate and formula outputs for a batch of input images.

Parameters
  • images (Tensor) – batch of input images of shape [batch, channels, height, width]

  • initial_masks (Optional[Dict[str, Tensor]]) – any further (pre-calculated) inputs to the formula calculation, e.g. the ground truth masks; these take precedence over newly calculated predicate outputs and constants

  • descs (Optional[Sequence[Hashable]]) – the descriptors of the images for obtaining respective predicate outputs from predicates_cache

  • only_formula_out (bool) – whether to prune the output to only contain the output values of the formulas from formulas

Returns

dictionary containing the outputs of the formulas as {out_key: output} with out_key from formulas; if only_formula_out is False, also the following intermediate outputs are added: predicate outputs indexed by the prediate name, all formula outputs and intermediate outputs according to the formula keep_keys settings (by default, all formula intermediate outputs registered for caching are added to this), all initial_masks, constants

Return type

Dict[str, Any]

restrict_to_used_constants()[source]

Set constants to a dict only containing the used constants.

Return type

FormulaEvaluator

conf: Mapping[str, Any]

The configuration mapping used to produce any default settings.

constants: Dict[str, Any]

The constants to supply to the formula evaluation. Have higher precendence than newly calculated predicate outputs.

device: Union[str, torch.device]

The device to use for evaluation.

formula_caches: Optional[Dict[str, Cache]]

Dictionary of {out_key: cache} for caching formula (intermediate) outputs.

formulas: Dict[str, Merge]

The formula objects to evaluate given as dictionary of the form {new_out_key: formula_object}. The new_out_key is used instead of the formula_object.out_key for the final output. The latter is only kept if there is a cache registered for it, see formula_caches.

predicates: torch.nn.modules.module.Module

The model producing the dictionary of predicate outputs.

predicates_cache: Cache

Cache for predicate outputs.

property used_constants: Dict[str, Any]

The constants actually used in one of the formulas.