| | --- |
| | license: mit |
| | tags: |
| | - pytorch |
| | - safetensors |
| | - threshold-logic |
| | - neuromorphic |
| | --- |
| | |
| | # threshold-exactly4outof5 |
| |
|
| | Exactly 4 of 5 inputs high. |
| |
|
| | ## Function |
| |
|
| | exactly4outof5(a, b, c, d, e) = 1 if (a + b + c + d + e) == 4, else 0 |
| |
|
| | ## Truth Table (selected) |
| |
|
| | | sum | out | |
| | |-----|-----| |
| | | 0 | 0 | |
| | | 1 | 0 | |
| | | 2 | 0 | |
| | | 3 | 0 | |
| | | 4 | 1 | |
| | | 5 | 0 | |
| |
|
| | ## Architecture |
| |
|
| | Two layers required (exactly-k is not linearly separable). |
| |
|
| | **Layer 1:** |
| | - N1: sum >= 4 (weights [1,1,1,1,1], bias -4) |
| | - N2: sum <= 4 (weights [-1,-1,-1,-1,-1], bias 4) |
| |
|
| | **Layer 2:** |
| | - AND(N1, N2): weights [1,1], bias -2 |
| |
|
| | ## Parameters |
| |
|
| | | | | |
| | |---|---| |
| | | Inputs | 5 | |
| | | Outputs | 1 | |
| | | Neurons | 3 | |
| | | Layers | 2 | |
| | | Parameters | 15 | |
| | | Magnitude | 22 | |
| |
|
| | ## Usage |
| |
|
| | ```python |
| | from safetensors.torch import load_file |
| | import torch |
| | |
| | w = load_file('model.safetensors') |
| | |
| | def exactly4of5(a, b, c, d, e): |
| | inp = torch.tensor([float(a), float(b), float(c), float(d), float(e)]) |
| | 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(exactly4of5(1, 1, 1, 1, 0)) # 1 (sum=4) |
| | print(exactly4of5(1, 1, 1, 1, 1)) # 0 (sum=5) |
| | ``` |
| |
|
| | ## License |
| |
|
| | MIT |
| |
|