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

At least 1 of 3 threshold circuit, magnitude 4

Browse files
Files changed (5) hide show
  1. README.md +68 -0
  2. config.json +9 -0
  3. create_safetensors.py +25 -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-1outof3
11
+
12
+ At least 1 of 3 inputs high. Equivalent to 3-input OR gate.
13
+
14
+ ## Function
15
+
16
+ 1outof3(a, b, c) = 1 if (a + b + c) >= 1, else 0
17
+
18
+ Equivalently: OR(a, b, c)
19
+
20
+ ## Truth Table
21
+
22
+ | a | b | c | sum | out |
23
+ |---|---|---|-----|-----|
24
+ | 0 | 0 | 0 | 0 | 0 |
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 | 1 |
32
+
33
+ ## Architecture
34
+
35
+ Single neuron: weights [1, 1, 1], bias -1
36
+
37
+ Fires when: a + b + c - 1 >= 0, i.e., sum >= 1
38
+
39
+ ## Parameters
40
+
41
+ | | |
42
+ |---|---|
43
+ | Inputs | 3 |
44
+ | Outputs | 1 |
45
+ | Neurons | 1 |
46
+ | Layers | 1 |
47
+ | Parameters | 4 |
48
+ | Magnitude | 4 |
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 atleast1of3(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(atleast1of3(0, 0, 0)) # 0
63
+ print(atleast1of3(0, 0, 1)) # 1
64
+ ```
65
+
66
+ ## License
67
+
68
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-1outof3",
3
+ "description": "At least 1 of 3 inputs high (3-input OR)",
4
+ "inputs": 3,
5
+ "outputs": 1,
6
+ "neurons": 1,
7
+ "layers": 1,
8
+ "parameters": 4
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]], dtype=torch.float32),
6
+ 'neuron.bias': torch.tensor([-1.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 1outof3...")
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) >= 1 else 0
20
+ if result != expected:
21
+ errors += 1
22
+ print(f"ERROR: {a}{b}{c} -> {result}, expected {expected}")
23
+ if errors == 0:
24
+ print("All 8 test cases passed!")
25
+ 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 atleast1of3(a, b, c, weights):
8
+ """Returns 1 if at least 1 of 3 inputs is high (OR gate)"""
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('1outof3 (OR3) 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} -> {atleast1of3(a, b, c, w)}')
model.safetensors ADDED
Binary file (160 Bytes). View file