Cognitive Language Interface

Type

Predicate

Conditional

VariableMap

class pysigma.structures.VariableMap(mapping_func, domain, codomain, dynamic=False)[source]

Class type for declaring mappings on relational variables in pattern elements. Wraps around a user-defined mapping function and records the one-to-one correspondence between inputs and outputs.

Since relational variables can be viewed as non-negative finite integer-valued variables, a VariableMap instance therefore declares an integer-valued mapping with finite domain.

The domain and codomain are assumed fixed, so they should be provided during initialization. The mapping can be computed either lazily at runtime or pre-computed at initialization. This flexibility is to allow dynamic mappings such as neural attention modules.

Only injective mapping can be accepted. This is because the semantic of mapping two relational variable’s values to a single value is ill-defined. The injectivity is checked during set_map() by comparing the cardinality of the image and the cardinality of the domain.

Parameters
  • mapping_func (callable) – a user-specified function. Takes a numpy array of integers as input, and returns a numpy array of integers of the same size. Each entry in the output array corresponds to the value of f(x) of input x at the same index in the input array.

  • domain (set of int) – The domain of the mapping.

  • codomain (set of int) – The codomain of the mapping.

  • dynamic (bool, optional) – Indicates whether the mapping is dynamic. If True, then mapping_func will be called each time a mapping dictionary is desired, i.e., when get_map() or get_inverse_map() is called. Otherwise, mapping_func will only be called once during initialization of this VariableMap instance, and the result will cached. Defaults to False.

mapping_func
domain
codomain
dynamic
map

The mapping cache.

Type

dict

image

Image of the map. Should be a subset of self.codomain.

Type

set

set_map()[source]

Set self.map by obtaining the mapping dictionary from self.mapping_func.

Raises
  • ValueError – If self.mapping_func does not return a numpy array or returns one with wrong data type.

  • ValueError – If self.mapping_func does not return a numpy array with the same size as the input array.

  • ValueError – If self.mapping_func contains values not in the codomain.

  • ValueError – If self.mapping_func is found to be not injective.

get_map()[source]

Returns the mapping dictionary, the map’s domain, and the map’s image.

If dynamic, then calls set_map() to re-compute the dict first, otherwise returns the cached one.

Returns

A 3-tuple containing the data (map_dict, domain, image).

Return type

tuple

get_inverse_map()[source]

Returns the inverse map’s mapping dictionary, the inverse map’s domain (original map’s image), and the inverse map’s image (should be the same as the original map’s domain)

Note that because injectivity is guaranteed, computing an inverse map is possible.

If dynamic, then calls set_map() to re-compute the dict first, otherwise returns the cached one.

Returns

A 3-tuple containing the data (inv_map_dict, inv_image, inv_domain).

Return type

tuple

FactorFunction

Summarization

class pysigma.structures.Summarization(sum_func)[source]

The decorator class for the custom summarization callback procedures which will be called during the outward summarization step in the Expansion / Summarization Factor Node (ESFN).

Provides the decorator @Summarization.

Note

The custom callback function should have the following signature:

Parameters
  • content_type ({'distribution', 'particles', 'both'}) – The type of the content to process for the current execution.

  • rv_info (OrderedDict) – Information / metadata regarding the random variables of the message. Each key-value pair in the dictionary is key-ed by the random variable’s name var_name : str and contains a 2-tuple (var_size : int, constraints : set of torch.distributions.Constraint) as the value. Note that rv_info is ordered, and this order of the random variables will be respected by entries in events and log_densities.

  • batch_shape (torch.Size) – A size-2 torch.Size list representing the batch dimension shapes. The first dimension is the batch dimension to be summarized over and reduced, and the second dimension should be left untouched.

  • dist (torch.distributions.Distribution, optional) – The batched distribution instance representing the message. dist ‘s batch shape is batch_shape, and its event shape is the sum of the sizes of all random variables. This field will be provided when content_type is distribution or both, or will be left None when content_type is particles.

  • events (tuple of torch.Tensor, optional) – The tuple of event value tensors. Each entry is a 2D tensor corresponding to one of the random variables declared in rv_info associated with the message. The first dimension is the sample dimension with a size equal to the number of event particles. The last dimension is the event dimension with a size equal to the size of the corresponding random variable. This field will be provided when content_type is particles or both, or will be left None when content_type is parameter.

  • log_densities (tuple of torch.Tensor, optional) – The tuple of marginal log sampling densities of each of the event values in events. Each entry is a 1D tensor annotating the event value tensor in events with the same tuple index. The dimension size equals to the number of event particles. This field will be provided when content_type is particles or both, or will be left None when content_type is parameter.

  • weight (torch.Tensor, optional) – The event particles weight tensor. Have shape (batch_shape + sample_shape), where sample_shape is the concatenated list of the sample sizes of all event tensors in events. This field will be provided when content_type is particles or both, or will be left None when content_type is parameter.

Returns

A 2-tuple consisting of (reduced_dist : torch.distributions.Distribution or None, reduced_weight : torch.Tensor or None). reduced_dist will be ignored if content_type is particles, and reduced_weight will be ignored if content_type is parameter. Both of the entries should have the reduced batch shape of length 1.

Return type

tuple

__call__(msg, ran_vars)[source]

Implements so that the Summarization instance is a callable functor.

The summarization instance should be called by ESFN internally only.

Parameters
  • msg (Message) – The incoming message. Batch shape should be of length 2, with the first batch dimension being the one to be summarized over, and the second dimension being the one to be left untouched.

  • ran_vars (tuple of Variable) – The tuple of random variables of the target Variable node

Returns

The reduced message. Will have only one batch dimension which is the one to be left untouched from the original message.

Return type

Message