CharlesCNorton commited on
Commit
69d9906
·
0 Parent(s):

At most 2 of 3 threshold circuit (NAND3), magnitude 5

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