--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic --- # threshold-atmost2outof3 At most 2 of 3 inputs high. Equivalent to 3-input NAND gate. ## Function atmost2outof3(a, b, c) = 1 if (a + b + c) <= 2, else 0 Equivalently: NAND(a, b, c) = NOT(a AND b AND c) ## Truth Table | a | b | c | sum | out | |---|---|---|-----|-----| | 0 | 0 | 0 | 0 | 1 | | 0 | 0 | 1 | 1 | 1 | | 0 | 1 | 0 | 1 | 1 | | 0 | 1 | 1 | 2 | 1 | | 1 | 0 | 0 | 1 | 1 | | 1 | 0 | 1 | 2 | 1 | | 1 | 1 | 0 | 2 | 1 | | 1 | 1 | 1 | 3 | 0 | ## Architecture Single neuron: weights [-1, -1, -1], bias 2 Fires when: -a - b - c + 2 >= 0, i.e., sum <= 2 ## Parameters | | | |---|---| | Inputs | 3 | | Outputs | 1 | | Neurons | 1 | | Layers | 1 | | Parameters | 4 | | Magnitude | 5 | ## Usage ```python from safetensors.torch import load_file import torch w = load_file('model.safetensors') def atmost2of3(a, b, c): inp = torch.tensor([float(a), float(b), float(c)]) return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item()) print(atmost2of3(1, 1, 0)) # 1 (sum=2) print(atmost2of3(1, 1, 1)) # 0 (sum=3) ``` ## License MIT