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

5-input minority gate, magnitude 7

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