CharlesCNorton commited on
Commit
92ccbac
·
0 Parent(s):

7-input majority gate, magnitude 11

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-majority7
11
+
12
+ 7-input majority gate. Outputs 1 when at least 4 of 7 inputs are high.
13
+
14
+ ## Function
15
+
16
+ majority7(a, b, c, d, e, f, g) = 1 if sum >= 4, else 0
17
+
18
+ ## Architecture
19
+
20
+ Single neuron: weights [1, 1, 1, 1, 1, 1, 1], bias -4
21
+
22
+ Fires when: sum >= 4
23
+
24
+ ## Parameters
25
+
26
+ | | |
27
+ |---|---|
28
+ | Inputs | 7 |
29
+ | Outputs | 1 |
30
+ | Neurons | 1 |
31
+ | Layers | 1 |
32
+ | Parameters | 8 |
33
+ | Magnitude | 11 |
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 majority7(a, b, c, d, e, f, g):
44
+ inp = torch.tensor([float(a), float(b), float(c), float(d), float(e), float(f), float(g)])
45
+ return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item())
46
+
47
+ print(majority7(0, 0, 0, 1, 1, 1, 1)) # 1
48
+ print(majority7(0, 0, 0, 0, 1, 1, 1)) # 0
49
+ ```
50
+
51
+ ## License
52
+
53
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-majority7",
3
+ "description": "7-input majority gate",
4
+ "inputs": 7,
5
+ "outputs": 1,
6
+ "neurons": 1,
7
+ "layers": 1,
8
+ "parameters": 8
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, 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 majority7(*args):
11
+ inp = torch.tensor([float(x) for x in args])
12
+ return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
13
+
14
+ print("Verifying majority7...")
15
+ errors = 0
16
+ for i in range(128):
17
+ bits = [(i >> j) & 1 for j in range(6, -1, -1)]
18
+ result = majority7(*bits)
19
+ expected = 1 if sum(bits) >= 4 else 0
20
+ if result != expected:
21
+ errors += 1
22
+ if errors == 0:
23
+ print("All 128 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 majority7(a, b, c, d, e, f, g, weights):
8
+ inp = torch.tensor([float(a), float(b), float(c), float(d), float(e), float(f), float(g)])
9
+ return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
10
+
11
+ if __name__ == '__main__':
12
+ w = load_model()
13
+ print('majority7 sample outputs:')
14
+ print(f' 0000000 -> {majority7(0,0,0,0,0,0,0,w)}')
15
+ print(f' 0000111 -> {majority7(0,0,0,0,1,1,1,w)}')
16
+ print(f' 0001111 -> {majority7(0,0,0,1,1,1,1,w)}')
17
+ print(f' 1111111 -> {majority7(1,1,1,1,1,1,1,w)}')
model.safetensors ADDED
Binary file (176 Bytes). View file