phanerozoic commited on
Commit
bfd282f
·
verified ·
1 Parent(s): 222a92c

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +53 -53
  2. config.json +9 -9
  3. create_safetensors.py +24 -24
  4. model.py +15 -15
README.md CHANGED
@@ -1,53 +1,53 @@
1
- ---
2
- license: mit
3
- tags:
4
- - pytorch
5
- - safetensors
6
- - threshold-logic
7
- - neuromorphic
8
- ---
9
-
10
- # threshold-2outof5
11
-
12
- At least 2 of 5 inputs high.
13
-
14
- ## Function
15
-
16
- 2outof5(a, b, c, d, e) = 1 if (a + b + c + d + e) >= 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 atleast2of5(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(atleast2of5(0, 0, 0, 0, 1)) # 0 (sum=1)
48
- print(atleast2of5(0, 0, 0, 1, 1)) # 1 (sum=2)
49
- ```
50
-
51
- ## License
52
-
53
- MIT
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-2outof5
11
+
12
+ At least 2 of 5 inputs high.
13
+
14
+ ## Function
15
+
16
+ 2outof5(a, b, c, d, e) = 1 if (a + b + c + d + e) >= 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 atleast2of5(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(atleast2of5(0, 0, 0, 0, 1)) # 0 (sum=1)
48
+ print(atleast2of5(0, 0, 0, 1, 1)) # 1 (sum=2)
49
+ ```
50
+
51
+ ## License
52
+
53
+ MIT
config.json CHANGED
@@ -1,9 +1,9 @@
1
- {
2
- "name": "threshold-2outof5",
3
- "description": "At least 2 of 5 inputs high",
4
- "inputs": 5,
5
- "outputs": 1,
6
- "neurons": 1,
7
- "layers": 1,
8
- "parameters": 6
9
- }
 
1
+ {
2
+ "name": "threshold-2outof5",
3
+ "description": "At least 2 of 5 inputs high",
4
+ "inputs": 5,
5
+ "outputs": 1,
6
+ "neurons": 1,
7
+ "layers": 1,
8
+ "parameters": 6
9
+ }
create_safetensors.py CHANGED
@@ -1,24 +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 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 2outof5...")
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) >= 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}")
 
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 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 2outof5...")
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) >= 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 CHANGED
@@ -1,15 +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 atleast2of5(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('2outof5 sample outputs:')
14
- print(f' 00001 -> {atleast2of5(0,0,0,0,1,w)}')
15
- print(f' 00011 -> {atleast2of5(0,0,0,1,1,w)}')
 
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 atleast2of5(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('2outof5 sample outputs:')
14
+ print(f' 00001 -> {atleast2of5(0,0,0,0,1,w)}')
15
+ print(f' 00011 -> {atleast2of5(0,0,0,1,1,w)}')