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

At least 4 of 5 threshold circuit, magnitude 9

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