CharlesCNorton commited on
Commit
eb0c29e
·
0 Parent(s):

Exactly 2 of 4 threshold circuit, magnitude 16

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