TorchOrNumpyOperation
- class hybrid_learning.fuzzy_logic.logic_base.merge_operation.TorchOrNumpyOperation(*in_keys, out_key=None, overwrite=True, skip_none=True, replace_none=None, symb=None, cache_duplicates=True, keep_keys=None, _variadic=False)[source]
Bases:
TorchOperation
,ABC
Generic merge operation allowing to define both a torch and a numpy operation. Which one is selected depends on the types of the provided annotations: If any is a torch tensor, the torch operation is used and a torch tensor returned, otherwise the numpy operation.
Public Data Attributes:
Inherited from : py: class:Merge
SYMB
The string symbol of this class (override for sub-classes).
ARITY
The arity of the operation.
IS_COMMUTATIVE
Whether instances are equivalent to ones with permuted
in_keys
.is_variadic
Whether the instance is variadic.
settings
Settings to reproduce the instance.
setting_defaults
Defaults used for
settings
.pretty_op_symb
Name of the operation symbol suitable for filenames etc.
children
The input keys which are child operations.
all_children
All children operations in the flattened computational tree, sorted depth first.
consts
The constant string keys in the input keys.
operation_keys
The list of keys used for this parent operation in original order (constants and children output keys).
all_in_keys
All string input keys both of self and of all child operations.
all_out_keys
Output keys of self and all child operations.
Inherited from : py: class:DictTransform
settings
Settings to reproduce the instance.
Inherited from : py: class:Transform
IDENTITY_CLASS
The identity class or classes for composition / addition.
settings
Settings to reproduce the instance.
Public Methods:
operation
(annotation_vals)Operation on either torch tensors or Booleans, numpy arrays and numbers.
Inherited from : py: class:TorchOperation
operation
(annotation_vals)Operation on either torch tensors or Booleans, numpy arrays and numbers.
Inherited from : py: class:Merge
to_infix_notation
([sort_key, ...])Return an infix str encoding equal for differently sorted operations.
to_str
(**infix_notation_kwargs)Alias for
to_infix_notation()
.to_pretty_str
(**infix_notation_kwargs)Same as
to_str()
but using pretty operation names suitable for filenames etc.to_repr
([settings, defaults, sort_key, ...])Return str representation which can be used to reproduce and compare the instance.
treerecurse_replace_keys
(**replace_map)Return a new formula with all occurences of variables in
replace_map
replaced and else identical settings.treerecurse
(fun)Apply the given function recursively to this and all children instances.
apply_to
(annotations[, keep_keys])Apply this operation to the
annotations
dict.variadic_apply_to
(annotations)Return the result of operation on the values/items of a mapping or sequence of arbitrary length.
operation
(annotation_vals)Operation on either torch tensors or Booleans, numpy arrays and numbers.
Inherited from : py: class:DictTransform
apply_to
(annotations[, keep_keys])Apply this operation to the
annotations
dict.Inherited from : py: class:Transform
apply_to
(annotations[, keep_keys])Apply this operation to the
annotations
dict.Special Methods:
Inherited from : py: class:Merge
__init__
(*in_keys[, out_key, overwrite, ...])Init.
__str__
()Return str(self).
__repr__
()Call
to_repr()
without sorting.__eq__
(other)Two merge operations are considered equal, if their normalized representations coincide.
__copy__
()Return a deep copy of self using settings.
__call__
(annotations[, keep_keys])Call method modifying a given dictionary.
Inherited from : py: class:DictTransform
__call__
(annotations[, keep_keys])Call method modifying a given dictionary.
Inherited from : py: class:Transform
__repr__
()Call
to_repr()
without sorting.__eq__
(other)Two merge operations are considered equal, if their normalized representations coincide.
__copy__
()Return a deep copy of self using settings.
__add__
(other)Return a flat composition of
self
withother
.__radd__
(other)Return a flat composition of
other
andself
.__call__
(annotations[, keep_keys])Call method modifying a given dictionary.
- Parameters
- abstract static numpy_operation(*inputs)[source]
Operation on Booleans, numpy arrays and numbers. If possible, the operation should support broadcasting.
- Parameters
inputs (ndarray) –
- Return type
ndarray
- operation(annotation_vals)[source]
Operation on either torch tensors or Booleans, numpy arrays and numbers.
- cache_duplicates: bool
Whether to cache duplicate child operation outputs with duplicate out_key. If set to false, all children and children children are evaluated and the values of duplicate
out_keys
are evaluated several times and overwritten, possibly leading to more computational time while using less memory. Note that the order of children execution is determined by their order inin_keys
, depth first for nested operations.
- in_keys: Sequence[Union[str, Merge]]
The keys of segmentation masks to unite in given order. Keys are either constant strings or a merge operation.
- keep_keys: Optional[Collection[str]]
The keys of intermediate outputs in
all_out_keys
which should be added to the return of a call. Default (None
or empty collection): duplicate children outputs are cached but not returned to save memory.
- out_key: str
The key to use to store the merge output in the annotations dict. Take care to not accidentally overwrite existing keys (cf.
overwrite
).
- overwrite: Union[bool, Literal['noop']]
Whether to overwrite a value in the input dictionary when applying this operation. The operation is defined in
operation()
. The key that may be overwritten is stored inout_key
. An exception is raised if this isFalse
and the key exists. If set to'noop'
andout_key
is in the given annotations dict, it is returned unchanged.
- replace_none: Optional[Any]
If not
None
, any receivedNone
value is replaced by the given value. This is done only for computation, theNone
value in the received dict is left unchanged. Key-value pairs withNone
value may come from the input or from child operations.