File size: 1,214 Bytes
035d82d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
---
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