CharlesCNorton commited on
Commit
598fc5a
·
0 Parent(s):

At least 2 of 4 threshold circuit, magnitude 6

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