Tensor Operations¶
These functions apply fuzzy reductions to tensors along given axes. Noisy-OR combines independent
probabilities \(p_i\) as \(1 - \prod_i (1 - p_i)\) (Pearl, 1988), so 0.2 and 0.3 combine to
0.44 where a sum gives 0.5. The module gives noisy-OR contraction and reduction, noisy-AND
reduction, and componentwise lifting of tensors. Composition under the default Algebra, the
product t-norm with noisy-OR, matches noisy_or_contract.
tensor_ops
¶
Noisy-OR tensor contraction and marginalization.
The noisy-OR of probabilities p_1, ..., p_n is defined as:
noisy_or(p_1, ..., p_n) = 1 - prod_i (1 - p_i)
This module implements tensor contraction (analogous to matrix multiplication) where the summation is replaced by noisy-OR and the product is standard multiplication. Given tensors M of shape (*D, S) and N of shape (S, C), the contraction over the shared dimensions S produces a tensor of shape (D, *C) with entries:
result[d..., c...] = 1 - prod_{s...} (1 - M[d..., s...] * N[s..., c...])
All operations use log-space for numerical stability.
noisy_or_contract
¶
Contract two tensors via noisy-OR over shared dimensions.
| PARAMETER | DESCRIPTION |
|---|---|
m
|
Left tensor of shape (*domain, *shared).
TYPE:
|
n
|
Right tensor of shape (*shared, *codomain).
TYPE:
|
n_contract
|
Number of trailing dimensions of m (= leading dimensions of n) to contract over.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Result of shape (*domain, *codomain). |
Examples:
>>> M = torch.tensor([[0.5, 0.3], [0.8, 0.1]])
>>> N = torch.tensor([[0.4, 0.6], [0.7, 0.2]])
>>> result = noisy_or_contract(M, N, n_contract=1)
>>> result.shape
torch.Size([2, 2])
Source code in src/quivers/core/tensor_ops.py
noisy_or_reduce
¶
Marginalize (reduce) a tensor over dimensions using noisy-OR.
Computes 1 - prod_i (1 - t_i) along the specified dimension(s). This is the fuzzy analogue of existential quantification (∃).
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Input tensor with values in [0, 1].
TYPE:
|
dim
|
Dimension(s) to reduce over.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reduced tensor with the specified dimensions removed. |
Source code in src/quivers/core/tensor_ops.py
noisy_and_reduce
¶
Reduce a tensor over dimensions using product (fuzzy AND).
Computes prod_i t_i along the specified dimension(s). This is the fuzzy analogue of universal quantification (∀).
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Input tensor with values in [0, 1].
TYPE:
|
dim
|
Dimension(s) to reduce over.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reduced tensor with the specified dimensions removed. |
Source code in src/quivers/core/tensor_ops.py
componentwise_lift
¶
Lift a morphism tensor to the k-fold componentwise product.
This is the functorial action of the free monoid functor on morphisms. Given f of shape (|A|, |B|), produces f^k of shape (|A|,)k + (|B|,)k where:
f^k[a1, ..., ak, b1, ..., bk] = ⊗_i f[ai, bi]
The tensor product ⊗ is determined by the algebra (defaults to ProductFuzzyAlgebra, where ⊗ = ordinary multiplication).
Categorically, this is the monoidal functor action: the free monoid on objects sends A to A* = 1 + A + A×A + ..., and on morphisms sends f: A → B to f: A → B* acting componentwise on each length stratum.
| PARAMETER | DESCRIPTION |
|---|---|
f
|
Morphism tensor of shape (|A|, |B|) — a 2D fuzzy relation.
TYPE:
|
k
|
String length (number of components). Must be >= 0.
TYPE:
|
algebra
|
The algebra whose tensor_op is used for the componentwise product. If None, defaults to PRODUCT_FUZZY.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Lifted tensor of shape (|A|,)k + (|B|,)k. For k=0, returns a tensor of shape (1, 1) filled with the algebra's unit value. For k=1, returns f unchanged. |
Source code in src/quivers/core/tensor_ops.py
diagonal_reduce
¶
diagonal_reduce(tensor: Tensor, contra_dims: tuple[int, ...], co_dims: tuple[int, ...], reduce_fn: Callable[[Tensor, int], Tensor]) -> Tensor
Restrict to the diagonal of matched dimension pairs and reduce over it.
For each pair (contra_dims[i], co_dims[i]) the tensor is
restricted to the entries whose two indices agree, and the shared
index is reduced with reduce_fn; the other dimensions keep their
order.
| PARAMETER | DESCRIPTION |
|---|---|
tensor
|
Input tensor.
TYPE:
|
contra_dims
|
Dimension indices of the first member of each pair.
TYPE:
|
co_dims
|
Dimension indices of the second member of each pair.
TYPE:
|
reduce_fn
|
Reduction taking |
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The reduced tensor. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a pair's dimensions differ in size, or a dimension is used twice. |
Source code in src/quivers/core/tensor_ops.py
Cited sources¶
Judea Pearl. Probabilistic Reasoning in Intelligent Systems: Networks of Plausible Inference. Morgan Kaufmann, 1988. doi:10.1016/C2009-0-27609-4.