--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic --- # threshold-exactly2outof3 Exactly 2 of 3 inputs high. ## Function exactly2outof3(a, b, c) = 1 if (a + b + c) == 2, else 0 ## Truth Table | a | b | c | sum | out | |---|---|---|-----|-----| | 0 | 0 | 0 | 0 | 0 | | 0 | 0 | 1 | 1 | 0 | | 0 | 1 | 0 | 1 | 0 | | 0 | 1 | 1 | 2 | 1 | | 1 | 0 | 0 | 1 | 0 | | 1 | 0 | 1 | 2 | 1 | | 1 | 1 | 0 | 2 | 1 | | 1 | 1 | 1 | 3 | 0 | ## Architecture Two layers (not linearly separable): **Layer 1:** - N1: weights [1, 1, 1], bias -2 (fires when sum >= 2) - N2: weights [-1, -1, -1], bias 2 (fires when sum <= 2) **Layer 2:** - AND: weights [1, 1], bias -2 (fires when both N1 and N2 fire) ## Parameters | | | |---|---| | Inputs | 3 | | Outputs | 1 | | Neurons | 3 | | Layers | 2 | | Parameters | 11 | | Magnitude | 14 | ## Usage ```python from safetensors.torch import load_file import torch w = load_file('model.safetensors') def exactly2of3(a, b, c): inp = torch.tensor([float(a), float(b), float(c)]) l1 = (inp @ w['layer1.weight'].T + w['layer1.bias'] >= 0).float() out = (l1 @ w['layer2.weight'].T + w['layer2.bias'] >= 0).float() return int(out.item()) print(exactly2of3(0, 1, 1)) # 1 (sum=2) print(exactly2of3(1, 1, 1)) # 0 (sum=3) ``` ## License MIT