CharlesCNorton commited on
Commit
035d82d
·
0 Parent(s):

Exactly 4 of 5 threshold circuit, magnitude 22

Browse files
Files changed (5) hide show
  1. README.md +71 -0
  2. config.json +9 -0
  3. create_safetensors.py +32 -0
  4. model.py +18 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-exactly4outof5
11
+
12
+ Exactly 4 of 5 inputs high.
13
+
14
+ ## Function
15
+
16
+ exactly4outof5(a, b, c, d, e) = 1 if (a + b + c + d + e) == 4, else 0
17
+
18
+ ## Truth Table (selected)
19
+
20
+ | sum | out |
21
+ |-----|-----|
22
+ | 0 | 0 |
23
+ | 1 | 0 |
24
+ | 2 | 0 |
25
+ | 3 | 0 |
26
+ | 4 | 1 |
27
+ | 5 | 0 |
28
+
29
+ ## Architecture
30
+
31
+ Two layers required (exactly-k is not linearly separable).
32
+
33
+ **Layer 1:**
34
+ - N1: sum >= 4 (weights [1,1,1,1,1], bias -4)
35
+ - N2: sum <= 4 (weights [-1,-1,-1,-1,-1], bias 4)
36
+
37
+ **Layer 2:**
38
+ - AND(N1, N2): weights [1,1], bias -2
39
+
40
+ ## Parameters
41
+
42
+ | | |
43
+ |---|---|
44
+ | Inputs | 5 |
45
+ | Outputs | 1 |
46
+ | Neurons | 3 |
47
+ | Layers | 2 |
48
+ | Parameters | 15 |
49
+ | Magnitude | 22 |
50
+
51
+ ## Usage
52
+
53
+ ```python
54
+ from safetensors.torch import load_file
55
+ import torch
56
+
57
+ w = load_file('model.safetensors')
58
+
59
+ def exactly4of5(a, b, c, d, e):
60
+ inp = torch.tensor([float(a), float(b), float(c), float(d), float(e)])
61
+ l1 = (inp @ w['layer1.weight'].T + w['layer1.bias'] >= 0).float()
62
+ out = (l1 @ w['layer2.weight'].T + w['layer2.bias'] >= 0).float()
63
+ return int(out.item())
64
+
65
+ print(exactly4of5(1, 1, 1, 1, 0)) # 1 (sum=4)
66
+ print(exactly4of5(1, 1, 1, 1, 1)) # 0 (sum=5)
67
+ ```
68
+
69
+ ## License
70
+
71
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-exactly4outof5",
3
+ "description": "Exactly 4 of 5 inputs high",
4
+ "inputs": 5,
5
+ "outputs": 1,
6
+ "neurons": 3,
7
+ "layers": 2,
8
+ "parameters": 15
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {
5
+ 'layer1.weight': torch.tensor([
6
+ [1.0, 1.0, 1.0, 1.0, 1.0], # N1: sum >= 4
7
+ [-1.0, -1.0, -1.0, -1.0, -1.0] # N2: sum <= 4
8
+ ], dtype=torch.float32),
9
+ 'layer1.bias': torch.tensor([-4.0, 4.0], dtype=torch.float32),
10
+ 'layer2.weight': torch.tensor([[1.0, 1.0]], dtype=torch.float32),
11
+ 'layer2.bias': torch.tensor([-2.0], dtype=torch.float32)
12
+ }
13
+ save_file(weights, 'model.safetensors')
14
+
15
+ def exactly4of5(a, b, c, d, e):
16
+ inp = torch.tensor([float(a), float(b), float(c), float(d), float(e)])
17
+ l1 = (inp @ weights['layer1.weight'].T + weights['layer1.bias'] >= 0).float()
18
+ out = (l1 @ weights['layer2.weight'].T + weights['layer2.bias'] >= 0).float()
19
+ return int(out.item())
20
+
21
+ print("Verifying exactly4outof5...")
22
+ errors = 0
23
+ for i in range(32):
24
+ bits = [(i >> j) & 1 for j in range(5)]
25
+ result = exactly4of5(*bits)
26
+ expected = 1 if sum(bits) == 4 else 0
27
+ if result != expected:
28
+ errors += 1
29
+ print(f"ERROR: {bits} -> {result}, expected {expected}")
30
+ if errors == 0:
31
+ print("All 32 test cases passed!")
32
+ print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
model.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import load_file
3
+
4
+ def load_model(path='model.safetensors'):
5
+ return load_file(path)
6
+
7
+ def exactly4of5(a, b, c, d, e, weights):
8
+ inp = torch.tensor([float(a), float(b), float(c), float(d), float(e)])
9
+ l1 = (inp @ weights['layer1.weight'].T + weights['layer1.bias'] >= 0).float()
10
+ out = (l1 @ weights['layer2.weight'].T + weights['layer2.bias'] >= 0).float()
11
+ return int(out.item())
12
+
13
+ if __name__ == '__main__':
14
+ w = load_model()
15
+ print('exactly4outof5 selected outputs:')
16
+ for s in range(6):
17
+ bits = [(1 if j < s else 0) for j in range(5)]
18
+ print(f' sum={s}: {bits} -> {exactly4of5(*bits, w)}')
model.safetensors ADDED
Binary file (340 Bytes). View file