Tensor Operations¶
Vectorized tensor operations and broadcasting utilities for efficient computation.
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
¶
noisy_or_contract(m: Tensor, n: Tensor, n_contract: int) -> Tensor
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
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
noisy_or_reduce
¶
noisy_or_reduce(t: Tensor, dim: int | tuple[int, ...]) -> Tensor
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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
noisy_and_reduce
¶
noisy_and_reduce(t: Tensor, dim: int | tuple[int, ...]) -> Tensor
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
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
componentwise_lift
¶
componentwise_lift(f: Tensor, k: int, quantale: Quantale | None = None) -> Tensor
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 quantale (defaults to ProductFuzzy, 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:
|
quantale
|
The quantale 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 quantale's unit value. For k=1, returns f unchanged. |
Source code in src/quivers/core/tensor_ops.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |