CharlesCNorton commited on
Commit
3a5e261
·
0 Parent(s):

At most 2 of 5 threshold circuit, magnitude 7

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