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

5-input majority gate, magnitude 8

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 +17 -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-majority5
11
+
12
+ 5-input majority gate. Outputs 1 when at least 3 of 5 inputs are high.
13
+
14
+ ## Function
15
+
16
+ majority5(a, b, c, d, e) = 1 if (a + b + c + d + e) >= 3, else 0
17
+
18
+ ## Architecture
19
+
20
+ Single neuron: weights [1, 1, 1, 1, 1], bias -3
21
+
22
+ Fires when: sum >= 3
23
+
24
+ ## Parameters
25
+
26
+ | | |
27
+ |---|---|
28
+ | Inputs | 5 |
29
+ | Outputs | 1 |
30
+ | Neurons | 1 |
31
+ | Layers | 1 |
32
+ | Parameters | 6 |
33
+ | Magnitude | 8 |
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 majority5(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(majority5(0, 0, 1, 1, 1)) # 1
48
+ print(majority5(0, 0, 0, 1, 1)) # 0
49
+ ```
50
+
51
+ ## License
52
+
53
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-majority5",
3
+ "description": "5-input majority gate",
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([-3.0], dtype=torch.float32)
7
+ }
8
+ save_file(weights, 'model.safetensors')
9
+
10
+ def majority5(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 majority5...")
15
+ errors = 0
16
+ for i in range(32):
17
+ bits = [(i >> j) & 1 for j in range(4, -1, -1)]
18
+ result = majority5(*bits)
19
+ expected = 1 if sum(bits) >= 3 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,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 majority5(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('majority5 sample outputs:')
14
+ print(f' 00000 -> {majority5(0,0,0,0,0,w)}')
15
+ print(f' 00011 -> {majority5(0,0,0,1,1,w)}')
16
+ print(f' 00111 -> {majority5(0,0,1,1,1,w)}')
17
+ print(f' 11111 -> {majority5(1,1,1,1,1,w)}')
model.safetensors ADDED
Binary file (168 Bytes). View file